Coverage Report - org.owasp.dependencycheck.data.nvdcve.DriverLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
DriverLoader
56%
26/46
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 java.io.File;
 21  
 import java.net.MalformedURLException;
 22  
 import java.net.URL;
 23  
 import java.net.URLClassLoader;
 24  
 import java.security.AccessController;
 25  
 import java.security.PrivilegedAction;
 26  
 import java.sql.Driver;
 27  
 import java.sql.DriverManager;
 28  
 import java.sql.SQLException;
 29  
 import java.util.ArrayList;
 30  
 import java.util.logging.Level;
 31  
 import java.util.logging.Logger;
 32  
 
 33  
 /**
 34  
  * DriverLoader is a utility class that is used to load database drivers.
 35  
  *
 36  
  * @author Jeremy Long <jeremy.long@owasp.org>
 37  
  */
 38  
 public final class DriverLoader {
 39  
 
 40  
     /**
 41  
      * Private constructor for a utility class.
 42  
      */
 43  
     private DriverLoader() {
 44  
     }
 45  
 
 46  
     /**
 47  
      * Loads the specified class using the system class loader and registers the driver with the driver manager.
 48  
      *
 49  
      * @param className the fully qualified name of the desired class
 50  
      * @return the loaded Driver
 51  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 52  
      */
 53  
     public static Driver load(String className) throws DriverLoadException {
 54  8
         final ClassLoader loader = DriverLoader.class.getClassLoader(); //ClassLoader.getSystemClassLoader();
 55  8
         return load(className, loader);
 56  
     }
 57  
 
 58  
     /**
 59  
      * Loads the specified class by registering the supplied paths to the class loader and then registers the driver
 60  
      * with the driver manager. The pathToDriver argument is added to the class loader so that an external driver can be
 61  
      * loaded. Note, the pathTodriver can contain a semi-colon separated list of paths so any dependencies can be added
 62  
      * as needed. If a path in the pathToDriver argument is a directory all files in the directory are added to the
 63  
      * class path.
 64  
      *
 65  
      * @param className the fully qualified name of the desired class
 66  
      * @param pathToDriver the path to the JAR file containing the driver; note, this can be a semi-colon separated list
 67  
      * of paths
 68  
      * @return the loaded Driver
 69  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 70  
      */
 71  
     public static Driver load(String className, String pathToDriver) throws DriverLoadException {
 72  4
         final URLClassLoader parent = (URLClassLoader) ClassLoader.getSystemClassLoader();
 73  4
         final ArrayList<URL> urls = new ArrayList<URL>();
 74  4
         final String[] paths = pathToDriver.split(File.pathSeparator);
 75  9
         for (String path : paths) {
 76  5
             final File file = new File(path);
 77  5
             if (file.isDirectory()) {
 78  2
                 final File[] files = file.listFiles();
 79  
 
 80  27
                 for (File f : files) {
 81  
                     try {
 82  25
                         urls.add(f.toURI().toURL());
 83  0
                     } catch (MalformedURLException ex) {
 84  0
                         final String msg = String.format("Unable to load database driver '%s'; invalid path provided '%s'",
 85  
                                 className, f.getAbsoluteFile());
 86  0
                         Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 87  0
                         throw new DriverLoadException(msg, ex);
 88  25
                     }
 89  
                 }
 90  2
             } else if (file.exists()) {
 91  
                 try {
 92  2
                     urls.add(file.toURI().toURL());
 93  0
                 } catch (MalformedURLException ex) {
 94  0
                     final String msg = String.format("Unable to load database driver '%s'; invalid path provided '%s'",
 95  
                             className, file.getAbsoluteFile());
 96  0
                     Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 97  0
                     throw new DriverLoadException(msg, ex);
 98  2
                 }
 99  
             }
 100  
         }
 101  4
         final URLClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
 102  
             @Override
 103  
             public URLClassLoader run() {
 104  4
                 return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
 105  
             }
 106  
         });
 107  
 
 108  4
         return load(className, loader);
 109  
     }
 110  
 
 111  
     /**
 112  
      * Loads the specified class using the supplied class loader and registers the driver with the driver manager.
 113  
      *
 114  
      * @param className the fully qualified name of the desired class
 115  
      * @param loader the class loader to use when loading the driver
 116  
      * @return the loaded Driver
 117  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 118  
      */
 119  
     private static Driver load(String className, ClassLoader loader) throws DriverLoadException {
 120  
         try {
 121  12
             final Class c = Class.forName(className, true, loader);
 122  
             //final Class c = loader.loadClass(className);
 123  9
             final Driver driver = (Driver) c.newInstance();
 124  9
             final Driver shim = new DriverShim(driver);
 125  
             //using the DriverShim to get around the fact that the DriverManager won't register a driver not in the base class path
 126  9
             DriverManager.registerDriver(shim);
 127  9
             return shim;
 128  3
         } catch (ClassNotFoundException ex) {
 129  3
             final String msg = String.format("Unable to load database driver '%s'", className);
 130  3
             Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 131  3
             throw new DriverLoadException(msg, ex);
 132  0
         } catch (InstantiationException ex) {
 133  0
             final String msg = String.format("Unable to load database driver '%s'", className);
 134  0
             Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 135  0
             throw new DriverLoadException(msg, ex);
 136  0
         } catch (IllegalAccessException ex) {
 137  0
             final String msg = String.format("Unable to load database driver '%s'", className);
 138  0
             Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 139  0
             throw new DriverLoadException(msg, ex);
 140  0
         } catch (SQLException ex) {
 141  0
             final String msg = String.format("Unable to load database driver '%s'", className);
 142  0
             Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 143  0
             throw new DriverLoadException(msg, ex);
 144  
         }
 145  
     }
 146  
 }