1 7 package ersatz.resourceadapter; 8 9 import javax.naming.Reference; 10 import java.io.PrintWriter; 11 import java.util.Set; 12 import java.util.Timer; 13 import java.util.Iterator; 14 import java.util.Date; 15 import javax.security.auth.Subject; 16 import javax.resource.ResourceException; 17 import javax.resource.NotSupportedException; 18 import javax.resource.spi.ResourceAllocationException; 19 import javax.resource.spi.ManagedConnectionFactory; 20 import javax.resource.spi.ResourceAdapterAssociation; 21 import javax.resource.spi.ConnectionManager; 22 import javax.resource.spi.ConnectionRequestInfo; 23 import javax.resource.spi.ManagedConnection; 24 import javax.resource.spi.ResourceAdapter; 25 import javax.resource.spi.BootstrapContext; 26 import javax.resource.spi.security.PasswordCredential; 27 import java.io.Serializable; 28 29 35 public class ManagedConnectionFactoryImpl 36 implements ManagedConnectionFactory, ResourceAdapterAssociation, 37 Serializable 38 { 39 Reference reference; 40 public String defaultUserName=""; public String defaultPassword=""; 43 ResourceAdapter ra=null; 45 String ServerName=""; 46 String PortNumber=""; 47 String userName=""; 48 String password=""; 49 String protocol=""; 50 51 public ConnectionManager cm; private ManagedConnectionFactoryImpl mcf; 53 private ConnectionRequestInfoImpl crii; 54 private ResourceAdapterImpl rai=null; private BootstrapContext bootstrapCtx; private boolean raLoaded=false; 57 PrintWriter pw; boolean forceMatchNull; public String res_auth; String mfId; 62 String cName="ManagedConnectionFactoryImpl"; 63 public ManagedConnectionFactoryImpl() 65 { 66 crii = new ConnectionRequestInfoImpl(); 67 pw = null; 68 } 69 70 public Object createConnectionFactory(ConnectionManager connectionmanager) 71 throws ResourceException 72 { 73 cm = connectionmanager; 74 Utility.log(cName+".createConnectionFactory cm="+cm); 75 Object obj; 76 obj = new ConnectionFactoryImpl(this, cm); return obj; 78 } 79 85 public Object createConnectionFactory() 86 throws ResourceException 87 { 88 Utility.log(cName+": error - non-managed detected"); 89 throw new NotSupportedException( 90 "A non-managed two-tier application environment is not supported in J2EE testing."); 91 } 92 public ManagedConnection createManagedConnection(Subject subject, 93 ConnectionRequestInfo CRI) 94 throws ResourceException 95 { 96 ConnectionRequestInfoImpl info = (ConnectionRequestInfoImpl)CRI; 97 Utility.log(cName+".createManagedConnection subject="+subject 98 +", cri="+info); 99 mcf = this; 100 ManagedConnectionImpl mc; 101 if (subject!=null) { 102 try { 106 PasswordCredential pc = Utility.getPasswordCredential(this, subject, info); 107 if (pc==null) { 108 Utility.log(cName 109 +".createManagedConnection PasswordCredential = null, Subject not null."); 110 } else { 111 if (pc.getUserName()==null || pc.getUserName().length()==0) { 112 Utility.log(cName+".createManagedConnection userName=empty"); 113 } else { 114 userName = new String(pc.getUserName()); 115 Utility.log(cName+".createManagedConnection" 116 +" PasswordCredential userName="+userName); 117 password = new String(pc.getPassword()); Utility.log(cName+".createManagedConnection" 119 +" PasswordCredential password="+password); 120 } 121 } 122 } catch (Exception e) { 123 Utility.log(cName+".createManagedConnection getPasswordCredential " 124 +"error: e="+e.toString()); 125 } 126 } 127 128 if (info != null) { 129 String s = info.getUserName(); 134 if (s.length()>0) userName = s; 135 else 136 Utility.log(cName+".createManagedConnection" 137 +" ConnectionRequestInfo userName="+s+". Use default="+userName); 138 Utility.log(cName+".createManagedConnection" 139 +" ConnectionRequestInfo userName="+userName); 140 s = info.getPassword(); 141 if (s.length()>0) password = s; 142 else 143 Utility.log(cName+".createManagedConnection" 144 +" ConnectionRequestInfo password="+s+". Use default="+password); 145 Utility.log(cName+".createManagedConnection" 146 +" ConnectionRequestInfo password="+password); 147 } 148 149 if ((subject==null && info==null) || userName=="") { 150 userName=defaultUserName; 154 password=defaultPassword; 155 Utility.log(cName+".createManagedConnection default userName="+userName); 156 Utility.log(cName+".createManagedConnection default password="+password); 157 } 158 ConnectionRequestInfoImpl mcCrii = new ConnectionRequestInfoImpl(crii); 159 mcCrii.setPassword(password); 160 mcCrii.setUserName(userName); 161 try { 162 mc = new ManagedConnectionImpl(mcCrii); 166 mc.setMcf(mcf); 167 mc.setRes_Auth(res_auth); 168 mc.setLogWriter(pw); 169 Utility.log(cName+".createManagedConnection for eis="+rai.getEIS_URL() 170 +" mc="+mc); 171 return mc; 172 } catch (Exception ex) { 173 Utility.log(cName+".createManagedConnection : error - Exception ex=" 174 +ex.getMessage()); 175 throw new ResourceAllocationException(ex.getMessage()); 176 } 177 } 178 public void setMatchNull(boolean truefalse) { forceMatchNull=truefalse; 180 } 181 public ManagedConnection matchManagedConnections(Set connectionSet, 182 Subject subject, 183 ConnectionRequestInfo info) 184 throws ResourceException 185 { 186 String mName=".matchManagedConnections"; 187 Utility.log(cName+mName+" Set="+connectionSet+" Subject="+subject+" Crinfo="+info); 188 ManagedConnectionImpl mc = null; 189 if (forceMatchNull) { 190 Utility.log(cName+mName+" force new connection"); 191 return mc; } 193 194 ConnectionRequestInfoImpl mcCrii = null; 195 ConnectionRequestInfoImpl curCrii = new ConnectionRequestInfoImpl(crii); 196 ManagedConnectionImpl wmc = null; 197 Iterator it = connectionSet.iterator(); 198 int cnt=0; 199 while (it.hasNext()) { 200 Object obj = it.next(); 201 Utility.log(cName+mName 202 +" find next ManagedConnectionImpl in Set cnt="+ (++cnt)+" obj="+obj); 203 if (obj instanceof ManagedConnectionImpl) { 204 wmc = (ManagedConnectionImpl) obj; 209 mcCrii=wmc.getCrii(); 210 if (curCrii.equals(mcCrii)) { 211 if (wmc.getCHandle()==null || wmc.isClosed()) { 212 mc=wmc; 213 Utility.log(cName+mName 214 +" connection handle == null. Set cnt="+cnt+" matched mc="+wmc); 215 216 } else { 217 String s = "connection handle should not exist"; 219 Utility.log(cName+mName 220 +"error: "+s+". connectionSet cnt="+cnt); 221 } 222 } 223 224 } 225 } 226 if (mc==null) 227 Utility.log(cName+mName+" mc=null No match found. Set cnt="+cnt); 228 else 229 Utility.log(cName+mName+" match found mc="+mc); 230 return mc; 231 } 232 public void setLogWriter(PrintWriter printwriter) 237 throws ResourceException 238 { 239 Utility.log(cName+".setLogWriter mcfi.pw="+pw+" ApplicationServer's PrintWriter="+printwriter); 240 if (printwriter!=null) pw = printwriter; 241 } 242 243 public PrintWriter getLogWriter() 244 throws ResourceException 245 { 246 return pw; 247 } 248 public ResourceAdapter getResourceAdapter() 249 { 250 return ra; 251 } 252 257 public void setResourceAdapter(ResourceAdapter ra) throws ResourceException 258 { 259 Utility.log(cName+".setResourceAdapter arg="+ra); 260 if (ra==null) { 261 String s = "Application Server cannot set ResourceAdapter to null"; 262 Utility.log(cName+".setResourceAdapter error: "+s); 263 throw new ResourceException(s); 264 } 265 if (!raLoaded) { 266 this.ra = ra; 267 rai = (ResourceAdapterImpl)ra; 268 crii.setRaName(rai.getEIS_URL()); 269 raLoaded=true; } else { 271 if (this.ra==ra) { 272 String s = "setResoruceAdapter called again by the Application " 273 +"Server with same ResourceAdapter value."; 274 Utility.log(cName+".setResourceAdapter error: "+s); 275 throw new ResourceException(s); 276 } else { 277 String s = "ResourceAdapter already set to "+this.ra 278 +". Must not change association"; 279 Utility.log(cName+".setResourceAdapter error: "+s); 280 throw new ResourceException(s); 281 } 282 } 283 } 284 292 public void setServerName(String c1) 293 { 294 ServerName = c1; 295 crii.setServerName(ServerName); 296 } 298 public String getServerName() { 299 return ServerName; 300 } 301 public void setPortNumber(String s) 302 { 303 PortNumber=s; 304 crii.setPortNumber(PortNumber); 305 } 307 public String getPortNumber() 308 { 309 return PortNumber; 310 } 311 public void setDUserName(String s) 312 { 313 defaultUserName=s; 314 crii.setUserName(s); 315 } 317 public String getDUserName() 318 { 319 return defaultUserName; 320 } 321 public void setDPassword(String s) 322 { 323 defaultPassword=s; 324 crii.setPassword(s); 325 } 327 public String getDPassword() 328 { 329 return defaultPassword; 330 } 331 public void setProtocol(String s) 332 { 333 protocol=s; 334 crii.setProtocol(protocol); 335 } 337 public String getProtocol() 338 { 339 return protocol; 340 } 341 public ConnectionManager getCM() 342 { 343 return cm; 344 } 345 public String getRes_Auth() { 346 return res_auth; 347 } 348 public void setRes_Auth(String r) { 349 res_auth=r; 350 } 351 public int hashCode() 355 { 356 if (mfId == null || mfId.length() == 0) { 357 crii.hashCode(); 358 mfId = crii.getId(); 359 } 360 int hash = mfId.hashCode(); 361 return hash; 362 } 363 public boolean equals(Object other) 364 { 365 boolean match = false; 366 if (other==null) { 367 return match; 368 } 369 if (other instanceof ManagedConnectionFactoryImpl) { 370 ManagedConnectionFactoryImpl otherMcf = (ManagedConnectionFactoryImpl)other; 371 try { 372 if (hashCode() != otherMcf.hashCode()) { 373 Utility.log(cName+".equals("+otherMcf+") return="+match); 374 return false; 375 } 376 match = crii.equals(otherMcf.crii); 377 } catch (Exception e) { 378 Utility.log(cName+".equals("+otherMcf+") Exception="+e); 379 return false; 380 } 381 } 382 return match; 383 } 384 391 public boolean isTimerWorking() 392 { 393 ResourceAdapterImpl rai = (ResourceAdapterImpl)ra; 394 try { 395 if (rai.bootstrapCtx==null) { 396 throw new Exception("bootstrapCtx=null"); 397 } 398 Timer timer = rai.bootstrapCtx.createTimer(); Utility.log(cName+".isTimerWorking Timer was created. "); 400 return true; 401 } catch (Exception e) { 402 Utility.log(cName+".isTimerWorking Error: "+e); 403 return false; 404 } 405 } 406 } 407 | Popular Tags |