FileDocCategorySizeDatePackage
AbstractLoader.javaAPI DocGlassfish v2 API36120Tue Jul 10 10:51:40 BST 2007com.sun.enterprise.server

AbstractLoader

public abstract class AbstractLoader extends Object implements EntryPoint
Base loader to load and unload applicaton and stand alone module.
author
Mahesh Kannan
author
Nazrul Islam
since
JDK1.4

Fields Summary
protected String
id
the registration name for this application
ClassLoader
parentClassLoader
parent class loader of ejb class loader
ClassLoader
ejbClassLoader
ejb class loader used by this application
com.sun.enterprise.deployment.Application
application
deployment descriptor object for this application
protected boolean
robFlag
protected ApplicationRegistry
registry
application registry
protected com.sun.enterprise.instance.BaseManager
configManager
encapsulates application related information
static Logger
_logger
logger to log loader messages
private com.sun.enterprise.security.factory.FactoryForSecurityManagerFactory
ffsmf
factory for SecurityManagerFactory
protected boolean
cascade
This variable tells whether to load/unload pools and resources recursively.
protected int
loadUnloadAction
Possible values for this: 0. LOAD_UNSET is before initialization For loading: 1. LOAD_ALL is for loading regular application 2. LOAD_RAR is for loading the rar part of the embedded rar 3. LOAD_REST is for loading the rest part of the embedded rar Embedded rar is loaded in two steps so we can create connector resources in between. For unloading: 4. UNLOAD_ALL is for unloading regular application 5. UNLOAD_RAR is for unloading the rar part of the embedded rar 6. UNLOAD_REST is for unloading the rest part of the embedded rar Embedded rar is unloaded in two steps so we can delete connector resources in between.
private com.sun.enterprise.server.event.ApplicationLoaderEventNotifier
loaderEventNotifier
private com.sun.enterprise.config.ConfigContext
dynamicConfigContext
Constructors Summary
AbstractLoader(String id, ClassLoader parentClassLoader, com.sun.enterprise.instance.BaseManager configMgr)
Constructor. ApplicationLoader loads an application or stand alone ejb module.

param
id the registration name of this application
param
parentClassLoader the parent class loader
param
configMgr the config manager for this VS


                                                                      
        
               

        this.id                     = id;
        this.parentClassLoader      = parentClassLoader;
        this.registry               = ApplicationRegistry.getInstance();
        this.configManager          = configMgr;

	this.loaderEventNotifier =
	    ApplicationLoaderEventNotifier.getInstance();
    
Methods Summary
abstract voidcreateLeafMBean(com.sun.enterprise.deployment.Descriptor descriptor)
Create jsr77 mBean for the leaf object The leaf mBean correspond to the child object of rootMBean. For an ejb stand-alone module it will be ejb and for a web stand-alone module it will be servlet and for a connector stand-alone module it will be resource adapter

param
descriptor descriptor object for the leaf element

abstract voidcreateLeafMBeans()
Create jsr77 mBeans for components within root mBean The leaf mBeans correspond to the child objects of rootMBean. For an ejb stand-alone module it will be ejbs and for a web stand-alone module it will be servlets and for a connector stand-alone module it will be resource adapters

abstract voidcreateRootMBean()
Create jsr77 root mBean The root mBean corresponds to either an application or stand-alone module depending on j2ee type

abstract voiddeleteLeafAndRootMBeans()
Delete jsr77 mBeans for the root and its' components

abstract voiddeleteLeafMBean(com.sun.enterprise.deployment.Descriptor descriptor)
Delete jsr77 mBean for the leaf object The leaf mBean correspond to the child object of rootMBean. For an ejb stand-alone module it will be ejb and for a web stand-alone module it will be servlet and for a connector stand-alone module it will be resource adapter

abstract voiddeleteLeafMBeans()
Delete jsr77 mBeans for components within root mBean The leaf mBeans correspond to the child objects of rootMBean. For an ejb stand-alone module it will be ejbs and for a web stand-alone module it will be servlets and for a connector stand-alone module it will be resource adapters

