Coverage Report - org.owasp.dependencycheck.data.nvdcve.DriverLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
DriverLoader
57%
27/47
100%
8/8
4.4
DriverLoader$1
100%
2/2
N/A
4.4
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.nvdcve;
 19  
 
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import java.io.File;
 24  
 import java.net.MalformedURLException;
 25  
 import java.net.URL;
 26  
 import java.net.URLClassLoader;
 27  
 import java.security.AccessController;
 28  
 import java.security.PrivilegedAction;
 29  
 import java.sql.Driver;
 30  
 import java.sql.DriverManager;
 31  
 import java.sql.SQLException;
 32  
 import java.util.ArrayList;
 33  
 import java.util.List;
 34  
 
 35  
 /**
 36  
  * DriverLoader is a utility class that is used to load database drivers.
 37  
  *
 38  
  * @author Jeremy Long
 39  
  */
 40  
 public final class DriverLoader {
 41  
 
 42  
     /**
 43  
      * The logger.
 44  
      */
 45  1
     private static final Logger LOGGER = LoggerFactory.getLogger(DriverLoader.class);
 46  
 
 47  
     /**
 48  
      * Private constructor for a utility class.
 49  
      */
 50  0
     private DriverLoader() {
 51  0
     }
 52  
 
 53  
     /**
 54  
      * Loads the specified class using the system class loader and registers the driver with the driver manager.
 55  
      *
 56  
      * @param className the fully qualified name of the desired class
 57  
      * @return the loaded Driver
 58  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 59  
      */
 60  
     public static Driver load(String className) throws DriverLoadException {
 61  3
         final ClassLoader loader = DriverLoader.class.getClassLoader(); //ClassLoader.getSystemClassLoader();
 62  3
         return load(className, loader);
 63  
     }
 64  
 
 65  
     /**
 66  
      * Loads the specified class by registering the supplied paths to the class loader and then registers the driver
 67  
      * with the driver manager. The pathToDriver argument is added to the class loader so that an external driver can be
 68  
      * loaded. Note, the pathToDriver can contain a semi-colon separated list of paths so any dependencies can be added
 69  
      * as needed. If a path in the pathToDriver argument is a directory all files in the directory are added to the
 70  
      * class path.
 71  
      *
 72  
      * @param className the fully qualified name of the desired class
 73  
      * @param pathToDriver the path to the JAR file containing the driver; note, this can be a semi-colon separated list
 74  
      * of paths
 75  
      * @return the loaded Driver
 76  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 77  
      */
 78  
     public static Driver load(String className, String pathToDriver) throws DriverLoadException {
 79  4
         final URLClassLoader parent = (URLClassLoader) ClassLoader.getSystemClassLoader();
 80  4
         final List<URL> urls = new ArrayList<URL>();
 81  4
         final String[] paths = pathToDriver.split(File.pathSeparator);
 82  9
         for (String path : paths) {
 83  5
             final File file = new File(path);
 84  5
             if (file.isDirectory()) {
 85  2
                 final File[] files = file.listFiles();
 86  
 
 87  37
                 for (File f : files) {
 88  
                     try {
 89  35
                         urls.add(f.toURI().toURL());
 90  0
                     } catch (MalformedURLException ex) {
 91  0
                         LOGGER.debug("Unable to load database driver '{}'; invalid path provided '{}'",
 92  
                             className, f.getAbsoluteFile(), ex);
 93  0
                         throw new DriverLoadException("Unable to load database driver. Invalid path provided", ex);
 94  35
                     }
 95  
                 }
 96  2
             } else if (file.exists()) {
 97  
                 try {
 98  2
                     urls.add(file.toURI().toURL());
 99  0
                 } catch (MalformedURLException ex) {
 100  0
                     LOGGER.debug("Unable to load database driver '{}'; invalid path provided '{}'",
 101  
                         className, file.getAbsoluteFile(), ex);
 102  0
                     throw new DriverLoadException("Unable to load database driver. Invalid path provided", ex);
 103  2
                 }
 104  
             }
 105  
         }
 106  8
         final URLClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
 107  
             @Override
 108  
             public URLClassLoader run() {
 109  4
                 return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
 110  
             }
 111  
         });
 112  
 
 113  4
         return load(className, loader);
 114  
     }
 115  
 
 116  
     /**
 117  
      * Loads the specified class using the supplied class loader and registers the driver with the driver manager.
 118  
      *
 119  
      * @param className the fully qualified name of the desired class
 120  
      * @param loader the class loader to use when loading the driver
 121  
      * @return the loaded Driver
 122  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 123  
      */
 124  
     private static Driver load(String className, ClassLoader loader) throws DriverLoadException {
 125  
         try {
 126  7
             final Class c = Class.forName(className, true, loader);
 127  
             //final Class c = loader.loadClass(className);
 128  4
             final Driver driver = (Driver) c.newInstance();
 129  4
             final Driver shim = new DriverShim(driver);
 130  
             //using the DriverShim to get around the fact that the DriverManager won't register a driver not in the base class path
 131  4
             DriverManager.registerDriver(shim);
 132  4
             return shim;
 133  3
         } catch (ClassNotFoundException ex) {
 134  3
             final String msg = String.format("Unable to load database driver '%s'", className);
 135  3
             LOGGER.debug(msg, ex);
 136  3
             throw new DriverLoadException(msg, ex);
 137  0
         } catch (InstantiationException ex) {
 138  0
             final String msg = String.format("Unable to load database driver '%s'", className);
 139  0
             LOGGER.debug(msg, ex);
 140  0
             throw new DriverLoadException(msg, ex);
 141  0
         } catch (IllegalAccessException ex) {
 142  0
             final String msg = String.format("Unable to load database driver '%s'", className);
 143  0
             LOGGER.debug(msg, ex);
 144  0
             throw new DriverLoadException(msg, ex);
 145  0
         } catch (SQLException ex) {
 146  0
             final String msg = String.format("Unable to load database driver '%s'", className);
 147  0
             LOGGER.debug(msg, ex);
 148  0
             throw new DriverLoadException(msg, ex);
 149  
         }
 150  
     }
 151  
 }