/*
 * Copyright (c) 2014, The Dattack team (http://www.dattack.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dattack.ext.jdbc;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dattack.ext.io.FilesystemUtils;

/**
 * @author cvarela
 * @since 0.1
 */
public final class DataSourceClasspathDecorator extends DataSourceDecorator {

    private static final Logger LOGGER = LoggerFactory.getLogger(JNDIDataSource.class);

    private final List<String> extraClasspath;
    private volatile boolean initialized;

    public DataSourceClasspathDecorator(final DataSource inner, final List<String> extraClasspath) {
        super(inner);
        this.extraClasspath = new ArrayList<String>(extraClasspath);
        initialized = false;
    }

    private synchronized void configureClasspath() {

        if (initialized) {
            return;
        }

        for (final String path : extraClasspath) {
            final File file = FilesystemUtils.locate(path);
            if (file.exists()) {
                try {
                    if (file.isDirectory()) {
                        configureDirectoryClasspath(file);
                    } else {
                        configureJarClasspath(file);
                    }
                } catch (final Exception e) {
                    LOGGER.warn(e.getMessage());
                }
            } else {
                LOGGER.debug("Missing directory/file: '{}'", path);
            }
        }

        initialized = true;
    }

    private static void configureClasspath(final List<URL> urlList) throws NoSuchMethodException, SecurityException,
    IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {

        try (final URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader()) {
            final Class<?> urlClass = URLClassLoader.class;
            final Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
            method.setAccessible(true);

            for (final URL u : urlList) {
                method.invoke(urlClassLoader, new Object[] { u });
            }
        }
    }

    private static void configureDirectoryClasspath(final File directory)
            throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, IOException {

        final File[] jars = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File pathname) {
                return pathname.isFile() && pathname.getAbsolutePath().endsWith(".jar");
            }
        });

        final List<URL> urlList = new ArrayList<URL>();
        for (final File jar : jars) {
            try {
                LOGGER.info("Scanning JAR: {}", jar);
                urlList.add(jar.toURI().toURL());
            } catch (final MalformedURLException e) {
                LOGGER.warn(e.getMessage());
            }
        }

        configureClasspath(urlList);
    }

    private static void configureJarClasspath(final File jar) throws NoSuchMethodException, SecurityException,
    IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {

        final List<URL> urlList = new ArrayList<URL>();
        try {
            LOGGER.info("Scanning JAR: {}", jar);
            urlList.add(jar.toURI().toURL());
            configureClasspath(urlList);
        } catch (final MalformedURLException e) {
            LOGGER.warn(e.getMessage());
        }
    }

    @Override
    public Connection getConnection() throws SQLException {

        if (!initialized) {
            configureClasspath();
        }

        return super.getConnection();
    }

    @Override
    public Connection getConnection(final String username, final String password) throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        return super.getConnection(username, password);
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        return super.getLoginTimeout();
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        return super.getLogWriter();
    }

    @Override
    public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
        if (!initialized) {
            configureClasspath();
        }
        return super.getParentLogger();
    }

    @Override
    public boolean isWrapperFor(final Class<?> iface) throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        return super.isWrapperFor(iface);
    }

    @Override
    public void setLoginTimeout(final int seconds) throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        super.setLoginTimeout(seconds);

    }

    @Override
    public void setLogWriter(final PrintWriter out) throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        super.setLogWriter(out);
    }

    @Override
    public <T> T unwrap(final Class<T> iface) throws SQLException {
        if (!initialized) {
            configureClasspath();
        }
        return super.unwrap(iface);
    }
}

@@ -706,36 +706,8 @@
 er;%0A
-import java.io.IOException;%0A
 impo
@@ -2674,135 +2674,12 @@
 tion
-, IOException %7B%0A%0A        try (final URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader()) %7B%0A    
+ %7B%0A%0A
     
@@ -2730,20 +2730,16 @@
 .class;%0A
-    
         
@@ -2829,28 +2829,24 @@
 %7D);%0A        
-    
 method.setAc
@@ -2866,28 +2866,24 @@
 );%0A%0A        
-  
-  
 for (final U
@@ -2908,28 +2908,24 @@
             
-    
 method.invok
@@ -2926,19 +2926,37 @@
 .invoke(
-url
+ClassLoader.getSystem
 ClassLoa
@@ -2958,16 +2958,18 @@
 ssLoader
+()
 , new Ob
@@ -2983,30 +2983,16 @@
 %7B u %7D);%0A
-            %7D%0A
         
@@ -3218,37 +3218,24 @@
 getException
-, IOException
  %7B%0A%0A        
@@ -3975,32 +3975,40 @@
 urityException,%0A
+        
     IllegalAcces
@@ -4074,21 +4074,8 @@
 tion
-, IOException
  %7B%0A%0A

Fix a bug in loading drivers