01: package liquibase.test;
02:
03: import java.io.File;
04: import java.io.FileFilter;
05: import java.io.FilenameFilter;
06: import java.net.URI;
07: import java.net.URL;
08: import java.net.URLClassLoader;
09: import java.util.ArrayList;
10: import java.util.List;
11:
12: /**
13: * Class Loader for loading JDBC drivers in unit tests. It was orginally not a singleton, but
14: * would instead take the directory of the particular driver you wanted and create a new
15: * class loader with just those jar files. Unfortunatley, the class loaders were never cleaned up by
16: * the JVM even though there were no references to them and the permgen space requirements would skyrocket.
17: * It was re-implemented as a singleton to solve that problem. If we ever need to make different unit tests that use
18: * the same driver class name but different jars (versions) we will need to re-address the issue.
19: */
20: public class JUnitJDBCDriverClassLoader extends URLClassLoader {
21:
22: private static final JUnitJDBCDriverClassLoader instance = new JUnitJDBCDriverClassLoader();
23:
24: private JUnitJDBCDriverClassLoader() {
25: super (getDriverClasspath());
26: }
27:
28: public static JUnitJDBCDriverClassLoader getInstance() {
29: return instance;
30: }
31:
32: private static URL[] getDriverClasspath() {
33: try {
34: List<URL> urls = new ArrayList<URL>();
35:
36: File this ClassFile = new File(
37: new URI(
38: Thread
39: .currentThread()
40: .getContextClassLoader()
41: .getResource(
42: "liquibase/test/JUnitJDBCDriverClassLoader.class")
43: .toExternalForm()));
44: File jdbcLib = new File(this ClassFile.getParentFile()
45: .getParentFile().getParentFile().getParent(),
46: "lib-jdbc");
47:
48: for (File driverDir : jdbcLib.listFiles(new FileFilter() {
49: public boolean accept(File pathname) {
50: return pathname.isDirectory();
51: }
52: })) {
53: File[] driverJars = driverDir
54: .listFiles(new FilenameFilter() {
55: public boolean accept(File dir, String name) {
56: return name.endsWith("jar");
57: }
58: });
59:
60: for (File jar : driverJars) {
61: urls.add(jar.toURL());
62: }
63:
64: }
65: return urls.toArray(new URL[urls.size()]);
66: } catch (Exception e) {
67: throw new RuntimeException(e);
68: }
69: }
70: }
|