| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public final class DriverLoader { |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 1 | private static final Logger LOGGER = Logger.getLogger(DriverLoader.class.getName()); |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
private DriverLoader() { |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public static Driver load(String className) throws DriverLoadException { |
| 59 | 3 | final ClassLoader loader = DriverLoader.class.getClassLoader(); |
| 60 | 3 | return load(className, loader); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public static Driver load(String className, String pathToDriver) throws DriverLoadException { |
| 77 | 4 | final URLClassLoader parent = (URLClassLoader) ClassLoader.getSystemClassLoader(); |
| 78 | 4 | final ArrayList<URL> urls = new ArrayList<URL>(); |
| 79 | 4 | final String[] paths = pathToDriver.split(File.pathSeparator); |
| 80 | 9 | for (String path : paths) { |
| 81 | 5 | final File file = new File(path); |
| 82 | 5 | if (file.isDirectory()) { |
| 83 | 2 | final File[] files = file.listFiles(); |
| 84 | |
|
| 85 | 29 | for (File f : files) { |
| 86 | |
try { |
| 87 | 27 | urls.add(f.toURI().toURL()); |
| 88 | 0 | } catch (MalformedURLException ex) { |
| 89 | 0 | final String msg = String.format("Unable to load database driver '%s'; invalid path provided '%s'", |
| 90 | |
className, f.getAbsoluteFile()); |
| 91 | 0 | LOGGER.log(Level.FINE, msg, ex); |
| 92 | 0 | throw new DriverLoadException(msg, ex); |
| 93 | 27 | } |
| 94 | |
} |
| 95 | 2 | } else if (file.exists()) { |
| 96 | |
try { |
| 97 | 2 | urls.add(file.toURI().toURL()); |
| 98 | 0 | } catch (MalformedURLException ex) { |
| 99 | 0 | final String msg = String.format("Unable to load database driver '%s'; invalid path provided '%s'", |
| 100 | |
className, file.getAbsoluteFile()); |
| 101 | 0 | LOGGER.log(Level.FINE, msg, ex); |
| 102 | 0 | throw new DriverLoadException(msg, ex); |
| 103 | 2 | } |
| 104 | |
} |
| 105 | |
} |
| 106 | 4 | 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 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 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 | |
|
| 128 | 4 | final Driver driver = (Driver) c.newInstance(); |
| 129 | 4 | final Driver shim = new DriverShim(driver); |
| 130 | |
|
| 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.log(Level.FINE, 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.log(Level.FINE, 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.log(Level.FINE, 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.log(Level.FINE, msg, ex); |
| 148 | 0 | throw new DriverLoadException(msg, ex); |
| 149 | |
} |
| 150 | |
} |
| 151 | |
} |