abstract voiddeleteRootMBean()
Delete jsr77 root mBean The root mBean corresponds to either an application or stand-alone module depending on j2ee type

abstract booleandoLoad(boolean jsr77)
Loads a deployed application or stand alone module. If jsr77 is true then corresponding jsr77 mBeans will be created during load time.

param
jsr77 create jsr77 mbeans if true

protected voiddone()
Helps garbage collector by assigning member variables to null. This is called from unload.

see
#unload


        // releases resources (file handles, etc) in the class loader
        if (this.ejbClassLoader instanceof EJBClassLoader) {
            ((EJBClassLoader) ejbClassLoader).done();
        }
        
        // set connector descriptor classloaders to null
        Set rars = this.application.getRarDescriptors();
        for (Iterator itr = rars.iterator(); itr.hasNext();) {
            ConnectorDescriptor cd = (ConnectorDescriptor) itr.next();
            cd.setClassLoader(null);
        }
        
        this.id                 = null;
        this.parentClassLoader  = null;
        this.application        = null;
        this.ejbClassLoader     = null;
        this.registry           = null;
        this.configManager      = null;
    
public voidgenerateEntryContext(java.lang.Object context)

         ServerEntryHelper.generateAppLoaderEntryContext((Descriptor) context);
    
com.sun.enterprise.deployment.ApplicationgetApplication()
Returns the deployment descriptor object used by this loader.

return
the deployment descriptor object

        return this.application;
    
protected booleangetCascade()

        return this.cascade;
    
java.lang.ClassLoadergetClassLoader()
Returns the ejb class loader used by this loader.

return
the ejb class loader

        return this.ejbClassLoader;
    
com.sun.enterprise.config.ConfigContextgetConfigContext()

        return dynamicConfigContext;
    
protected intgetLoadUnloadAction()

        return this.loadUnloadAction;
    
protected voidinitializeLoader(java.lang.String[] classPaths, java.lang.String moduleRoot, javax.enterprise.deploy.shared.ModuleType moduleType)
Initializes this loader by creating the ejb class loader.

param
classPaths class paths for the ejb class loader

        this.ejbClassLoader = EJBClassPathUtils.createEJBClassLoader(
                        classPaths, moduleRoot, this.id, this.parentClassLoader, moduleType);
    
protected voidinitializeLoader(java.lang.ClassLoader clazzloader)
Initializes this loader to the given classloader

        this.ejbClassLoader = clazzloader;
    
booleanload(boolean jsr77)
Loads a deployed application or stand alone module. Verifies that the module's directory exists and is valid before trying to load the module, so as to display a useful warning in the log without displaying a stack trace from later failures (such as failed attempts to load classes from the module). If jsr77 is true then corresponding jsr77 mBeans will be created during load time.

param
jsr77 create jsr77 mbeans if true

        return (verifyModuleDirectory() && doLoad(jsr77));
    
protected booleanloadEjbs(boolean jsr77)
Creates the bean containers for all ejbs. If jsr77 is true then corresponding jsr77 mBeans will be created during load time.

