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  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.data.nvdcve;
 20  
 
 21  
 import java.io.File;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.net.URLClassLoader;
 25  
 import java.security.AccessController;
 26  
 import java.security.PrivilegedAction;
 27  
 import java.sql.Driver;
 28  
 import java.sql.DriverManager;
 29  
 import java.sql.SQLException;
 30  
 import java.util.ArrayList;
 31  
 import java.util.logging.Level;
 32  
 import java.util.logging.Logger;
 33  
 
 34  
 /**
 35  
  * DriverLoader is a utility class that is used to load database drivers.
 36  
  *
 37  
  * @author Jeremy Long <jeremy.long@owasp.org>
 38  
  */
 39  
 public final class DriverLoader {
 40  
 
 41  
     /**
 42  
      * Private constructor for a utility class.
 43  
      */
 44  0
     private DriverLoader() {
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Loads the specified class using the system class loader and registers the driver with the driver manager.
 49  
      *
 50  
      * @param className the fully qualified name of the desired class
 51  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 52  
      */
 53  
     public static void load(String className) throws DriverLoadException {
 54  2
         final ClassLoader loader = ClassLoader.getSystemClassLoader();
 55  2
         load(className, loader);
 56  1
     }
 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  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 69  
      */
 70  
     public static void load(String className, String pathToDriver) throws DriverLoadException {
 71  4
         final URLClassLoader parent = (URLClassLoader) ClassLoader.getSystemClassLoader();
 72  4
         final ArrayList<URL> urls = new ArrayList<URL>();
 73  4
         final String[] paths = pathToDriver.split(File.pathSeparator);
 74  9
         for (String path : paths) {
 75  5
             final File file = new File(path);
 76  5
             if (file.isDirectory()) {
 77  2
                 final File[] files = file.listFiles();
 78  
 
 79  18
                 for (File f : files) {
 80  
                     try {
 81  16
                         urls.add(f.toURI().toURL());
 82  0
                     } catch (MalformedURLException ex) {
 83  0
                         final String msg = String.format("Unable to load database driver '%s'; invalid path provided '%s'",
 84  
                                 className, f.getAbsoluteFile());
 85  0
                         Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 86  0
                         throw new DriverLoadException(msg, ex);
 87  16
                     }
 88  
                 }
 89  2
             } else if (file.exists()) {
 90  
                 try {
 91  2
                     urls.add(file.toURI().toURL());
 92  0
                 } catch (MalformedURLException ex) {
 93  0
                     final String msg = String.format("Unable to load database driver '%s'; invalid path provided '%s'",
 94  
                             className, file.getAbsoluteFile());
 95  0
                     Logger.getLogger(DriverLoader.class.getName()).log(Level.FINE, msg, ex);
 96  0
                     throw new DriverLoadException(msg, ex);
 97  2
                 }
 98  
             }
 99  
         }
 100  8
         final URLClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
 101  
             @Override
 102  
             public URLClassLoader run() {
 103  4
                 return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
 104  
             }
 105  
         });
 106  
 
 107  4
         load(className, loader);
 108  2
     }
 109  
 
 110  
     /**
 111  
      * Loads the specified class using the supplied class loader and registers the driver with the driver manager.
 112  
      *
 113  
      * @param className the fully qualified name of the desired class
 114  
      * @param loader the class loader to use when loading the driver
 115  
      * @throws DriverLoadException thrown if the driver cannot be loaded
 116  
      */
 117  
     private static void load(String className, ClassLoader loader) throws DriverLoadException {
 118  
         try {
 119  6
             final Class c = loader.loadClass(className);
 120  3
             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  3
             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  3
         }
 140  3
     }
 141  
 }