1 22 package org.jboss.resource.adapter.jdbc.local; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.sql.Connection ; 28 import java.sql.Driver ; 29 import java.sql.DriverManager ; 30 import java.util.Iterator ; 31 import java.util.Properties ; 32 import java.util.Set ; 33 34 import javax.resource.ResourceException ; 35 import javax.resource.spi.ConnectionRequestInfo ; 36 import javax.resource.spi.ManagedConnection ; 37 import javax.security.auth.Subject ; 38 39 import org.jboss.resource.JBossResourceException; 40 import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory; 41 import org.jboss.util.NestedRuntimeException; 42 43 50 public class LocalManagedConnectionFactory extends BaseWrapperManagedConnectionFactory 51 { 52 static final long serialVersionUID = 4698955390505160469L; 53 54 private String driverClass; 55 56 private transient Driver driver; 57 58 private String connectionURL; 59 60 protected String connectionProperties; 61 62 public LocalManagedConnectionFactory() 63 { 64 65 } 66 67 72 public String getConnectionURL() 73 { 74 return connectionURL; 75 } 76 77 82 public void setConnectionURL(final String connectionURL) 83 { 84 this.connectionURL = connectionURL; 85 } 86 87 92 public String getDriverClass() 93 { 94 return driverClass; 95 } 96 97 102 public synchronized void setDriverClass(final String driverClass) 103 { 104 this.driverClass = driverClass; 105 driver = null; 106 } 107 108 113 public String getConnectionProperties() 114 { 115 return connectionProperties; 116 } 117 118 123 public void setConnectionProperties(String connectionProperties) 124 { 125 this.connectionProperties = connectionProperties; 126 connectionProps.clear(); 127 if (connectionProperties != null) 128 { 129 connectionProperties = connectionProperties.replaceAll("\\\\", "\\\\\\\\"); 131 132 InputStream is = new ByteArrayInputStream (connectionProperties.getBytes()); 133 try 134 { 135 connectionProps.load(is); 136 } 137 catch (IOException ioe) 138 { 139 throw new NestedRuntimeException("Could not load connection properties", ioe); 140 } 141 } 142 } 143 144 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cri) 145 throws javax.resource.ResourceException 146 { 147 Properties props = getConnectionProperties(subject, cri); 148 Properties copy = (Properties ) props.clone(); 152 boolean trace = log.isTraceEnabled(); 153 if (trace) 154 { 155 Properties logCopy = copy; 157 if (copy.getProperty("password") != null) 158 { 159 logCopy = (Properties ) props.clone(); 160 logCopy.setProperty("password", "--hidden--"); 161 } 162 log.trace("Using properties: " + logCopy); 163 } 164 165 try 166 { 167 String url = getConnectionURL(); 168 Driver d = getDriver(url); 169 Connection con = d.connect(url, copy); 170 if (con == null) 171 throw new JBossResourceException("Wrong driver class for this connection URL"); 172 173 return new LocalManagedConnection(this, con, props, transactionIsolation, preparedStatementCacheSize); 174 } 175 catch (Exception e) 176 { 177 throw new JBossResourceException("Could not create connection", e); 178 } 179 } 180 181 public ManagedConnection matchManagedConnections(final Set mcs, final Subject subject, 182 final ConnectionRequestInfo cri) throws ResourceException 183 { 184 Properties newProps = getConnectionProperties(subject, cri); 185 186 for (Iterator i = mcs.iterator(); i.hasNext();) 187 { 188 Object o = i.next(); 189 190 if (o instanceof LocalManagedConnection) 191 { 192 LocalManagedConnection mc = (LocalManagedConnection) o; 193 194 if (mc.getProperties().equals(newProps)) 196 { 197 if ((getValidateOnMatch() && mc.checkValid()) || !getValidateOnMatch()) 199 { 200 201 return mc; 202 203 } 204 205 } 206 } 207 } 208 209 return null; 210 } 211 212 public int hashCode() 213 { 214 int result = 17; 215 result = result * 37 + ((connectionURL == null) ? 0 : connectionURL.hashCode()); 216 result = result * 37 + ((driverClass == null) ? 0 : driverClass.hashCode()); 217 result = result * 37 + ((userName == null) ? 0 : userName.hashCode()); 218 result = result * 37 + ((password == null) ? 0 : password.hashCode()); 219 result = result * 37 + transactionIsolation; 220 return result; 221 } 222 223 public boolean equals(Object other) 224 { 225 if (this == other) 226 return true; 227 if (getClass() != other.getClass()) 228 return false; 229 LocalManagedConnectionFactory otherMcf = (LocalManagedConnectionFactory) other; 230 return this.connectionURL.equals(otherMcf.connectionURL) && this.driverClass.equals(otherMcf.driverClass) 231 && ((this.userName == null) ? otherMcf.userName == null : this.userName.equals(otherMcf.userName)) 232 && ((this.password == null) ? otherMcf.password == null : this.password.equals(otherMcf.password)) 233 && this.transactionIsolation == otherMcf.transactionIsolation; 234 235 } 236 237 243 protected synchronized Driver getDriver(final String url) throws ResourceException 244 { 245 boolean trace = log.isTraceEnabled(); 246 247 if (driver != null) 249 { 250 return driver; 251 } 252 if (trace) 253 log.trace("Checking driver for URL: " + url); 254 255 if (driverClass == null) 256 { 257 throw new JBossResourceException("No Driver class specified!"); 258 } 259 260 262 if (isDriverLoadedForURL(url)) 263 { 264 return driver; 265 } 267 try 268 { 269 Class clazz = Class.forName(driverClass, true, Thread.currentThread().getContextClassLoader()); 271 if (isDriverLoadedForURL(url)) 272 return driver; 274 275 driver = (Driver ) clazz.newInstance(); 278 DriverManager.registerDriver(driver); 279 if (isDriverLoadedForURL(url)) 280 return driver; 281 } 283 catch (Exception e) 284 { 285 throw new JBossResourceException("Failed to register driver for: " + driverClass, e); 286 } 287 288 throw new JBossResourceException("Apparently wrong driver class specified for URL: class: " + driverClass 289 + ", url: " + url); 290 } 291 292 private boolean isDriverLoadedForURL(String url) 293 { 294 boolean trace = log.isTraceEnabled(); 295 296 try 297 { 298 driver = DriverManager.getDriver(url); 299 if (trace) 300 log.trace("Driver already registered for url: " + url); 301 return true; 302 } 303 catch (Exception e) 304 { 305 if (trace) 306 log.trace("Driver not yet registered for url: " + url); 307 return false; 308 } 309 } 310 311 protected String internalGetConnectionURL() 312 { 313 return connectionURL; 314 } 315 } 316 | Popular Tags |