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