1 23 24 package com.sun.enterprise.connectors; 25 26 import java.util.ArrayList ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.logging.*; 31 import javax.naming.*; 32 import com.sun.enterprise.connectors.util.*; 33 import com.sun.enterprise.deployment.*; 34 import com.sun.enterprise.deployment.util.*; 35 import com.sun.enterprise.server.*; 36 import com.sun.enterprise.*; 37 import com.sun.enterprise.config.serverbeans.*; 38 import com.sun.enterprise.config.*; 39 import com.sun.enterprise.util.ConnectorClassLoader; 40 41 42 48 49 public class ResourceAdapterAdminServiceImpl 50 extends ConnectorServiceImpl 51 implements ConnectorAdminService { 52 53 private JmsRaMapping ramap= null; 54 55 private int resource_adapter_shutdown_timeout = 0; 56 57 private final Hashtable raShutdownStatusTable = new Hashtable (); 59 60 63 64 public ResourceAdapterAdminServiceImpl() { 65 super(); 66 ramap= new JmsRaMapping(); 67 } 68 69 70 82 83 public void destroyActiveResourceAdapter(String moduleName) 84 throws ConnectorRuntimeException { 85 destroyActiveResourceAdapter(moduleName, false); 86 87 } 88 89 110 111 public void destroyActiveResourceAdapter( 112 String moduleName, 113 boolean cascade) 114 throws ConnectorRuntimeException { 115 116 ResourcesUtil resutil= ResourcesUtil.getInstance(); 117 if (resutil == null) { 118 ConnectorRuntimeException cre= 119 new ConnectorRuntimeException("Failed to get ResourcesUtil object"); 120 _logger.log(Level.SEVERE, "resourcesutil_get_failure", moduleName); 121 _logger.log(Level.SEVERE, "", cre); 122 throw cre; 123 } 124 Object [][] resources= null; 125 try { 126 resources= resutil.getAllConnectorResources(moduleName); 127 } catch (ConfigException ce) { 128 ConnectorRuntimeException cre= 129 new ConnectorRuntimeException("Failed to get Resources from domain.xml"); 130 ce.initCause(ce); 131 _logger.log( 132 Level.SEVERE, 133 "rardeployment.resources_list_error", 134 cre); 135 throw cre; 136 } 137 138 boolean errrorOccured= false; 139 140 com.sun.enterprise.config.serverbeans.ConnectorConnectionPool ccp= null; 141 if (cascade == true && resources != null) { 142 for (int i= 0; 143 resources[0] != null && i < resources[0].length; 144 ++i) { 145 ccp= 146 (com 147 .sun 148 .enterprise 149 .config 150 .serverbeans 151 .ConnectorConnectionPool) resources[0][i]; 152 try { 153 _runtime.deleteConnectorConnectionPool( 154 ccp.getName(), 155 cascade); 156 } catch (ConnectorRuntimeException cre) { 157 errrorOccured= true; 158 } 159 } 160 for (int i= 0; 161 resources[2] != null && i < resources[2].length; 162 ++i) { 163 try { 164 AdminObjectResource aor= 165 (AdminObjectResource) resources[2][i]; 166 _runtime.deleteAdminObject(aor.getJndiName()); 167 } catch (ConnectorRuntimeException cre) { 168 errrorOccured= true; 169 } 170 } 171 } else if ( 172 (resources[0] != null && resources[0].length != 0) 173 || (resources[1] != null && resources[1].length != 0) 174 || (resources[2] != null && resources[2].length != 0)) { 175 _logger.log( 176 Level.SEVERE, 177 "rardeployment.pools_and_resources_exist", 178 moduleName); 179 ConnectorRuntimeException cre= 180 new ConnectorRuntimeException("Error: Connector Connection Pools/resources exist."); 181 _logger.log(Level.SEVERE, "", cre); 182 throw cre; 183 } 184 185 if (!stopAndRemoveActiveResourceAdapter(moduleName)) { 186 ConnectorRuntimeException cre= 187 new ConnectorRuntimeException("Failed to remove Active Resource Adapter"); 188 _logger.log( 189 Level.SEVERE, 190 "rardeployment.ra_removal_registry_failure", 191 moduleName); 192 _logger.log(Level.SEVERE, "", cre); 193 throw cre; 194 } 195 196 if (!(ResourcesUtil.getInstance().belongToEmbeddedRar(moduleName))) { 198 ConnectorClassLoader.getInstance().removeResourceAdapter(moduleName); 199 } 200 201 202 if (errrorOccured == true) { 203 ConnectorRuntimeException cre= 204 new ConnectorRuntimeException("Failed to remove all connector resources/pools"); 205 _logger.log( 206 Level.SEVERE, 207 "rardeployment.ra_resource_removal", 208 moduleName); 209 _logger.log(Level.SEVERE, "", cre); 210 throw cre; 211 } 212 } 213 214 232 233 public synchronized void createActiveResourceAdapter( 234 ConnectorDescriptor connectorDescriptor, 235 String moduleName, 236 String moduleDir, 237 boolean writeSunDescriptor) 238 throws ConnectorRuntimeException { 239 _logger.fine("ResourceAdapterAdminServiceImpl :: createActiveRA " 240 + moduleName + " at " + moduleDir ); 241 242 ActiveResourceAdapter activeResourceAdapter= 243 _registry.getActiveResourceAdapter(moduleName); 244 if (activeResourceAdapter != null) { 245 _logger.log( 246 Level.FINE, 247 "rardeployment.resourceadapter.already.started", 248 moduleName); 249 return; 250 } 251 252 ClassLoader loader= null; 253 try { 254 loader= connectorDescriptor.getClassLoader(); 255 } catch (Exception ex) { 256 _logger.log( 257 Level.FINE, 258 "No classloader available with connector descriptor"); 259 loader= null; 260 } 261 ModuleDescriptor moduleDescriptor= null; 262 Application application = null; 263 _logger.fine("ResourceAdapterAdminServiceImpl :: createActiveRA " 264 + moduleName + " at " + moduleDir + " loader :: " + loader); 265 if (loader == null) { 266 if (environment == SERVER) { 267 ConnectorClassLoader.getInstance().addResourceAdapter(moduleName, moduleDir); 268 loader = ConnectorClassLoader.getInstance(); 269 if (loader == null) { 270 ConnectorRuntimeException cre= 271 new ConnectorRuntimeException("Failed to obtain the class loader"); 272 _logger.log( 273 Level.SEVERE, 274 "rardeployment.failed_toget_classloader"); 275 _logger.log(Level.SEVERE, "", cre); 276 throw cre; 277 } 278 } 279 } else { 280 connectorDescriptor.setClassLoader(null); 281 moduleDescriptor= connectorDescriptor.getModuleDescriptor(); 282 application = connectorDescriptor.getApplication(); 283 connectorDescriptor.setModuleDescriptor(null); 284 connectorDescriptor.setApplication(null); 285 } 286 try { 287 activeResourceAdapter= 288 ActiveRAFactory.createActiveResourceAdapter( 289 connectorDescriptor, 290 moduleName, 291 loader, 292 writeSunDescriptor); 293 _logger.fine("ResourceAdapterAdminServiceImpl :: createActiveRA " + 294 moduleName + " at " + moduleDir + 295 " ADDING to registry " + activeResourceAdapter); 296 _registry.addActiveResourceAdapter( 297 moduleName, 298 activeResourceAdapter); 299 _logger.fine("ResourceAdapterAdminServiceImpl:: createActiveRA " + 300 moduleName + " at " + moduleDir 301 + " env =server ? " + (environment==SERVER)); 302 303 if (environment == SERVER) { 304 activeResourceAdapter.setup(); 305 String descriptorJNDIName = ConnectorAdminServiceUtils. 306 getReservePrefixedJNDINameForDescriptor(moduleName); 307 _logger.fine("ResourceAdapterAdminServiceImpl :: createActiveRA " 308 + moduleName + " at " + moduleDir 309 + " publishing descriptor " + descriptorJNDIName); 310 updateRAConfigInDescriptor(connectorDescriptor, moduleName); 313 Switch.getSwitch().getNamingManager().publishObject( 314 descriptorJNDIName, connectorDescriptor, true); 315 String securityWarningMessage= 316 _runtime.getSecurityPermissionSpec(moduleName); 317 if (securityWarningMessage != null) { 319 _logger.log(Level.WARNING, securityWarningMessage); 320 } 321 } 322 323 } catch (NullPointerException npEx) { 324 ConnectorRuntimeException cre= 325 new ConnectorRuntimeException("Error in creating active RAR"); 326 cre.initCause(npEx); 327 _logger.log( 328 Level.SEVERE, 329 "rardeployment.nullPointerException", 330 moduleName); 331 _logger.log(Level.SEVERE, "", cre); 332 throw cre; 333 } catch (NamingException ne) { 334 ConnectorRuntimeException cre= 335 new ConnectorRuntimeException("Error in creating active RAR"); 336 cre.initCause(ne); 337 _logger.log(Level.SEVERE, "rardeployment.jndi_publish_failure"); 338 _logger.log(Level.SEVERE, "", cre); 339 throw cre; 340 } finally { 341 if (moduleDescriptor != null) { 342 connectorDescriptor.setModuleDescriptor(moduleDescriptor); 343 connectorDescriptor.setApplication(application); 344 connectorDescriptor.setClassLoader(loader); 345 } 346 } 347 } 348 349 359 private void updateRAConfigInDescriptor(ConnectorDescriptor connectorDescriptor, 360 String moduleName) { 361 362 ResourceAdapterConfig raConfig = 363 ConnectorRegistry.getInstance().getResourceAdapterConfig(moduleName); 364 365 ElementProperty[] raConfigProps = null; 366 if(raConfig != null) { 367 raConfigProps = raConfig.getElementProperty(); 368 } 369 370 _logger.fine("current RAConfig In Descriptor " + connectorDescriptor.getConfigProperties()); 371 372 if(raConfigProps != null) { 373 Set mergedProps = ConnectorDDTransformUtils.mergeProps( 374 raConfigProps, connectorDescriptor.getConfigProperties()); 375 Set actualProps = connectorDescriptor.getConfigProperties(); 376 actualProps.clear(); 377 actualProps.addAll(mergedProps); 378 _logger.fine("updated RAConfig In Descriptor " + connectorDescriptor.getConfigProperties()); 379 } 380 381 } 382 383 384 399 400 public synchronized void createActiveResourceAdapter( 401 String moduleDir, 402 String moduleName, 403 boolean writeSunDescriptor) 404 throws ConnectorRuntimeException { 405 406 ActiveResourceAdapter activeResourceAdapter= 407 _registry.getActiveResourceAdapter(moduleName); 408 if (activeResourceAdapter != null) { 409 _logger.log( 410 Level.FINE, 411 "rardeployment.resourceadapter.already.started", 412 moduleName); 413 return; 414 } 415 416 if (ResourcesUtil.getInstance().belongToSystemRar(moduleName)) { 417 moduleDir = Switch.getSwitch().getResourceInstaller(). 418 getSystemModuleLocation(moduleName); 419 } 420 421 ConnectorDescriptor connectorDescriptor= null; 422 connectorDescriptor= 423 ConnectorDDTransformUtils.getConnectorDescriptor(moduleDir); 424 if (connectorDescriptor == null) { 425 ConnectorRuntimeException cre= 426 new ConnectorRuntimeException("Failed to obtain the connectorDescriptor"); 427 _logger.log( 428 Level.SEVERE, 429 "rardeployment.connector_descriptor_notfound", 430 moduleName); 431 _logger.log(Level.SEVERE, "", cre); 432 throw cre; 433 } 434 createActiveResourceAdapter( 435 connectorDescriptor, 436 moduleName, 437 moduleDir, 438 writeSunDescriptor); 439 } 440 441 452 453 public void reCreateActiveResourceAdapter(String moduleName) 454 throws ConnectorRuntimeException { 455 String moduleDir= null; 456 if (isRarDeployed(moduleName)) { 457 _runtime.killAllPools(moduleName); 458 stopAndRemoveActiveResourceAdapter(moduleName); 459 moduleDir= ResourcesUtil.getInstance().getLocation(moduleName); 460 createActiveResourceAdapter(moduleDir, moduleName, false); 461 } else { 462 moduleDir= ResourcesUtil.getInstance().getLocation(moduleName); 463 if (moduleDir != null) { 464 createActiveResourceAdapter(moduleDir, moduleName, false); 465 } 466 } 467 } 468 469 478 479 public boolean stopAndRemoveActiveResourceAdapter(String moduleName) { 480 481 ActiveResourceAdapter acr= null; 482 if (moduleName != null) { 483 acr= _registry.getActiveResourceAdapter(moduleName); 484 } 485 if (acr != null) { 486 acr.destroy(); 487 boolean ret= _registry.removeActiveResourceAdapter(moduleName); 488 return ret; 489 } 490 return false; 491 } 492 493 public void addResourceAdapterConfig( 494 String rarName, 495 ResourceAdapterConfig raConfig) 496 throws ConnectorRuntimeException { 497 if (rarName != null && raConfig != null) { 498 _registry.addResourceAdapterConfig(rarName, raConfig); 499 reCreateActiveResourceAdapter(rarName); 500 } 501 } 502 503 509 510 public void deleteResourceAdapterConfig(String rarName) { 511 if (rarName != null) { 512 _registry.removeResourceAdapterConfig(rarName); 513 } 514 } 515 516 public static boolean isJmsRa() { 517 return true; 519 } 520 521 public JmsRaMapping getJmsRaMapping() { 522 return ramap; 523 } 524 525 532 533 public boolean isRarDeployed(String moduleName) { 534 535 ActiveResourceAdapter activeResourceAdapter= 536 _registry.getActiveResourceAdapter(moduleName); 537 if (activeResourceAdapter != null) { 538 return true; 539 } else { 540 return false; 541 } 542 } 543 544 547 public void stopAllActiveResourceAdapters() { 548 try { 549 resource_adapter_shutdown_timeout = 550 ResourcesUtil.getInstance().getShutdownTimeout(); 551 } catch (ConnectorRuntimeException e) { 552 _logger.log(Level.WARNING, "error_reading_connectorservice_elt", e); 553 resource_adapter_shutdown_timeout 555 = ConnectorConstants.DEFAULT_RESOURCE_ADAPTER_SHUTDOWN_TIMEOUT; 556 } 557 ActiveResourceAdapter[] resourceAdapters = 558 ConnectorRegistry.getInstance().getAllActiveResourceAdapters(); 559 560 ArrayList resourceAdaptersToStop = new ArrayList (); 562 for (int i = 0; i < resourceAdapters.length; i++) { 563 if (resourceAdapters[i] instanceof ActiveInboundResourceAdapter){ 564 _logger.log(Level.FINE, "Connector 1.5 spec compliant RA", 565 resourceAdapters[i].getModuleName()); 566 raShutdownStatusTable.put( 567 resourceAdapters[i].getModuleName(), new Boolean (false)); 568 resourceAdaptersToStop.add(resourceAdapters[i]); 569 } else { 570 _logger.log(Level.FINE, "Connector 1.0 spec compliant RA", 571 resourceAdapters[i].getModuleName()); 572 } 573 } 574 ActiveResourceAdapter[] raToStop = (ActiveResourceAdapter[]) 575 resourceAdaptersToStop.toArray(new ActiveResourceAdapter[]{}); 576 sendStopToAllResourceAdapters(raToStop); 577 } 578 579 580 581 585 private void sendStopToAllResourceAdapters(ActiveResourceAdapter[] 586 resourceAdaptersToStop) { 587 int numberOfResourceAdaptersToStop = 0; 589 590 numberOfResourceAdaptersToStop = resourceAdaptersToStop.length; 591 592 Thread [] raShutDownThreads = new Thread [numberOfResourceAdaptersToStop]; 593 Thread [] joinerThreads = new Thread [numberOfResourceAdaptersToStop]; 594 595 for (int i = 0; i < numberOfResourceAdaptersToStop; i++) { 598 599 _logger.log(Level.FINE, "Starting RA shutdown thread for " 600 + ( (ActiveInboundResourceAdapter)resourceAdaptersToStop[i] ) 601 .getModuleName() ); 602 Thread rast = new RAShutdownThread( 603 (ActiveInboundResourceAdapter)resourceAdaptersToStop[i]); 604 raShutDownThreads[i] = rast; 605 rast.start(); 606 607 _logger.log(Level.FINE, "Starting Thread to join time-out " + 608 "shutdown of " + 609 ( ( ActiveInboundResourceAdapter )resourceAdaptersToStop[i]).getModuleName() ); 610 Thread joiner = new JoinerThread(raShutDownThreads[i]); 611 joinerThreads[i] = joiner; 612 joiner.start(); 613 } 614 615 for (int i = 0; i < joinerThreads.length; i++) { 617 try { 618 _logger.log(Level.FINE, "Joining joiner thread of " 619 + ((ActiveResourceAdapter)resourceAdaptersToStop[i]) 620 .getModuleName() ); 621 joinerThreads[i].join(); 622 } catch (InterruptedException e) { 623 e.printStackTrace(); 624 } 625 } 626 627 _logger.log(Level.FINE, "stop() Complete for all " + 628 "active 1.5 compliant RARs"); 629 630 if (resourceAdaptersToStop.length != 0) { 632 _logger.log(Level.FINE, "resource_adapter_stop_status"); 633 } 634 635 for (Iterator iter = raShutdownStatusTable.keySet().iterator(); 636 iter.hasNext();) { 637 String raName = (String ) iter.next(); 638 if(((Boolean )raShutdownStatusTable.get(raName)).booleanValue()) { 639 _logger.log(Level.INFO, "ra.stop-successful", raName); 640 } else { 641 _logger.log(Level.WARNING, "ra.stop-unsuccessful", raName); 642 } 643 } 644 } 645 646 private class JoinerThread extends Thread { 647 private Thread threadToJoin; 648 649 public JoinerThread(Thread threadToJoin) { 650 this.threadToJoin = threadToJoin; 651 } 652 653 public void run() { 654 try { 655 this.threadToJoin.join(ResourceAdapterAdminServiceImpl.this. 656 resource_adapter_shutdown_timeout * 1000); 657 } catch (InterruptedException e) { 658 e.printStackTrace(); 659 } 660 } 661 } 662 663 private class RAShutdownThread extends Thread { 664 private ActiveInboundResourceAdapter ra; 665 666 public RAShutdownThread(ActiveInboundResourceAdapter ratoBeShutDown){ 667 super(); 668 this.ra = ratoBeShutDown; 669 this.setDaemon(true); 671 } 672 673 public void run() { 674 _logger.log(Level.FINE, "Calling" + ra.getModuleName() 675 + " shutdown "); 676 this.ra.destroy(); 677 ResourceAdapterAdminServiceImpl.this.raShutdownStatusTable.put( 678 ra.getModuleName(), new Boolean (true)); 679 } 680 } 681 682 } 683 | Popular Tags |