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 org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.io.File;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.security.AccessController;
29 import java.security.PrivilegedAction;
30 import java.sql.Driver;
31 import java.sql.DriverManager;
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import javax.annotation.concurrent.ThreadSafe;
36
37
38
39
40
41
42 @ThreadSafe
43 public final class DriverLoader {
44
45
46
47
48 private static final Logger LOGGER = LoggerFactory.getLogger(DriverLoader.class);
49
50
51
52
53
54
55 public static void cleanup(Driver driver) {
56 try {
57 LOGGER.debug("Begin deregister driver");
58 DriverManager.deregisterDriver(driver);
59 LOGGER.debug("End deregister driver");
60 } catch (SQLException ex) {
61 LOGGER.debug("An error occurred unloading the database driver", ex);
62 } catch (Throwable unexpected) {
63 LOGGER.debug("An unexpected throwable occurred unloading the database driver", unexpected);
64 }
65 }
66
67
68
69
70 private DriverLoader() {
71 }
72
73
74
75
76
77
78
79
80
81 public static Driver load(String className) throws DriverLoadException {
82 final ClassLoader loader = DriverLoader.class.getClassLoader();
83 return load(className, loader);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @SuppressWarnings("StringSplitter")
102 public static Driver load(String className, String pathToDriver) throws DriverLoadException {
103 final ClassLoader parent = ClassLoader.getSystemClassLoader();
104 final List<URL> urls = new ArrayList<>();
105 final String[] paths = pathToDriver.split(File.pathSeparator);
106 for (String path : paths) {
107 final File file = new File(path);
108 if (file.isDirectory()) {
109 final File[] files = file.listFiles();
110 if (files != null) {
111 for (File f : files) {
112 try {
113 urls.add(f.toURI().toURL());
114 } catch (MalformedURLException ex) {
115 LOGGER.debug("Unable to load database driver '{}'; invalid path provided '{}'",
116 className, f.getAbsoluteFile(), ex);
117 throw new DriverLoadException("Unable to load database driver. Invalid path provided", ex);
118 }
119 }
120 }
121 } else if (file.exists()) {
122 try {
123 urls.add(file.toURI().toURL());
124 } catch (MalformedURLException ex) {
125 LOGGER.debug("Unable to load database driver '{}'; invalid path provided '{}'",
126 className, file.getAbsoluteFile(), ex);
127 throw new DriverLoadException("Unable to load database driver. Invalid path provided", ex);
128 }
129 }
130 }
131 final URLClassLoader loader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () ->
132 new URLClassLoader(urls.toArray(new URL[0]), parent));
133
134 return load(className, loader);
135 }
136
137
138
139
140
141
142
143
144
145
146 private static Driver load(String className, ClassLoader loader) throws DriverLoadException {
147 try {
148 final Class<?> c = Class.forName(className, true, loader);
149
150 final Driver driver = (Driver) c.getDeclaredConstructor().newInstance();
151
152
153 final Driver shim = new DriverShim(driver);
154
155 DriverManager.registerDriver(shim);
156 return shim;
157 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException
158 | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException ex) {
159 final String msg = String.format("Unable to load database driver '%s'", className);
160 LOGGER.debug(msg, ex);
161 throw new DriverLoadException(msg, ex);
162 }
163 }
164 }