1 23 24 package com.sun.gjc.spi; 25 26 import javax.resource.ResourceException ; 27 import javax.resource.spi.ConnectionRequestInfo ; 28 import com.sun.gjc.util.SecurityUtils; 29 import javax.resource.spi.security.PasswordCredential ; 30 import java.sql.DriverManager ; 31 import com.sun.gjc.spi.ManagedConnectionFactory; 32 import com.sun.gjc.common.DataSourceSpec; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 import java.util.NoSuchElementException ; 36 import com.sun.logging.*; 37 import java.util.logging.Logger ; 38 import java.util.logging.Level ; 39 40 46 47 public class DMManagedConnectionFactory extends ManagedConnectionFactory { 48 49 Properties props; 50 51 private static Logger _logger; 52 static { 53 _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER ); 54 } 55 private boolean debug = false; 56 57 74 public javax.resource.spi.ManagedConnection createManagedConnection(javax.security.auth.Subject subject, 75 ConnectionRequestInfo cxRequestInfo) throws ResourceException { 76 if(logWriter != null) { 77 logWriter.println("In createManagedConnection"); 78 } 79 80 PasswordCredential pc = SecurityUtils.getPasswordCredential(this, subject, cxRequestInfo); 81 82 try { 83 Thread.currentThread().getContextClassLoader().loadClass(spec.getDetail(DataSourceSpec.CLASSNAME)); 84 } catch(ClassNotFoundException cnfe) { 85 _logger.log(Level.SEVERE, "jdbc.exc_cnfe"); 86 throw new ResourceException ("The driver could not be loaded: " + cnfe.getMessage()); 87 } 88 89 java.sql.Connection dsConn = null; 90 91 Properties driverProps = getPropertiesObj(); 92 93 try { 94 if(cxRequestInfo != null) { 95 driverProps.setProperty("user", pc.getUserName()); 96 driverProps.setProperty("password", new String (pc.getPassword())); 97 } 98 99 dsConn = DriverManager.getConnection(spec.getDetail(DataSourceSpec.URL), driverProps); 100 101 } catch(java.sql.SQLException sqle) { 102 _logger.log(Level.SEVERE, "jdbc.exc_create_mc", sqle); 103 throw new javax.resource.spi.ResourceAllocationException ("The connection could not be allocated: " + 104 sqle.getMessage()); 105 } 106 107 com.sun.gjc.spi.ManagedConnection mc = constructManagedConnection( 108 null, dsConn, pc, this); 109 110 setIsolation(mc); 112 isValid(mc); 113 return mc; 114 } 115 116 126 private Properties getPropertiesObj() throws ResourceException { 127 if(props != null) { 128 return props; 129 } 130 131 props = new Properties (); 132 props.setProperty("user", getuser()); 133 props.setProperty("password", getpassword()); 134 135 String driverProps = spec.getDetail(DataSourceSpec.DRIVERPROPERTIES); 136 String delimiter = spec.getDetail(DataSourceSpec.DELIMITER); 137 138 if(driverProps != null && driverProps.trim().equals("") == false) { 139 if(delimiter == null || delimiter.equals("")) { 140 throw new ResourceException ("Invalid driver properties string - " + 141 "delimiter not properly set!!"); 142 } 143 144 StringTokenizer st = new StringTokenizer (driverProps, delimiter); 145 while (st.hasMoreTokens()) { 146 String keyValuePair = null; 147 try { 148 keyValuePair = st.nextToken(); 149 } catch(NoSuchElementException nsee) { 150 throw new ResourceException ("Invalid driver properties string - " + 151 "Key value pair not available: " + nsee.getMessage()); 152 } 153 154 int indexOfEqualsSign = -1; 155 try { 156 indexOfEqualsSign = keyValuePair.indexOf("="); 157 if(indexOfEqualsSign == -1) { 158 throw new ResourceException ("Invalid driver properties string - " + 159 "Key value pair should be of the form key = value"); 160 } 161 } catch(NullPointerException npe) { 162 if (debug) { 163 _logger.log(Level.FINE, "jdbc.exc_caught_ign", npe.getMessage() ); 164 } 165 166 } 167 168 String key = null; 169 try { 170 key = keyValuePair.substring(0, indexOfEqualsSign).trim(); 171 } catch(IndexOutOfBoundsException iobe) { 172 if (debug) { 173 _logger.log(Level.FINE, "jdbc.exc_caught_ign", iobe.getMessage() ); 174 } 175 } 176 if(key.equals("")) { 177 throw new ResourceException ("Invalid driver properties string - " + 178 "Key cannot be an empty string"); 179 } 180 181 String value = null; 182 try { 183 value = keyValuePair.substring(indexOfEqualsSign+1).trim(); 184 } catch(IndexOutOfBoundsException iobe) { 185 if (debug) { 186 _logger.log(Level.FINE, "jdbc.exc_caught_ign", iobe.getMessage() ); 187 } 188 } 189 190 props.setProperty(key, value); 191 } 192 } 193 194 return props; 195 } 196 197 206 public boolean equals(Object other) { 207 if(logWriter != null) { 208 logWriter.println("In equals"); 209 } 210 211 215 if(other instanceof com.sun.gjc.spi.DMManagedConnectionFactory) { 216 com.sun.gjc.spi.DMManagedConnectionFactory otherMCF = 217 (com.sun.gjc.spi.DMManagedConnectionFactory) other; 218 return this.spec.equals(otherMCF.spec); 219 } 220 return false; 221 } 222 223 229 public void setloginTimeOut(String loginTimeOut) { 230 int timeOut = 0; 231 try { 232 timeOut = Integer.valueOf(loginTimeOut).intValue(); 233 DriverManager.setLoginTimeout(timeOut); 234 spec.setDetail(DataSourceSpec.LOGINTIMEOUT, loginTimeOut); 235 } catch(Exception e) { 236 if (debug) { 237 _logger.log(Level.FINE, "jdbc.exc_caught_ign", e.getMessage() ); 238 } 239 } 240 } 241 242 248 public String getloginTimeOut() { 249 return spec.getDetail(DataSourceSpec.LOGINTIMEOUT); 250 } 251 252 258 public void setLoginTimeOut(String loginTimeOut) { 259 int timeOut = 0; 260 try { 261 timeOut = Integer.valueOf(loginTimeOut).intValue(); 262 DriverManager.setLoginTimeout(timeOut); 263 spec.setDetail(DataSourceSpec.LOGINTIMEOUT, loginTimeOut); 264 } catch(Exception e) { 265 if (debug) { 266 _logger.log(Level.FINE, "jdbc.exc_caught_ign", e.getMessage() ); 267 } 268 } 269 } 270 271 277 public String getLoginTimeOut() { 278 return spec.getDetail(DataSourceSpec.LOGINTIMEOUT); 279 } 280 281 287 public void setconnectionURL(String url) { 288 spec.setDetail(DataSourceSpec.URL, url); 289 } 290 291 297 public String getconnectionURL() { 298 return spec.getDetail(DataSourceSpec.URL); 299 } 300 301 307 public void setConnectionURL(String url) { 308 spec.setDetail(DataSourceSpec.URL, url); 309 } 310 311 317 public String getConnectionURL() { 318 return spec.getDetail(DataSourceSpec.URL); 319 } 320 321 327 public void setclassName(String className) { 328 spec.setDetail(DataSourceSpec.CLASSNAME, className); 329 } 330 331 337 public String getclassName() { 338 return spec.getDetail(DataSourceSpec.CLASSNAME); 339 } 340 341 347 public void setClassName(String className) { 348 spec.setDetail(DataSourceSpec.CLASSNAME, className); 349 } 350 351 357 public String getClassName() { 358 return spec.getDetail(DataSourceSpec.CLASSNAME); 359 } 360 361 367 public void setdriverProperties(String driverProps) { 368 spec.setDetail(DataSourceSpec.DRIVERPROPERTIES, driverProps); 369 } 370 371 377 public String getdriverProperties() { 378 return spec.getDetail(DataSourceSpec.DRIVERPROPERTIES); 379 } 380 381 387 public void setDriverProperties(String driverProps) { 388 spec.setDetail(DataSourceSpec.DRIVERPROPERTIES, driverProps); 389 } 390 391 397 public String getDriverProperties() { 398 return spec.getDetail(DataSourceSpec.DRIVERPROPERTIES); 399 } 400 401 408 public void setdelimiter(String delim) { 409 spec.setDetail(DataSourceSpec.DELIMITER, delim); 410 } 411 412 418 public String getdelimiter() { 419 return spec.getDetail(DataSourceSpec.DELIMITER); 420 } 421 422 429 public void setDelimiter(String delim) { 430 spec.setDetail(DataSourceSpec.DELIMITER, delim); 431 } 432 433 439 public String getDelimiter() { 440 return spec.getDetail(DataSourceSpec.DELIMITER); 441 } 442 } 443 | Popular Tags |