1 21 package net.sf.hajdbc.sql; 22 23 import java.sql.Connection ; 24 import java.sql.Driver ; 25 import java.sql.DriverManager ; 26 import java.util.Properties ; 27 28 import net.sf.hajdbc.ActiveDatabaseMBean; 29 import net.sf.hajdbc.InactiveDatabaseMBean; 30 import net.sf.hajdbc.Messages; 31 32 37 public class DriverDatabase extends AbstractDatabase<Driver> implements InactiveDriverDatabaseMBean 38 { 39 private static final String USER = "user"; 40 private static final String PASSWORD = "password"; 41 42 private String url; 43 private Class <? extends Driver> driverClass; 44 45 48 public String getUrl() 49 { 50 return this.url; 51 } 52 53 56 public void setUrl(String url) 57 { 58 this.getDriver(url); 59 this.checkDirty(this.url, url); 60 this.url = url; 61 } 62 63 66 public String getDriver() 67 { 68 return (this.driverClass != null) ? this.driverClass.getName() : null; 69 } 70 71 76 public void setDriver(String driver) 77 { 78 try 79 { 80 Class <? extends Driver> driverClass = null; 81 82 if ((driver != null) && (driver.length() > 0)) 83 { 84 driverClass = Class.forName(driver).asSubclass(Driver.class); 85 } 86 87 this.checkDirty(this.driverClass, driverClass); 88 this.driverClass = driverClass; 89 } 90 catch (ClassNotFoundException e) 91 { 92 throw new IllegalArgumentException (e); 93 } 94 catch (ClassCastException e) 95 { 96 throw new IllegalArgumentException (e); 97 } 98 } 99 100 106 public Connection connect(Driver driver) throws java.sql.SQLException 107 { 108 Properties properties = new Properties (this.getProperties()); 109 110 if (this.user != null) 111 { 112 properties.setProperty(USER, this.user); 113 } 114 115 if (this.password != null) 116 { 117 properties.setProperty(PASSWORD, this.password); 118 } 119 120 return driver.connect(this.url, properties); 121 } 122 123 126 public Driver createConnectionFactory() 127 { 128 return this.getDriver(this.url); 129 } 130 131 private Driver getDriver(String url) 132 { 133 try 134 { 135 return DriverManager.getDriver(url); 136 } 137 catch (java.sql.SQLException e) 138 { 139 throw new IllegalArgumentException (Messages.getMessage(Messages.JDBC_URL_REJECTED, url), e); 140 } 141 } 142 143 146 public Class <Driver> getConnectionFactoryClass() 147 { 148 return Driver.class; 149 } 150 151 154 public Class <? extends ActiveDatabaseMBean> getActiveMBeanClass() 155 { 156 return ActiveDriverDatabaseMBean.class; 157 } 158 159 162 public Class <? extends InactiveDatabaseMBean> getInactiveMBeanClass() 163 { 164 return InactiveDriverDatabaseMBean.class; 165 } 166 167 static String getClassName(Class <?> targetClass) 168 { 169 return targetClass.getName(); 170 } 171 172 static Class <?> forName(String className) throws ClassNotFoundException 173 { 174 return (className != null) ? Class.forName(className) : null; 175 } 176 } 177 | Popular Tags |