return
true if all modules deployed successfully


        // application.getApplicationArchivist().getClassLoader();
        final ClassLoader loader = this.ejbClassLoader;

        if (this.application == null) {
            return false;
        }
        
        if (_logger.isLoggable(Level.FINEST)) {
            _logger.log(Level.FINEST, 
                        "[AbstractLoader] Id: " + this.id +
                        " Setting thread context class loader to: " + loader);
        }

        /*
         *5003242 - Save current context class loader as it is changed.  Restored in finally block of next try.
        */
        ClassLoader savedContextClassLoader = Utility.setContextClassLoader(loader);

      try {
        if (!application.isVirtual()) {
            // adds the ejb class loader to the repository
            this.registry.addAppId2ClassLoader(this.id, this.ejbClassLoader);
        } else {
            this.registry.addModuleId2ClassLoader(this.id, this.ejbClassLoader);
        }

        // adds the deployment descriptor obj to the repository
        this.registry.addClassLoader2Application(this.ejbClassLoader, 
                                                 this.application);

        HashSet myContainers = new HashSet();

        if (_logger.isLoggable(Level.FINEST)) {
            _logger.log(Level.FINEST,
                        "[AbstractLoader] Id: " + this.id 
                        + " Unique Id: " + this.application.getUniqueId());
        }
       
        Vector beanDescriptors = application.getEjbDescriptors();

        // create EJB containers for each EJB
        Enumeration e = beanDescriptors.elements(); 
        ContainerFactory cf = Switch.getSwitch().getContainerFactory();

        while (e.hasMoreElements()) {            
            EjbDescriptor nextDescriptor = (EjbDescriptor) e.nextElement();

            // detects any unique id collisions
            if ( !this.registry.isUnique( nextDescriptor.getUniqueId() ) ) {

                _logger.log(Level.SEVERE, "loader.duplicate_unique_id",
                            new Object[] {
                              String.valueOf(nextDescriptor.getUniqueId()),
                              nextDescriptor.getName()
                            });

                // unload all the ejbs
                unloadEjbs(jsr77);

                // abort loading
                return false;
            }

            try {
		if(ffsmf == null){
		    ffsmf = FactoryForSecurityManagerFactoryImpl.getInstance();
		}
		SecurityManagerFactory smf = ffsmf.getSecurityManagerFactory("ejb");
		SecurityManager sm = smf.createSecurityManager(nextDescriptor);
                // create the bean container
                Container container = null;
                try {
		    notifyEjbEvent(EjbContainerEvent.BEFORE_EJB_CONTAINER_LOAD,
			nextDescriptor);

                    container = cf.createContainer(nextDescriptor, loader,
					sm, dynamicConfigContext);

		    notifyEjbEvent(EjbContainerEvent.AFTER_EJB_CONTAINER_LOAD,
			nextDescriptor);
                } catch (NameAlreadyBoundException jndiEx) {

                    // logs a message
                    _logger.log(Level.SEVERE, "loader.jndi_name_conflict",
                                new Object[] {
                                    this.id, 
                                    nextDescriptor.getJndiName(),
                                    nextDescriptor.getName()
                                });

                    // logs the actual exception
                    _logger.log(Level.SEVERE,
                        "loader.naming_exception_in_createcontainer", jndiEx);

                    // remove all loaded ejbs
                    unloadEjbs(jsr77);

                    // abort loading
                    return false;
                }

                if (_logger.isLoggable(Level.FINE)) {
                    _logger.log(Level.FINE, 
                                "Created container with uinque id: " 
                                + nextDescriptor.getUniqueId());
                }

                // adds the container to the repository
                this.registry.addDescriptor2Container(nextDescriptor,container);

                // adds the container to the temp local table
                myContainers.add(container);

            } catch (Exception beanEx) {
                _logger.log(Level.SEVERE,
                    "loader.unexpected_error_while_creating_ejb_container",
                    beanEx);

                // remove all loaded ejbs
                unloadEjbs(jsr77);

                return false;
            }

            if (jsr77) {
              try {
                createLeafMBean(nextDescriptor);
              } catch (MBeanException mbe) {
                _logger.log(Level.WARNING, "loader.create_ejb_mbean_exception", mbe);
              }
            }
        }

	try {
            Enumeration en = beanDescriptors.elements();
            // generate policy files for applications with ejbs not otherwise
            while (en.hasMoreElements()) {            
                EjbDescriptor nextDescriptor = (EjbDescriptor)en.nextElement();
                SecurityUtil.generatePolicyFile(
                    EJBSecurityManager.getContextID(nextDescriptor));
            }
	} catch (Exception ge) {
            _logger.log(Level.SEVERE, 
			"code.could_not_generate/load_the_policy_files_for_system_app",
			ge);

	    // remove all loaded ejbs
	    unloadEjbs(jsr77);

	    return false;
	}
 
        // notify ejb containers that application deployment succeeded.
        Iterator iter = myContainers.iterator();
        while ( iter.hasNext() ) {
            Container c = (Container) iter.next();
            c.doAfterApplicationDeploy();
            //IASRI 4717059 BEGIN
            /*
            if (robFlag) {
                if ( c instanceof EntityContainer) {
                    ( (EntityContainer) c).setROBNotifier(robNotifier);
                }
            }
            */
            //IASRI 4717059 END
        }

        // diagnostics of all the roles and acls 
        com.sun.enterprise.security.Audit.showACL(this.application);

        // log that ejbs were loaded
        if (this.application.getEjbComponentCount() > 0) {
            _logger.log(Level.INFO, "loader.ejbs_loaded", this.id);
        }

        _logger.log(Level.FINE,"[AbstractLoader] loadEjbs completed");

        return true;
      } finally {
            /*
             *5003242 - Restore saved context class loader.
             */
            Utility.setContextClassLoader(savedContextClassLoader);
      }
            
    
protected booleanloadPersistenceUnits()
Loads all the EJB 3.0 persistence entities bundled in this application.

        try{
            new PersistenceUnitLoaderImpl().load(new ApplicationInfoImpl());
            return true;
        }catch(Exception ge){
            _logger.log(Level.WARNING, ge.getMessage(), ge);
            return false;
        }
    
protected booleanloadRars(boolean jsr77)
Load the rars of that application If jsr77 is true then corresponding jsr77 mBeans will be created during load time.

param
jsr77 delete jsr77 mBeans if true
return
true if all modules deployed successfully

        
        try {

            String appLocation = this.configManager.getLocation(this.id);
            Set rars = application.getRarDescriptors();
            ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();

            for (Iterator itr = rars.iterator(); itr.hasNext();) {
                ConnectorDescriptor cd = (ConnectorDescriptor) itr.next();
                String rarName = cd.getDeployName();
                String location = DeploymentUtils.getEmbeddedModulePath(
                    appLocation, rarName);
                String jndiName = this.id+
                       ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER+
                       FileUtils.makeFriendlyFilenameNoExtension(rarName);
                connectorRuntime.createActiveResourceAdapter(
                              cd,jndiName,location);
                if (jsr77) {
                    createLeafMBean(cd);
                }
            }
            // now load resources associated with these embedded RARs
            try {
                ConnectorResourcesLoader crl = new ConnectorResourcesLoader();
                crl.loadEmbeddedRarResources(id,getApplication());
            } catch (ConfigException cex) {
                _logger.log(Level.SEVERE, "loader.connector_resource_initialization_error", cex);   
            }
        } catch (Exception rarEx) {
            _logger.log(Level.WARNING,
                        "loader.application_loader_exception", rarEx);
        }
        _logger.log(Level.FINE,"[AbstractLoader] loadRars completed");
        return true;
    
protected booleanloadWebserviceEndpoints(boolean jsr77)
Loads all the Web Service Management MBeans. If jsr77 is true then corresponding jsr77 mBeans will be created during load time.

param
jsr77 create jsr77 mBeans if true
return
true if unloaded successfully


        boolean result = true;

        if (this.application == null) {
            result = false;
        } else {

            Set bundleSet = new HashSet();
            bundleSet.addAll(this.application.getEjbBundleDescriptors());
            bundleSet.addAll(this.application.getWebBundleDescriptors());

            // unload web service endpoint
            Iterator itr  = bundleSet.iterator();

            while ( itr.hasNext() ) {
                BundleDescriptor nextDescriptor = (BundleDescriptor) 
                        itr.next();

                try {
                    WSMonitorLifeCycleFactory.getInstance().
                    getWSMonitorLifeCycleProvider().
                    registerWebServiceEndpoints(
                    this.application.getRegistrationName(), nextDescriptor);
                } catch (Exception e) {
                  _logger.log(Level.WARNING, 
                  "loader.register_ws_endpoint_error", 
                  this.application.getRegistrationName());
                  _logger.log(Level.WARNING, 
                  "loader.register_ws_endpoint_exception", e);
                   result = false;
                }

                /*
                if (jsr77) {
                      try {
                            createLeafMBeans(nextDescriptor);
                          } catch (MBeanException mbe) {
                                mbe.printStackTrace();
                              _logger.log(Level.WARNING, 
                              "loader.create_ejb_mbean_exception", mbe);
                          }
                }
                */

                _logger.log(Level.FINE,
                 "[AbstractLoader] Unload Web Service Endpoint(s) Status: " + 
                 result);
            }
        }

        return result;
    
protected voidnotifyAppEvent(int eventType)

	ApplicationEvent event = new ApplicationEvent(eventType,
		getApplication(), getClassLoader(), dynamicConfigContext);
        generateEntryContext(getApplication());
	loaderEventNotifier.notifyListeners(event);
    
protected voidnotifyEjbEvent(int eventType, com.sun.enterprise.deployment.EjbDescriptor desc)

	EjbContainerEvent event = new EjbContainerEvent(eventType,
		desc, getClassLoader());
        generateEntryContext(desc);
	loaderEventNotifier.notifyListeners(event);
    
protected voidsetCascade(boolean cascade)

        this.cascade=cascade;
    
voidsetConfigContext(com.sun.enterprise.config.ConfigContext dynamicConfigContext)

	this.dynamicConfigContext = dynamicConfigContext;
    
protected voidsetLoadUnloadAction(int loadUnloadAction)

        this.loadUnloadAction = loadUnloadAction;
    
abstract voidsetState(int state)
Set state for jsr77 root mBean The state can be set for a root mBean and it could be one of STARTING, RUNNING, STOPPING, STOPPED, or FAILED

param
state state of the module or application

abstract booleanunload(boolean jsr77)
Unloads a deployed application or stand alone module. If jsr77 is true then corresponding jsr77 mBeans will be deleted during unload time.

param
jsr77 delete jsr77 mbeans if true

protected booleanunloadEjbs(boolean jsr77)
Unloads all the ejb bean containers. If jsr77 is true then corresponding jsr77 mBeans will be deleted during unload time.

param
jsr77 delete jsr77 mBeans if true
return
true if unloaded successfully


        boolean result = true;

        if (this.application == null) {
            result = false;
        } else {

            // undeploy ejb module
            Vector beanDescriptors  = this.application.getEjbDescriptors();
            Enumeration e           = beanDescriptors.elements(); 

            while ( e.hasMoreElements() ) {
                EjbDescriptor nextDescriptor = (EjbDescriptor) e.nextElement();

		notifyEjbEvent(EjbContainerEvent.BEFORE_EJB_CONTAINER_UNLOAD,
		    nextDescriptor);

                // removes the unique id from the registry
                this.registry.removeUniqueId( nextDescriptor.getUniqueId() );

                try {
                    // removes the bean container from the application registry
                    Container container = (Container) this.registry.
                            removeDescriptor2Container(nextDescriptor);
                    if (container != null) {
                        container.undeploy();
                    }

                } catch (Exception beanEx) {
                    result = false;
                    _logger.log(Level.WARNING,
                        "loader.unexpected_error_while_unloading_ejb_container",
                        beanEx);
                }

		notifyEjbEvent(EjbContainerEvent.AFTER_EJB_CONTAINER_UNLOAD,
		    nextDescriptor);

                if (jsr77) {
                          try {
                            deleteLeafMBean(nextDescriptor);
                          } catch (MBeanException mbe) {
                              _logger.log(Level.WARNING, 
                              "loader.delete_ejb_mbean_exception", mbe);
                          }
                }
            }

            if (!application.isVirtual()) {
                // removes the class loader from the application registry
                this.registry.removeAppId2ClassLoader(this.id);
            } else {
                this.registry.removeModuleId2ClassLoader(this.id);
            }

            // removes the descriptor object from the application registry
            this.registry.removeClassLoader2Application(this.ejbClassLoader);

        }

        _logger.log(Level.FINE,"[AbstractLoader] Unload EJB(s) Status: "
                    + result);

        return result;
    
protected booleanunloadPersistenceUnits()
Unloads all the EJB 3.0 persistence entities bundled in this application.

        try{
            new PersistenceUnitLoaderImpl().unload(new ApplicationInfoImpl());
            return true;
        }catch(Exception ge){
            _logger.log(Level.WARNING, ge.getMessage(), ge);
            return false;
        }
    
protected booleanunloadRars(boolean jsr77)
Unloads all the rar. If jsr77 is true then corresponding jsr77 mBeans will be deleted during unload time.

param
jsr77 delete jsr77 mBeans if true
return
true if unloaded successfully

        boolean result = true;
        // undeploy rar module
        try {
            Set rarsDescriptors  = this.application.getRarDescriptors();
            ConnectorRuntime connectorRuntime = ConnectorRuntime.getRuntime();
        
            for (Iterator itr = rarsDescriptors.iterator(); itr.hasNext();) {
                ConnectorDescriptor cd = (ConnectorDescriptor) itr.next();
                String rarName = cd.getDeployName();
                String jndiName = this.id+
                         ConnectorConstants.EMBEDDEDRAR_NAME_DELIMITER+
                         FileUtils.makeFriendlyFilenameNoExtension(rarName);
                connectorRuntime.destroyActiveResourceAdapter(jndiName,cascade);
                if (jsr77) {
                     deleteLeafMBean(cd);
                }
            }
        } catch (Exception rarEx) {
            result = false;
            _logger.log(Level.WARNING,
                        "loader.application_loader_exception", rarEx);
        } 

        _logger.log(Level.FINE,"[AbstractLoader] Unload RAR(s) Status: " 
                    + result);

        return result;
    
protected booleanunloadWebserviceEndpoints(boolean jsr77)
Unloads all the Web Service Management MBeans. If jsr77 is true then corresponding jsr77 mBeans will be deleted during unload time.

param
jsr77 delete jsr77 mBeans if true
return
true if unloaded successfully


        boolean result = true;

        if (this.application == null) {
            result = false;
        } else {

            // unload web service endpoint
            Set bundleSet = new HashSet();
            bundleSet.addAll(this.application.getEjbBundleDescriptors());
            bundleSet.addAll(this.application.getWebBundleDescriptors());
            Iterator itr  = bundleSet.iterator();

            while ( itr.hasNext() ) {
                BundleDescriptor nextDescriptor = (BundleDescriptor) itr.next();

                try {
                    WSMonitorLifeCycleFactory.getInstance().
                    getWSMonitorLifeCycleProvider().
                    unregisterWebServiceEndpoints(
                    this.application.getRegistrationName(), nextDescriptor);
                } catch (Exception e) {
                  _logger.log(Level.WARNING, 
                  "loader.unregister_ws_endpoint_error", 
                  this.application.getRegistrationName());
                  _logger.log(Level.WARNING, 
                  "loader.register_ws_endpoint_exception", e);
                   result = false;
                }

                /*
                if (jsr77) {
                    try {
                        deleteLeafMBeans(nextDescriptor);
                    } catch (MBeanException mbe) {
                                mbe.printStackTrace();
                        _logger.log(Level.WARNING, 
                        "loader.delete_ejb_mbean_exception", mbe);
                    }
                }
                */

                _logger.log(Level.FINE,
                "[AbstractLoader] Unload Web Service Endpoint(s) Status: " + 
                result);
            }
        }

        return result;
    
private booleanverifyModuleDirectory()

        String moduleDirectory;
        try {
            moduleDirectory = configManager.getLocation(id);
        } catch (ConfigException ce) {
            _logger.log(Level.WARNING, "loader.configexception", ce);
            return false;
        }
        boolean result = FileUtils.safeIsDirectory(moduleDirectory);
        if ( ! result) {
            /*
             *Log a warning message without a stack trace.
             */
            _logger.log(Level.WARNING, "loader.module_dir_error", new Object[] {id, moduleDirectory});
        }
        return result;