1 16 package scriptella.core; 17 18 import scriptella.jdbc.GenericDriver; 19 import scriptella.spi.ScriptellaDriver; 20 21 import java.sql.Driver ; 22 23 29 public final class DriverFactory { 30 private DriverFactory() { 32 } 33 34 50 public static ScriptellaDriver getDriver(String driverName, ClassLoader loader) throws ClassNotFoundException { 51 try { 53 return getDriver(Class.forName(driverName, true, loader)); 54 } catch (ClassNotFoundException e) { 55 String fullName = "scriptella.driver."+driverName+".Driver"; 57 try { 58 return getDriver(Class.forName(fullName, true, loader)); 59 } catch (ClassNotFoundException ignoredException) { 60 throw e; } 62 63 } 64 65 66 } 67 68 77 @SuppressWarnings ("unchecked") 78 public static ScriptellaDriver getDriver(Class drvClass) { 79 if (Driver.class.isAssignableFrom(drvClass)) { 80 try { 82 83 ClassLoader drvClassLoader = drvClass.getClassLoader(); 84 if (drvClassLoader==null) { drvClassLoader=DriverFactory.class.getClassLoader(); 87 } 88 Class <?> cl = Class.forName("scriptella.jdbc.GenericDriver", true, drvClassLoader); 89 return (ScriptellaDriver)cl.newInstance(); 90 } catch (Exception e) { 91 throw new IllegalStateException ("Unable to instantiate JDBC driver for " + drvClass, e); 92 93 } 94 } else if (ScriptellaDriver.class.isAssignableFrom(drvClass)) { 95 try { 96 return (ScriptellaDriver) drvClass.newInstance(); 97 } catch (Exception e) { 98 throw new IllegalStateException ("Unable to instantiate driver for " + drvClass, e); 99 } 100 101 } else { 102 throw new IllegalArgumentException ("Class " + drvClass + " is not a scriptella compatible driver."); 103 } 104 105 106 } 107 } 108 | Popular Tags |