1 23 24 37 package com.sun.enterprise.server; 38 39 import java.io.File ; 40 import java.util.Vector ; 41 import java.util.Set ; 42 import java.util.Iterator ; 43 import java.util.Enumeration ; 44 import java.util.HashSet ; 45 import java.util.logging.Logger ; 46 import java.util.logging.Level ; 47 import javax.enterprise.deploy.shared.ModuleType ; 48 import javax.naming.NameAlreadyBoundException ; 49 50 import com.sun.ejb.Container; 51 import com.sun.ejb.ContainerFactory; 52 53 import com.sun.enterprise.Switch; 54 import com.sun.enterprise.deployment.Application; 55 import com.sun.enterprise.deployment.ConnectorDescriptor; 56 import com.sun.enterprise.deployment.BundleDescriptor; 57 import com.sun.enterprise.deployment.EjbDescriptor; 58 import com.sun.enterprise.SecurityManager; 59 import com.sun.enterprise.loader.EJBClassLoader; 60 import com.sun.enterprise.loader.EJBClassPathUtils; 61 import com.sun.enterprise.loader.InstrumentableClassLoader; 62 import com.sun.logging.LogDomains; 63 import com.sun.enterprise.security.factory.FactoryForSecurityManagerFactoryImpl; 64 import com.sun.enterprise.security.factory.SecurityManagerFactory; 65 import com.sun.enterprise.security.factory.FactoryForSecurityManagerFactory; 66 67 import com.sun.enterprise.security.SecurityUtil; 68 69 import com.sun.enterprise.config.ConfigContext; 70 import com.sun.enterprise.config.ConfigException; 71 import com.sun.enterprise.config.serverbeans.Applications; 72 import com.sun.enterprise.config.serverbeans.Domain; 73 import com.sun.enterprise.config.serverbeans.J2eeApplication; 74 75 import com.sun.enterprise.instance.BaseManager; 76 77 import com.sun.enterprise.util.io.FileUtils; 78 import com.sun.enterprise.util.Utility; 79 80 import com.sun.enterprise.connectors.*; 81 import com.sun.enterprise.server.ondemand.entry.*; 82 83 import com.sun.enterprise.deployment.Descriptor; 85 import javax.management.MBeanException ; 86 87 import com.sun.enterprise.security.application.EJBSecurityManager; 88 import com.sun.enterprise.server.event.ApplicationEvent; 89 import com.sun.enterprise.server.event.EjbContainerEvent; 90 import com.sun.enterprise.server.event.ApplicationLoaderEventNotifier; 91 92 import com.sun.enterprise.admin.monitor.WSMonitorLifeCycleFactory; 93 94 101 abstract class AbstractLoader implements EntryPoint { 102 103 104 protected String id = null; 105 106 107 ClassLoader parentClassLoader = null; 108 109 110 ClassLoader ejbClassLoader = null; 111 112 113 Application application = null; 114 115 protected boolean robFlag = false; 116 117 118 protected ApplicationRegistry registry = null; 119 120 121 protected BaseManager configManager = null; 122 123 124 static Logger _logger = LogDomains.getLogger(LogDomains.LOADER_LOGGER); 125 126 127 private FactoryForSecurityManagerFactory ffsmf = null; 128 129 130 131 protected boolean cascade=false; 132 133 150 protected int loadUnloadAction; 151 152 private ApplicationLoaderEventNotifier loaderEventNotifier; 153 154 private ConfigContext dynamicConfigContext; 155 156 164 AbstractLoader(String id, ClassLoader parentClassLoader, 165 BaseManager configMgr) { 166 167 this.id = id; 168 this.parentClassLoader = parentClassLoader; 169 this.registry = ApplicationRegistry.getInstance(); 170 this.configManager = configMgr; 171 172 this.loaderEventNotifier = 173 ApplicationLoaderEventNotifier.getInstance(); 174 } 175 176 181 protected void initializeLoader(String [] classPaths, String moduleRoot, ModuleType moduleType) { 182 this.ejbClassLoader = EJBClassPathUtils.createEJBClassLoader( 183 classPaths, moduleRoot, this.id, this.parentClassLoader, moduleType); 184 } 185 186 189 protected void initializeLoader(ClassLoader clazzloader) { 190 this.ejbClassLoader = clazzloader; 191 } 192 193 void setConfigContext(ConfigContext dynamicConfigContext) { 194 this.dynamicConfigContext = dynamicConfigContext; 195 } 196 197 204 abstract boolean load(boolean jsr77); 205 206 213 abstract boolean unload(boolean jsr77); 214 215 220 abstract void createRootMBean () throws MBeanException ; 221 222 227 abstract void deleteRootMBean () throws MBeanException ; 228 229 236 abstract void createLeafMBeans () throws MBeanException ; 237 238 245 abstract void deleteLeafMBeans () throws MBeanException ; 246 247 256 abstract void createLeafMBean (Descriptor descriptor) throws MBeanException ; 257 258 265 abstract void deleteLeafMBean (Descriptor descriptor) throws MBeanException ; 266 267 270 abstract void deleteLeafAndRootMBeans () throws MBeanException ; 271 272 279 abstract void setState(int state) throws MBeanException ; 280 281 286 ClassLoader getClassLoader() { 287 return this.ejbClassLoader; 288 } 289 290 295 Application getApplication() { 296 return this.application; 297 } 298 299 305 protected void done() { 306 307 if (this.ejbClassLoader instanceof EJBClassLoader) { 309 ((EJBClassLoader) ejbClassLoader).done(); 310 } 311 312 Set rars = this.application.getRarDescriptors(); 314 for (Iterator itr = rars.iterator(); itr.hasNext();) { 315 ConnectorDescriptor cd = (ConnectorDescriptor) itr.next(); 316 cd.setClassLoader(null); 317 } 318 319 this.id = null; 320 this.parentClassLoader = null; 321 this.application = null; 322 this.ejbClassLoader = null; 323 this.registry = null; 324 this.configManager = null; 325 } 326 327 335 protected boolean unloadEjbs(boolean jsr77) { 336 337 boolean result = true; 338 339 if (this.application == null) { 340 result = false; 341 } else { 342 343 Vector beanDescriptors = this.application.getEjbDescriptors(); 345 Enumeration e = beanDescriptors.elements(); 346 347 while ( e.hasMoreElements() ) { 348 EjbDescriptor nextDescriptor = (EjbDescriptor) e.nextElement(); 349 350 notifyEjbEvent(EjbContainerEvent.BEFORE_EJB_CONTAINER_UNLOAD, 351 nextDescriptor); 352 353 this.registry.removeUniqueId( nextDescriptor.getUniqueId() ); 355 356 try { 357 Container container = (Container) this.registry. 359 removeDescriptor2Container(nextDescriptor); 360 if (container != null) { 361 container.undeploy(); 362 } 363 364 } catch (Exception beanEx) { 365 result = false; 366 _logger.log(Level.WARNING, 367 "loader.unexpected_error_while_unloading_ejb_container", 368 beanEx); 369 } 370 371 notifyEjbEvent(EjbContainerEvent.AFTER_EJB_CONTAINER_UNLOAD, 372 nextDescriptor); 373 374 if (jsr77) { 375 try { 376 deleteLeafMBean(nextDescriptor); 377 } catch (MBeanException mbe) { 378 _logger.log(Level.WARNING, 379 "loader.delete_ejb_mbean_exception", mbe); 380 } 381 } 382 } 383 384 if (!application.isVirtual()) { 385 this.registry.removeAppId2ClassLoader(this.id); 387 } else { 388 this.registry.removeModuleId2ClassLoader(this.id); 389 } 390 391 this.registry.removeClassLoader2Application(this.ejbClassLoader); 393 394 } 395 396 _logger.log(Level.FINE,"[AbstractLoader] Unload EJB(s) Status: " 397 + result); 398 399 return result; 400 } 401 402 void initializeRegistry() { 403 if (!application.isVirtual()) { 404 this.registry.addAppId2ClassLoader(this.id, this.ejbClassLoader); 406 } else { 407 this.registry.addModuleId2ClassLoader(this.id, this.ejbClassLoader); 408 } 409 410 this.registry.addClassLoader2Application(this.ejbClassLoader, 412 this.application); 413 } 414 415 422 protected boolean loadEjbs(boolean jsr77) { 423 424 final ClassLoader loader = this.ejbClassLoader; 426 427 if (this.application == null) { 428 return false; 429 } 430 431 if (_logger.isLoggable(Level.FINEST)) { 432 _logger.log(Level.FINEST, 433 "[AbstractLoader] Id: " + this.id + 434 " Setting thread context class loader to: " + loader); 435 } 436 437 440 ClassLoader savedContextClassLoader = Utility.setContextClassLoader(loader); 441 442 try { 443 444 initializeRegistry(); 445 HashSet myContainers = new HashSet (); 446 447 if (_logger.isLoggable(Level.FINEST)) { 448 _logger.log(Level.FINEST, 449 "[AbstractLoader] Id: " + this.id 450 + " Unique Id: " + this.application.getUniqueId()); 451 } 452 453 Vector beanDescriptors = application.getEjbDescriptors(); 454 455 Enumeration e = beanDescriptors.elements(); 457 ContainerFactory cf = Switch.getSwitch().getContainerFactory(); 458 459 while (e.hasMoreElements()) { 460 EjbDescriptor nextDescriptor = (EjbDescriptor) e.nextElement(); 461 462 if ( !this.registry.isUnique( nextDescriptor.getUniqueId() ) ) { 464 465 _logger.log(Level.SEVERE, "loader.duplicate_unique_id", 466 new Object [] { 467 String.valueOf(nextDescriptor.getUniqueId()), 468 nextDescriptor.getName() 469 }); 470 471 unloadEjbs(jsr77); 473 474 return false; 476 } 477 478 try { 479 if(ffsmf == null){ 480 ffsmf = FactoryForSecurityManagerFactoryImpl.getInstance(); 481 } 482 SecurityManagerFactory smf = ffsmf.getSecurityManagerFactory("ejb"); 483 SecurityManager sm = smf.createSecurityManager(nextDescriptor); 484 Container container = null; 486 try { 487 notifyEjbEvent(EjbContainerEvent.BEFORE_EJB_CONTAINER_LOAD, 488 nextDescriptor); 489 490 container = cf.createContainer(nextDescriptor, loader, 491 sm, dynamicConfigContext); 492 493 notifyEjbEvent(EjbContainerEvent.AFTER_EJB_CONTAINER_LOAD, 494 nextDescriptor); 495 } catch (NameAlreadyBoundException jndiEx) { 496 497 _logger.log(Level.SEVERE, "loader.jndi_name_conflict", 499 new Object [] { 500 this.id, 501 nextDescriptor.getJndiName(), 502 nextDescriptor.getName() 503 }); 504 505 _logger.log(Level.SEVERE, 507 "loader.naming_exception_in_createcontainer", jndiEx); 508 509 unloadEjbs(jsr77); 511 512 return false; 514 } 515 516 if (_logger.isLoggable(Level.FINE)) { 517 _logger.log(Level.FINE, 518 "Created container with uinque id: " 519 + nextDescriptor.getUniqueId()); 520 } 521 522 this.registry.addDescriptor2Container(nextDescriptor,container); 524 525 myContainers.add(container); 527 528 } catch (Exception beanEx) { 529 _logger.log(Level.SEVERE, 530 "loader.unexpected_error_while_creating_ejb_container", 531 beanEx); 532 533 unloadEjbs(jsr77); 535 536 return false; 537 } 538 539 if (jsr77) { 540 try { 541 createLeafMBean(nextDescriptor); 542 } catch (MBeanException mbe) { 543 _logger.log(Level.WARNING, "loader.create_ejb_mbean_exception", mbe); 544 } 545 } 546 } 547 548 try { 549 Enumeration en = beanDescriptors.elements(); 550 while (en.hasMoreElements()) { 552 EjbDescriptor nextDescriptor = (EjbDescriptor)en.nextElement(); 553 SecurityUtil.generatePolicyFile( 554 EJBSecurityManager.getContextID(nextDescriptor)); 555 } 556 } catch (Exception ge) { 557 _logger.log(Level.SEVERE, 558 "code.could_not_generate/load_the_policy_files_for_system_app", 559 ge); 560 561 unloadEjbs(jsr77); 563 564 return false; 565 } 566 567 Iterator iter = myContainers.iterator(); 569 while ( iter.hasNext() ) { 570 Container c = (Container) iter.next(); 571 c.doAfterApplicationDeploy(); 572 580 } 582 583 com.sun.enterprise.security.Audit.showACL(this.application); 585 586 if (this.application.getEjbComponentCount() > 0) { 588 _logger.log(Level.INFO, "loader.ejbs_loaded", this.id); 589 } 590 591 _logger.log(Level.FINE,"[AbstractLoader] loadEjbs completed"); 592 593 return true; 594 } finally { 595 598 Utility.setContextClassLoader(savedContextClassLoader); 599 } 600 601 } 602 603 605 613 protected boolean unloadRars(boolean jsr77) { 614 boolean result = true; 615 try { 617 Set rarsDescriptors = this.application.getRarDescriptors(); 618 ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime(); 619 620 for (Iterator itr = rarsDescriptors.iterator(); itr.hasNext();) { 621 ConnectorDescriptor cd = (ConnectorDescriptor) itr.next(); 622 String rarName = cd.getDeployName(); 623 String jndiName = this.id+ 624 ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER+ 625 FileUtils.makeFriendlyFilenameNoExtension(rarName); 626 connectorRuntime.destroyActiveResourceAdapter(jndiName,cascade); 627 if (jsr77) { 628 deleteLeafMBean(cd); 629 } 630 } 631 } catch (Exception rarEx) { 632 result = false; 633 _logger.log(Level.WARNING, 634 "loader.application_loader_exception", rarEx); 635 } 636 637 _logger.log(Level.FINE,"[AbstractLoader] Unload RAR(s) Status: " 638 + result); 639 640 return result; 641 } 642 643 644 652 protected boolean loadRars(boolean jsr77) { 653 654 try { 655 656 String appLocation = this.configManager.getLocation(this.id); 657 Set rars = application.getRarDescriptors(); 658 ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime(); 659 660 for (Iterator itr = rars.iterator(); itr.hasNext();) { 661 ConnectorDescriptor cd = (ConnectorDescriptor) itr.next(); 662 String rarName = cd.getDeployName(); 663 String location = appLocation 664 + File.separator 665 + FileUtils.makeFriendlyFilename(rarName); 666 String jndiName = this.id+ 667 ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER+ 668 FileUtils.makeFriendlyFilenameNoExtension(rarName); 669 connectorRuntime.createActiveResourceAdapter( 670 cd,jndiName,location,this.cascade); 671 if (jsr77) { 672 createLeafMBean(cd); 673 } 674 } 675 try { 677 ConnectorResourcesLoader crl = new ConnectorResourcesLoader(ApplicationServer.getServerContext()); 678 crl.loadEmbeddedRarResources(id,getApplication()); 679 } catch (ConfigException cex) { 680 _logger.log(Level.SEVERE, "loader.connector_resource_initialization_error", cex); 681 } 682 } catch (Exception rarEx) { 683 _logger.log(Level.WARNING, 684 "loader.application_loader_exception", rarEx); 685 } 686 _logger.log(Level.FINE,"[AbstractLoader] loadRars completed"); 687 return true; 688 } 689 690 protected void setCascade(boolean cascade) { 691 this.cascade=cascade; 692 } 693 694 protected boolean getCascade() { 695 return this.cascade; 696 } 697 698 protected int getLoadUnloadAction(){ 699 return this.loadUnloadAction; 700 } 701 702 protected void setLoadUnloadAction(int loadUnloadAction) { 703 this.loadUnloadAction = loadUnloadAction; 704 } 705 706 708 716 protected boolean loadWebserviceEndpoints(boolean jsr77) { 717 718 boolean result = true; 719 720 if (this.application == null) { 721 result = false; 722 } else { 723 724 Set bundleSet = new HashSet (); 725 bundleSet.addAll(this.application.getEjbBundleDescriptors()); 726 bundleSet.addAll(this.application.getWebBundleDescriptors()); 727 728 Iterator itr = bundleSet.iterator(); 730 731 while ( itr.hasNext() ) { 732 BundleDescriptor nextDescriptor = (BundleDescriptor) 733 itr.next(); 734 735 try { 736 WSMonitorLifeCycleFactory.getInstance(). 737 getWSMonitorLifeCycleProvider(). 738 registerWebServiceEndpoints( 739 this.application.getRegistrationName(), nextDescriptor); 740 } catch (Exception e) { 741 _logger.log(Level.WARNING, 742 "loader.register_ws_endpoint_error", 743 this.application.getRegistrationName()); 744 _logger.log(Level.WARNING, 745 "loader.register_ws_endpoint_exception", e); 746 result = false; 747 } 748 749 760 761 _logger.log(Level.FINE, 762 "[AbstractLoader] Unload Web Service Endpoint(s) Status: " + 763 result); 764 } 765 } 766 767 return result; 768 } 769 770 778 protected boolean unloadWebserviceEndpoints(boolean jsr77) { 779 780 boolean result = true; 781 782 if (this.application == null) { 783 result = false; 784 } else { 785 786 Set bundleSet = new HashSet (); 788 bundleSet.addAll(this.application.getEjbBundleDescriptors()); 789 bundleSet.addAll(this.application.getWebBundleDescriptors()); 790 Iterator itr = bundleSet.iterator(); 791 792 while ( itr.hasNext() ) { 793 BundleDescriptor nextDescriptor = (BundleDescriptor) itr.next(); 794 795 try { 796 WSMonitorLifeCycleFactory.getInstance(). 797 getWSMonitorLifeCycleProvider(). 798 unregisterWebServiceEndpoints( 799 this.application.getRegistrationName(), nextDescriptor); 800 } catch (Exception e) { 801 _logger.log(Level.WARNING, 802 "loader.unregister_ws_endpoint_error", 803 this.application.getRegistrationName()); 804 _logger.log(Level.WARNING, 805 "loader.register_ws_endpoint_exception", e); 806 result = false; 807 } 808 809 820 821 _logger.log(Level.FINE, 822 "[AbstractLoader] Unload Web Service Endpoint(s) Status: " + 823 result); 824 } 825 } 826 827 return result; 828 } 829 830 public void generateEntryContext(Object context) { 831 ServerEntryHelper.generateAppLoaderEntryContext((Descriptor) context); 832 } 833 834 protected void notifyAppEvent(int eventType) { 835 ApplicationEvent event = new ApplicationEvent(eventType, 836 getApplication(), getClassLoader()); 837 generateEntryContext(getApplication()); 838 loaderEventNotifier.notifyListeners(event); 839 } 840 841 protected void notifyEjbEvent(int eventType, EjbDescriptor desc) { 842 EjbContainerEvent event = new EjbContainerEvent(eventType, 843 desc, getClassLoader()); 844 generateEntryContext(desc); 845 loaderEventNotifier.notifyListeners(event); 846 } 847 848 851 protected boolean loadPersistenceUnits() { 852 try{ 853 new PersistenceUnitLoaderImpl().load(new ApplicationInfoImpl()); 854 return true; 855 }catch(Exception ge){ 856 _logger.log(Level.WARNING, ge.getMessage(), ge); 857 return false; 858 } 859 } 860 861 864 protected boolean unloadPersistenceUnits() { 865 try{ 866 new PersistenceUnitLoaderImpl().unload(new ApplicationInfoImpl()); 867 return true; 868 }catch(Exception ge){ 869 _logger.log(Level.WARNING, ge.getMessage(), ge); 870 return false; 871 } 872 } 873 874 private class ApplicationInfoImpl 875 implements PersistenceUnitLoader.ApplicationInfo { 876 public Application getApplication() { 877 return application; 878 } 879 880 public InstrumentableClassLoader getClassLoader() { 881 return InstrumentableClassLoader.class.cast(ejbClassLoader); 882 } 883 884 public String getApplicationLocation() { 885 try { 886 return configManager.getLocation(id); 887 } catch (ConfigException e) { 888 throw new RuntimeException (e); 889 } 890 } 891 } 892 } 893 | Popular Tags |