1 23 24 package com.sun.enterprise.connectors; 25 26 import com.sun.enterprise.*; 27 import com.sun.enterprise.naming.*; 28 import com.sun.enterprise.deployment.*; 29 import com.sun.enterprise.deployment.runtime.connector.ResourceAdapter; 30 import com.sun.enterprise.deployment.runtime.connector.*; 31 import com.sun.enterprise.util.*; 32 import com.sun.enterprise.connectors.util.*; 33 import com.sun.enterprise.resource.*; 34 import com.sun.enterprise.server.*; 35 import com.sun.enterprise.config.*; 36 import com.sun.enterprise.util.i18n.StringManager; 37 import com.sun.logging.LogDomains; 38 import javax.resource.spi.*; 39 import javax.naming.*; 40 import javax.sql.*; 41 import java.security.*; 42 import java.sql.*; 43 import java.util.*; 44 import java.net.*; 45 import java.io.*; 46 import java.util.logging.*; 47 48 49 58 59 public class ActiveOutboundResourceAdapter implements ActiveResourceAdapter { 60 61 protected ConnectorDescriptor desc_; 62 protected String moduleName_; 63 protected ClassLoader jcl_; 64 protected boolean writeToDomainXML_; 65 protected ConnectionDefDescriptor[] connectionDefs_; 66 protected ConnectorRuntime connectorRuntime_ = null; 67 68 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 69 private StringManager localStrings = 70 StringManager.getManager( ActiveOutboundResourceAdapter.class); 71 72 82 83 public ActiveOutboundResourceAdapter( ConnectorDescriptor desc, 84 String moduleName, ClassLoader jcl, 85 boolean writeToDomainXML) 86 { 87 88 this.desc_ = desc; 89 moduleName_ = moduleName; 90 jcl_ = jcl; 91 writeToDomainXML_ = writeToDomainXML; 92 connectorRuntime_ = ConnectorRuntime.getRuntime(); 93 connectionDefs_ = ConnectorDDTransformUtils. getConnectionDefs(desc_); 94 } 95 96 97 public String getModuleName() { 98 return moduleName_; 99 } 100 101 108 109 public void setup() throws ConnectorRuntimeException { 110 111 if(connectionDefs_ == null || 112 connectionDefs_.length != 1) { 113 _logger.log(Level.SEVERE,"rardeployment.invalid_connector_desc", 114 moduleName_); 115 String i18nMsg = localStrings.getString( 116 "ccp_adm.invalid_connector_desc"); 117 throw new ConnectorRuntimeException( i18nMsg ); 118 } 119 ResourcesUtil resUtil = ResourcesUtil.getInstance(); 120 121 if(isServer() && !resUtil.belongToSystemRar(moduleName_)) { 122 createAllConnectorResources(); 123 } 124 _logger.log(Level.FINE, 125 "Completed Active Resource adapter setup", moduleName_); 126 } 127 128 131 132 protected boolean isServer() { 133 if(connectorRuntime_.getEnviron() == ConnectorConstants.SERVER) { 134 return true; 135 } else { 136 return false; 137 } 138 } 139 140 142 143 protected void createAllConnectorResources() 144 throws ConnectorRuntimeException 145 { 146 try { 147 148 if (desc_.getSunDescriptor() != null && 149 desc_.getSunDescriptor().getResourceAdapter() != null) { 150 151 String jndiName = (String )desc_.getSunDescriptor(). 153 getResourceAdapter().getValue(ResourceAdapter.JNDI_NAME); 154 155 if (jndiName == null || jndiName.equals("")) { 156 createDefaultConnectorConnectionPools(true); 158 } else { 159 createSunRAConnectionPool(); 161 createDefaultConnectorConnectionPools(false); 162 } 163 164 } else { 165 166 createDefaultConnectorConnectionPools(false); 168 169 } 170 171 createDefaultConnectorResources(); 173 } catch (ConnectorRuntimeException cre) { 174 _logger.log(Level.SEVERE,"rardeployment.defaultpoolresourcecreation.failed", 177 cre); 178 _logger.log(Level.FINE, "Error while trying to create the default connector" + 179 "connection pool and resource", cre); 180 } 181 182 } 183 184 186 187 protected void destroyAllConnectorResources() { 188 if(!(ResourcesUtil.getInstance().belongToSystemRar(moduleName_))){ 189 deleteDefaultConnectorResources(); 190 deleteDefaultConnectorConnectionPools(); 191 192 if (desc_.getSunDescriptor() != null && 194 desc_.getSunDescriptor().getResourceAdapter() != null) { 195 196 String jndiName = (String )desc_.getSunDescriptor(). 198 getResourceAdapter().getValue(ResourceAdapter.JNDI_NAME); 199 200 if (jndiName == null || jndiName.equals("")) { 201 203 } else { 204 deleteSunRAConnectionPool(); 206 } 207 } 208 } 209 } 210 211 213 214 protected void deleteDefaultConnectorConnectionPools() { 215 for(int i=0;i<connectionDefs_.length;++i) { 216 String connectionDefName = 217 connectionDefs_[i].getConnectionFactoryIntf(); 218 String resourceJndiName = connectorRuntime_.getDefaultPoolName( 219 moduleName_, 220 connectionDefName); 221 try { 222 connectorRuntime_.deleteConnectorConnectionPool( 223 resourceJndiName); 224 } catch(ConnectorRuntimeException cre) { 225 _logger.log(Level.WARNING, 226 "rar.undeployment.default_resource_delete_fail", 227 resourceJndiName); 228 } 229 } 230 } 231 232 234 235 protected void deleteDefaultConnectorResources() { 236 for(int i=0;i<connectionDefs_.length;++i) { 237 String connectionDefName = 238 connectionDefs_[i].getConnectionFactoryIntf(); 239 String resourceJndiName = connectorRuntime_.getDefaultResourceName( 240 moduleName_, 241 connectionDefName); 242 try { 243 connectorRuntime_.deleteConnectorResource(resourceJndiName); 244 } catch(ConnectorRuntimeException cre) { 245 _logger.log(Level.WARNING, 246 "rar.undeployment.default_resource_delete_fail", 247 resourceJndiName); 248 _logger.log(Level.FINE, "Error while trying to delete the default " + 249 "connector resource", cre); 250 } 251 } 252 } 253 254 258 259 public void destroy() { 260 if(isServer()) { 261 destroyAllConnectorResources(); 262 } 263 } 264 265 268 269 public ConnectorDescriptor getDescriptor() { 270 return desc_; 271 } 272 273 281 public ManagedConnectionFactory createManagedConnectionFactory ( 282 ConnectorConnectionPool ccp, ClassLoader jcl) 283 { 284 final String mcfClass = ccp.getConnectorDescriptorInfo(). 285 getManagedConnectionFactoryClass(); 286 try { 287 288 ManagedConnectionFactory mcf = null; 289 if (moduleName_.equals(ConnectorRuntime.DEFAULT_JMS_ADAPTER)) { 290 Object tmp = AccessController.doPrivileged( 291 new PrivilegedExceptionAction() { 292 public Object run() throws Exception { 293 return instantiateMCF(mcfClass); 294 } 295 }); 296 mcf = (ManagedConnectionFactory) tmp; 297 } else { 298 mcf = instantiateMCF(mcfClass); 299 } 300 301 SetMethodAction setMethodAction = new SetMethodAction(mcf, 302 ccp.getConnectorDescriptorInfo().getMCFConfigProperties()); 303 setMethodAction.run(); 304 _logger.log(Level.FINE,"Created MCF object : ",mcfClass); 305 return mcf; 306 } catch (PrivilegedActionException ex) { 307 _logger.log(Level.SEVERE, 308 "rardeployment.privilegedaction_error", 309 new Object []{mcfClass, ex.getException().getMessage()}); 310 _logger.log(Level.FINE, 311 "rardeployment.privilegedaction_error", 312 ex.getException()); 313 return null; 314 } catch (ClassNotFoundException Ex) { 315 _logger.log(Level.SEVERE,"rardeployment.class_not_found", 316 new Object []{mcfClass, Ex.getMessage()}); 317 _logger.log(Level.FINE,"rardeployment.class_not_found", 318 Ex); 319 return null; 320 } catch (InstantiationException Ex) { 321 _logger.log(Level.SEVERE, 322 "rardeployment.class_instantiation_error", 323 new Object []{mcfClass, Ex.getMessage()}); 324 _logger.log(Level.FINE, 325 "rardeployment.class_instantiation_error", 326 Ex); 327 return null; 328 } catch (IllegalAccessException Ex) { 329 _logger.log(Level.SEVERE, 330 "rardeployment.illegalaccess_error", 331 new Object []{mcfClass, Ex.getMessage()}); 332 _logger.log(Level.FINE, 333 "rardeployment.illegalaccess_error", 334 Ex); 335 336 return null; 337 } catch (Exception Ex) { 338 _logger.log(Level.SEVERE,"rardeployment.mcfcreation_error", 339 new Object []{mcfClass, Ex.getMessage()}); 340 _logger.log(Level.FINE,"rardeployment.mcfcreation_error", 341 Ex); 342 return null; 343 } 344 345 } 346 347 private ManagedConnectionFactory instantiateMCF(String mcfClass) 348 throws Exception { 349 if(jcl_ != null) { 350 return (ManagedConnectionFactory)jcl_.loadClass( 351 mcfClass).newInstance(); 352 } else { 353 return (ManagedConnectionFactory) Class.forName( 354 mcfClass).newInstance(); 355 } 356 } 357 358 359 361 362 protected void createDefaultConnectorResources() 363 throws ConnectorRuntimeException 364 { 365 for(int i=0; i<connectionDefs_.length;++i) { 366 String connectionDefName = 367 connectionDefs_[i].getConnectionFactoryIntf(); 368 String resourceName = connectorRuntime_.getDefaultResourceName( 369 moduleName_, 370 connectionDefName); 371 String poolName = connectorRuntime_.getDefaultPoolName( 372 moduleName_, 373 connectionDefName); 374 connectorRuntime_.createConnectorResource( 375 resourceName,poolName,null); 376 _logger.log(Level.FINE,"Created default connector resource : ", 377 resourceName); 378 379 } 380 } 381 382 385 386 protected void createDefaultConnectorConnectionPools(boolean useSunRA) 387 throws ConnectorRuntimeException 388 { 389 390 for(int i = 0;i < connectionDefs_.length;++i) { 391 String poolName = connectorRuntime_.getDefaultPoolName( 392 moduleName_, 393 connectionDefs_[i].getConnectionFactoryIntf()); 394 395 ConnectorDescriptorInfo connectorDescriptorInfo = 396 ConnectorDDTransformUtils.getConnectorDescriptorInfo( 397 connectionDefs_[i]); 398 connectorDescriptorInfo.setRarName(moduleName_); 399 connectorDescriptorInfo.setResourceAdapterClassName( 400 desc_.getResourceAdapterClass()); 401 ConnectorConnectionPool connectorPoolObj; 402 403 if (useSunRA) { 406 connectorPoolObj = 407 ConnectionPoolObjectsUtils.createSunRaConnectorPoolObject(poolName, desc_, moduleName_); 408 } else { 409 connectorPoolObj = 410 ConnectionPoolObjectsUtils.createDefaultConnectorPoolObject(poolName, moduleName_); 411 } 412 413 connectorPoolObj.setConnectorDescriptorInfo( 414 connectorDescriptorInfo ); 415 connectorRuntime_.createConnectorConnectionPool(connectorPoolObj); 416 _logger.log(Level.FINE,"Created default connection pool : ", 417 poolName); 418 } 419 } 420 421 426 427 public void createSunRAConnectionPool() 428 throws ConnectorRuntimeException 429 { 430 431 String defaultPoolName = connectorRuntime_.getDefaultPoolName( 432 moduleName_,connectionDefs_[0].getConnectionFactoryIntf()); 433 434 String sunRAPoolName = defaultPoolName+ConnectorConstants.SUN_RA_POOL; 435 436 ConnectorDescriptorInfo connectorDescriptorInfo = 437 ConnectorDDTransformUtils.getConnectorDescriptorInfo( 438 connectionDefs_[0]); 439 connectorDescriptorInfo.setRarName(moduleName_); 440 connectorDescriptorInfo.setResourceAdapterClassName( 441 desc_.getResourceAdapterClass()); 442 ConnectorConnectionPool connectorPoolObj = 443 ConnectionPoolObjectsUtils.createSunRaConnectorPoolObject( 444 sunRAPoolName,desc_,moduleName_); 445 446 connectorPoolObj.setConnectorDescriptorInfo( 447 connectorDescriptorInfo ); 448 connectorRuntime_.createConnectorConnectionPool(connectorPoolObj); 449 _logger.log(Level.FINE,"Created SUNRA connection pool:",sunRAPoolName); 450 String jndiName = (String )desc_.getSunDescriptor(). 451 getResourceAdapter().getValue(ResourceAdapter.JNDI_NAME); 452 connectorRuntime_.createConnectorResource(jndiName,sunRAPoolName,null); 453 _logger.log(Level.FINE,"Created SUNRA connector resource : ", 454 jndiName); 455 456 String key = System.getProperty("connector.sunra"); 457 if(key != null && key.equals("true")) { 458 if(writeToDomainXML_) { 459 writePoolResourceToServerXML(sunRAPoolName); 460 } 461 } 462 } 463 464 468 469 public void deleteSunRAConnectionPool() 470 { 471 472 String defaultPoolName = connectorRuntime_.getDefaultPoolName( 473 moduleName_,connectionDefs_[0].getConnectionFactoryIntf()); 474 475 String sunRAPoolName = defaultPoolName+ConnectorConstants.SUN_RA_POOL; 476 try { 477 connectorRuntime_.deleteConnectorConnectionPool( 478 sunRAPoolName); 479 } catch(ConnectorRuntimeException cre) { 480 _logger.log(Level.WARNING, 481 "rar.undeployment.default_resource_delete_fail", 482 sunRAPoolName); 483 } 484 } 485 486 490 public ClassLoader getClassLoader(){ 491 return jcl_; 492 } 493 494 497 498 protected void writePoolResourceToServerXML(String poolName) 499 throws ConnectorRuntimeException 500 { 501 502 ResourcesUtil resutil = ResourcesUtil.getInstance(); 503 try { 504 resutil.writeSunConnector(desc_,moduleName_,poolName); 505 } catch(ConfigException ce) { 506 String i18nMsg = localStrings.getString("ccr_adm.err_writing_connector_desc"); 507 ConnectorRuntimeException crex = new ConnectorRuntimeException(i18nMsg); 508 crex.initCause(ce); 509 throw crex; 510 } 511 } 512 } 513 | Popular Tags |