piątek, stycznia 13, 2012

Użycie wskazanego drivera Oracle metodą brute force

LinkedList<URL> cpList = new LinkedList<URL>();
try {
    cpList.add(new URL("jar:file:/ojdbc_11_2_5.jar!/");
}
catch (MalformedURLException e) {
    throw new RuntimeException(e);
}
URLClassLoader parent = (URLClassLoader) this.getClass()
.getClassLoader();

for (URL url : parent.getURLs()) {
    String su = url.toString();
    if (!(su.contains("ojdbc")))
        cpList.add(url);
}

Logger.getInstance().debug(
"Using url "+cpList.get(0)+" for loading Oracle driver");

URLClassLoader cl = new URLClassLoader(
cpList.toArray(new URL[0]), null);
ClassLoader savedClassLoader = Thread.currentThread()
.getContextClassLoader();

// because of 'javax.management.InstanceAlreadyExistsException
// com.oracle.jdbc:type=diagnosability,
// name=sun.misc.Launcher$AppClassLoader@92e78c'
Thread.currentThread().setContextClassLoader(cl); 
Driver drv = (Driver) cl.loadClass(
"oracle.jdbc.OracleDriver").newInstance();
Thread.currentThread().setContextClassLoader(savedClassLoader);

0 komentarze: