FileDocCategorySizeDatePackage
ApplicationManager.javaAPI DocGlassfish v2 API33641Fri May 04 22:35:40 BST 2007com.sun.enterprise.server

ApplicationManager

public class ApplicationManager extends AbstractManager implements com.sun.enterprise.admin.event.ApplicationDeployEventListener
Applicaton Manager manages all j2ee application for a server instance. It also acts as an listener for the deployment events. All the hot application deployment events are handled in this manager.
author
Mahesh Kannan
author
Nazrul Islam
since
JDK1.4

Fields Summary
static Logger
_logger
logger to log core messages
private static com.sun.enterprise.util.i18n.StringManager
localStrings
local string manager
private static final String
RAR
Constructors Summary
ApplicationManager(com.sun.enterprise.instance.AppsManager appsManager, ClassLoader connectorClassLoader)
ApplicationManager is responsible for loading all applications in an instance. It creates a ApplicationLoader for each deployed application. ApplicationLader is responsible for further loading EJBContainers for each module in the Application.

It is assumed that the next two params are set by the Startup Code.

param
appsManager The appsManager obtained through ConfigFactory.createAppsManager(ConfigCtx)
param
commonclassLoader The top level class loader. For now this can be the MainThread's ClassLoader.


                                                                                                                                            
      
              

        super(connectorClassLoader, appsManager);

        AdminEventListenerRegistry.addApplicationDeployEventListener(this);
        
        /* Make sure the manager is alive to receive all start-up load events. */
        AppclientJWSSupportManager.getInstance();
    
Methods Summary
public synchronized voidapplicationDeployed(com.sun.enterprise.admin.event.ApplicationDeployEvent event)

        try {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                "[ApplicationManager] Handling event " + event.toString());
            }

            DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event);

            // refreshes the config context with the context from this event
            this.configManager.refreshConfigContext( event.getConfigContext() );

            // set jsr77 flag
            boolean jsr77 = false;
            String action = event.getAction();
            if ( (action.equals(BaseDeployEvent.DEPLOY)) ||
                 (action.equals(BaseDeployEvent.REDEPLOY)) ) {
                jsr77 = true;
            }

            String appName = event.getApplicationName();

            //If forceDeploy=true [ie a Redeploy] we need loadEmbeddedRARconfigs 
            //first and then deploy the application. After successful 
            //application Deployment, all embedded connector resources 
            //needs to be loaded
            ConnectorResourcesLoader connecorResourcesLoader  = null;
            if(event.getForceDeploy()) {
                //The undeploy event that had  been executed earlier
                //as part of the deploy --force=true action will remove the
                //application node from domain.xml.
                //Therefore refresh the ResourcesUtil singleton with
                //this event's config context so that it can 
                //find the application node while loading embedded RAR configs
                //and connector resources.
                ResourcesUtil.setEventConfigContext(event.getConfigContext());
                connecorResourcesLoader =  new ConnectorResourcesLoader();

                // do this before loading rar
                if (event.getLoadUnloadAction() == Constants.LOAD_RAR) {
                    connecorResourcesLoader.loadEmbeddedRarRAConfigs(appName);
                }
            }

            if (isEnabled(event.getConfigContext(), appName) && 
                !applicationDeployed(jsr77, appName, event.getConfigContext(), 
                 event.getLoadUnloadAction()) ) {
                    
                String msg = localStrings.getString(
                    "applicationmgr.application_deployed_failed", appName);
                registerException(event, msg);
            }
            
            // do this after rest of embedded rar is loaded
            if(event.getLoadUnloadAction() == Constants.LOAD_REST && 
                event.getForceDeploy()) {
                AbstractLoader appLoader = (AbstractLoader)id2loader.get(appName);
                if (appLoader != null) {
                    Application appDescriptor = appLoader.getApplication();
                    connecorResourcesLoader.loadEmbeddedRarResources(appName,appDescriptor);           
                }
            }
        } catch (ConfigException ce) {
            throw new AdminEventListenerException(ce.getMessage()); 
        } finally {
            ResourcesUtil.resetEventConfigContext();
        }
    
booleanapplicationDeployed(java.lang.String appID)
Loads the given application if enabled. This adds the application to the reload monitor list. This gets called when application is deployed.

param
appID registration name of the application
return
true if application is loaded successfully

        return applicationDeployed(false, appID, true);
    
booleanapplicationDeployed(boolean jsr77, java.lang.String appID)
Loads the given application if enabled. This adds the application to the reload monitor list. This gets called when application is deployed.

param
appID registration name of the application
param
jsr77 create jsr77 mBeans if it is true
return
true if application is loaded successfully

        return applicationDeployed(jsr77, appID, true);
    
booleanapplicationDeployed(boolean jsr77, java.lang.String appID, com.sun.enterprise.config.ConfigContext dynamicConfigContext)

        return applicationDeployed(jsr77, appID, true,
	    dynamicConfigContext);
    
booleanapplicationDeployed(boolean jsr77, java.lang.String appID, com.sun.enterprise.config.ConfigContext dynamicConfigContext, int loadUnloadAction)

        return applicationDeployed(jsr77, appID, true,
            dynamicConfigContext, loadUnloadAction);
    
booleanapplicationDeployed(boolean jsr77, java.lang.String appID, boolean addToRM)
Loads the given application if enabled.

param
jsr77 create jsr77 mBeans if it is true
param
appID registration name of the application
param
addToRM if true, adds this application to reload
return
true if application is loaded successfully

	return applicationDeployed(jsr77, appID, addToRM, null);
    
booleanapplicationDeployed(boolean jsr77, java.lang.String appID, boolean addToRM, com.sun.enterprise.config.ConfigContext dynamicConfigContext)

 
        return applicationDeployed(jsr77, appID, addToRM, dynamicConfigContext, 
            Constants.LOAD_ALL);
    
booleanapplicationDeployed(boolean jsr77, java.lang.String appID, boolean addToRM, com.sun.enterprise.config.ConfigContext dynamicConfigContext, int loadUnloadAction)


        boolean deployed = false;
        boolean loadJSR77 = jsr77 || loadJSR77(appID, DeployableObjectType.APP);
        AbstractLoader appLoader = null;
        try {
            if (this.configManager.isSystem(appID)) {
                    return true;
            }
        } catch (ConfigException confEx) {
             _logger.log(Level.WARNING,
                         "core.error_while_loading_app", confEx);
        }

        try {
            //set default value
            if (loadUnloadAction == Constants.LOAD_UNSET) {
                loadUnloadAction = Constants.LOAD_ALL;
            }

            boolean handlePreLoadAction = 
                (loadUnloadAction == Constants.LOAD_ALL ||
                 loadUnloadAction == Constants.LOAD_RAR); 

            boolean handlePostLoadAction = 
                (loadUnloadAction == Constants.LOAD_ALL ||
                 loadUnloadAction == Constants.LOAD_REST); 

            // adds the app to be monitored if ON
            if (handlePreLoadAction) {
                if (addToRM) {
                    addToReloadMonitor(appID);
                }
            }

            // Check to see if the app is already loaded.  If yes, do not 
            // reload. 
            if (id2loader.get(appID) != null) {
                    return true;
            }

            // create new loader for loading rars or loading non-embedded-rar 
            // application
            // and use the cached loader for the rest of the loading for 
            // embedded rar
            if (handlePreLoadAction) {
                appLoader = getLoader(appID);
                /**
                 *Add the apploader to the map now so that it can be cleaned 
                 *up later even if the load fails.
                 */
                if (loadUnloadAction == Constants.LOAD_ALL) {
                    id2loader.put(appID, appLoader);
                } else if (loadUnloadAction == Constants.LOAD_RAR) {
                    id2loader.put(appID + RAR, appLoader);
                }
            } else if (loadUnloadAction == Constants.LOAD_REST) {
                appLoader = (AbstractLoader) id2loader.remove(appID + RAR);
                if (appLoader != null) {
                    id2loader.put(appID, appLoader);
                } else {
                    return false;
                }
            }

	    appLoader.setConfigContext(dynamicConfigContext);
	    appLoader.setLoadUnloadAction(loadUnloadAction);

            // create jsr77 root Mbean for this application
            // only do this for second part of the loading
            if (handlePreLoadAction && loadJSR77) {
                try {
                    appLoader.createRootMBean();
                } catch (MBeanException mbe) {
                    _logger.log(Level.WARNING,"core.error_while_creating_jsr77_root_mbean",mbe);
                }
            }

            if (isEnabled(dynamicConfigContext, appID)) {

                _logger.log(Level.FINEST,
                    "[ApplicationManager] Application is enabled: " + appID);


                // only do this for first part of the loading
                if (handlePreLoadAction) {
                    // set jsr77 state to STARTING
                    try {
                        appLoader.setState(StateManageable.STARTING_STATE);
                    } catch (MBeanException mbe) {
                        _logger.log(Level.WARNING, 
                            "core.error_while_setting_jsr77_state",mbe);
                    }
                }

                boolean retSts = appLoader.load(jsr77);
                deployed = retSts;

                if (handlePostLoadAction && retSts) {

                    // set jsr77 state to RUNNING
                    try {
                        appLoader.setState(StateManageable.RUNNING_STATE);
                    } catch (MBeanException mbe) {
                        _logger.log(Level.WARNING, 
                        "core.error_while_setting_jsr77_state",mbe);
                    }

                					
                    // application loaded 
                    deployed = true;

                    SecurityRoleMapperFactory factory = SecurityRoleMapperFactoryMgr.getFactory();

                    if (factory==null) {
                        throw new IllegalArgumentException(localStrings.getString(
                        "enterprise.deployment.deployment.norolemapperfactorydefine",
                        "This application has no role mapper factory defined"));
                    }

                } else if (!retSts) {
                    if (handlePreLoadAction && loadJSR77) {
                        // delete leaf and root mBeans for this app
                        try {
                            appLoader.deleteLeafAndRootMBeans();
                        } catch (MBeanException mbe) {
                            _logger.log(Level.WARNING,
                            "core.error_while_deleting_jsr77_leaf_and_root_mbean",mbe);
                        }
                    } else if (!loadJSR77){
                        // set jsr77 state to FAILED
                        try {
                            appLoader.setState(StateManageable.FAILED_STATE);
                        } catch (MBeanException mbe) {
                            _logger.log(Level.WARNING, 
                            "core.error_while_setting_jsr77_state",mbe);
                        }
                    }
                    /*
                     *Release the app loader's resources so jar files can be
                     *closed.
                     */
                    try {
                        appLoader.done();
                    } catch (Throwable thr) {
                        String msg = localStrings.getString("core.application_not_loaded", appID);
                        _logger.log(Level.INFO, msg, thr);
                    }
                    _logger.log(Level.WARNING, 
                                "core.application_not_loaded", appID);
                }

            } else {
                _logger.log(Level.INFO, "core.application_disabled", appID);
                deployed = false;
            }
        } catch (ConfigException confEx) {
            _logger.log(Level.WARNING, "core.error_while_loading_app", confEx);
        } finally {
	    //To ensure that the config context is not reused...
	    if (appLoader != null) {
		appLoader.setConfigContext(null);
	    }
	}

        return deployed;
    
public synchronized voidapplicationDisabled(com.sun.enterprise.admin.event.ApplicationDeployEvent event)


        try {
            if (_logger.isLoggable(Level.FINEST)) {
               _logger.log(Level.FINEST,
                    "[ApplicationManager] Handling event " + event.toString());
            }

            // refreshes the config context with the context from this event
            this.configManager.refreshConfigContext( event.getConfigContext() );

            String appName = event.getApplicationName();
            AbstractLoader appLoader = (AbstractLoader) id2loader.get(appName);
	    if(appLoader == null) {
                if (_logger.isLoggable(Level.FINEST)) {
                   _logger.log(Level.FINEST,
                        "[ApplicationManager] appLoader Null. Returning applicationDisabled");
                }
		return;

	    }
            event.setCascade(true); 
            boolean disabled = applicationUndeployed(false, appName,true,true);

            if (!disabled) {
                String msg = localStrings.getString(
                        "applicationmgr.application_disabled_failed", appName);
                registerException(event, msg);
            }
        } catch(ConfigException ce) {
            throw new AdminEventListenerException(ce.getMessage()); 
        }
    
public synchronized voidapplicationEnabled(com.sun.enterprise.admin.event.ApplicationDeployEvent event)


        try {
            if (_logger.isLoggable(Level.FINEST)) {
               _logger.log(Level.FINEST,
                    "[ApplicationManager] Handling event " + event.toString());
            }

            //return if the app is not supposed to be enabled
            if (!isEnabled(event.getConfigContext(), event.getApplicationName())) {
                return;
            }

            // refreshes the config context with the context from this event
            this.configManager.refreshConfigContext( event.getConfigContext() );

            String appName = event.getApplicationName();
            boolean cascade = true;
            ConnectorResourcesLoader connecorResourcesLoader = 
                      new ConnectorResourcesLoader(); 
            ResourcesUtil.setEventConfigContext(event.getConfigContext());
 
            connecorResourcesLoader.loadEmbeddedRarRAConfigs(appName);
            boolean enabled = applicationDeployed(false, appName, event.getConfigContext());

            if (!enabled) {
                String msg = localStrings.getString(
                        "applicationmgr.application_enabled_failed", appName);
                registerException(event, msg);
            }
           
            AbstractLoader appLoader = 
                (AbstractLoader)id2loader.get(appName);
            Application appDescriptor = appLoader.getApplication();
            if (appDescriptor != null) {
                connecorResourcesLoader.loadEmbeddedRarResources(
                            appName,appDescriptor);
            } else {
                _logger.log(Level.FINE,
                "[ApplicationManager] Application descriptor is NULL.  Skip loading embedded rar resources...");
            }
        } catch(ConfigException ce) {
            throw new AdminEventListenerException(ce.getMessage()); 
        } catch(Throwable th) {
            AdminEventListenerException aele =
                           new AdminEventListenerException(th.getMessage());
            aele.initCause(th);
            throw aele;
        }finally {
            ResourcesUtil.resetEventConfigContext();
        }
    
public synchronized voidapplicationRedeployed(com.sun.enterprise.admin.event.ApplicationDeployEvent event)


        try {
            if (_logger.isLoggable(Level.FINEST)) {
               _logger.log(Level.FINEST,
                    "[ApplicationManager] Handling event " + event.toString());
            }

            // refreshes the config context with the context from this event
            this.configManager.refreshConfigContext( event.getConfigContext() );

            String appName = event.getApplicationName();
            boolean ok = applicationUndeployed(true, appName);
            if (ok) {
                ok = applicationDeployed(true, appName, event.getConfigContext());
            }

            if (!ok) {
                String msg = localStrings.getString(
                    "applicationmgr.application_redeployed_failed", appName);
                registerException(event, msg);
            }
        } catch(ConfigException ce) {
            throw new AdminEventListenerException(ce.getMessage()); 
        }
    
public voidapplicationReferenceAdded(com.sun.enterprise.admin.event.ApplicationDeployEvent event)
Invoked when an application reference is created from a server instance (or cluster) to a particular application.

throws
AdminEventListenerException when the listener is unable to process the event.

    
public voidapplicationReferenceRemoved(com.sun.enterprise.admin.event.ApplicationDeployEvent event)
Invoked when a reference is removed from a server instance (or cluster) to a particular application.

throws
AdminEventListenerException when the listener is unable to process the event.

                
    
booleanapplicationUndeployed(java.lang.String appID)
Unloads the given application. This removes the application from reload monitor list. This gets called when application is undeployed.

param
appID registration name of the application
return
true if application is unloaded successfully

        return applicationUndeployed(false, appID, true);
    
booleanapplicationUndeployed(boolean jsr77, java.lang.String appID)
Unloads the given application. This removes the application from reload monitor list. This gets called when application is undeployed.

param
jsr77 deletes jsr77 mBeans if it is true
param
appID registration name of the application
return
true if application is unloaded successfully

        return applicationUndeployed(jsr77, appID, true);
    
booleanapplicationUndeployed(boolean jsr77, java.lang.String appID, boolean clearRM)
Unloads the given application.

param
jsr77 delete jsr77 mBeans if it is true
param
appID registration name of the application
param
clearRM if true, removes this app from reload monitor
return
true if application is unloaded successfully

	    return applicationUndeployed(jsr77, appID,clearRM, false);
	
booleanapplicationUndeployed(boolean jsr77, java.lang.String appID, boolean clearRM, boolean cascade)


            return applicationUndeployed(jsr77, appID,clearRM, cascade, Constants.UNLOAD_ALL);
        
booleanapplicationUndeployed(boolean jsr77, java.lang.String appID, boolean clearRM, boolean cascade, int loadUnloadAction)


        //set default value
        if (loadUnloadAction == Constants.LOAD_UNSET) {
            loadUnloadAction = Constants.UNLOAD_ALL;
        }

        boolean handlePreUnloadAction = 
            (loadUnloadAction == Constants.UNLOAD_ALL ||
             loadUnloadAction == Constants.UNLOAD_REST); 

        boolean handlePostUnloadAction = 
            (loadUnloadAction == Constants.UNLOAD_ALL ||
             loadUnloadAction == Constants.UNLOAD_RAR); 

        // removes this app from reload monitor
        if (handlePreUnloadAction && clearRM) {
            removeFromReloadMonitor(appID);
        }

        AbstractLoader appLoader = (AbstractLoader) id2loader.get(appID);

        if (handlePostUnloadAction) {
            appLoader = (AbstractLoader) id2loader.remove(appID);
        }

        // application is not in the registry
        if ((appLoader == null) || 
            (appLoader != null && appLoader.getApplication() == null)) {
            return true;
        }

        appLoader.setCascade(cascade);
        appLoader.setLoadUnloadAction(loadUnloadAction);
        Application app = appLoader.getApplication();

        if (handlePreUnloadAction) {
            // set jsr77 state to STOPPING
            try {
                appLoader.setState(StateManageable.STOPPING_STATE);
            } catch (MBeanException mbe) {
                _logger.log(Level.WARNING,
                    "core.error_while_setting_jsr77_state",mbe);
            }

            RoleMapper.removeRoleMapper(app.getRoleMapper().getName());

            if (jsr77) {
                //delete jsr77 mbean
                try {
                    appLoader.deleteLeafAndRootMBeans();
                } catch (MBeanException mbe) {
                    _logger.log(Level.WARNING,
                        "core.error_while_deleting_jsr77_leaf_and_root_mbeans",mbe);
                }
            } else {
                // set jsr77 state STOPPED
                try {
                    appLoader.setState(StateManageable.STOPPED_STATE);
                } catch (MBeanException mbe) {
                    _logger.log(Level.WARNING,
                       "core.error_while_setting_jsr77_state",mbe);
                }
            }
        
        }

        boolean undeployed = appLoader.unload(jsr77);

        if (loadUnloadAction == Constants.UNLOAD_REST) {
            return undeployed;
        }

        if (undeployed) {
            // since web modules are loaded separately, 
            // at this point we can only claim ejbs to be 
            // loaded successfully
            if (app.getEjbComponentCount() > 0) {
                _logger.log(Level.INFO, 
                        "core.application_unloaded_ejb", appID);
            }
        } else {
            _logger.log(Level.INFO, 
                        "core.application_not_unloaded", appID);
        }

        return undeployed;
    
public synchronized voidapplicationUndeployed(com.sun.enterprise.admin.event.ApplicationDeployEvent event)


        // set jsr77 flag
        boolean jsr77 = false;
        String action = event.getAction();
        if ( (action.equals(BaseDeployEvent.UNDEPLOY)) ||
             (action.equals(BaseDeployEvent.REDEPLOY)) ) {
                jsr77 = true;
        }

        try {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                    "[ApplicationManager] Handling event " + event.toString());
            }

            // refreshes the config context with the context from this event
            this.configManager.refreshConfigContext(
                            event.getOldConfigContext());

            String appName = event.getApplicationName();
            //If forceDeploy = true, setcascade as true, so that RA undeployment 
            //removes all deployed connector connection pools, 
            //connector resources, RA configs
            if(event.getForceDeploy()) {
                event.setCascade(true);
            }

            boolean undeployed = applicationUndeployed(jsr77, appName, true, event.getCascade(), event.getLoadUnloadAction());

            if (!undeployed) {
                String msg = localStrings.getString(
                    "applicationmgr.application_undeployed_failed", appName);
                registerException(event, msg);
            }
        } catch(ConfigException ce) {
            throw new AdminEventListenerException(ce.getMessage()); 
        }
    
protected AbstractLoadergetLoader(java.lang.String id)
Returns an application loader for the given application id.

param
id registration name of an application
return
an application loader for this manager

        // get the appropriate loader
        PluggableFeatureFactory featureFactory =
            ApplicationServer.getServerContext().getPluggableFeatureFactory();
        ApplicationLoaderFactory appLoaderFactory =
            featureFactory.getApplicationLoaderFactory();
        ApplicationLoader appLoader =
            appLoaderFactory.createApplicationLoader(id,
                this.parentClassLoader,(AppsManager)this.configManager);
        _logger.log(Level.FINEST,"ApplicationLoader "+appLoader);
        return appLoader;
    
private voidholdRequest()

 
private voidholdRequest(java.lang.String appID)

 
public synchronized booleanreload(MonitorableEntry entry)
This is a callback from realod monitor. This is called when the thread detects a change in the $APP_ROOT/.reload file's time stamp. This method reloads the application.

The time stamp is set when the callback is made in the entry.

param
entry monitored entry with a change in time stamp
return
if applicatoin was reloaded successfully


        String appName = entry.getId();
        boolean status = false;

        try {
            DeploymentRequest req = new DeploymentRequest(
                            this.configManager.getInstanceEnvironment(), 
                            DeployableObjectType.APP, 
                            DeploymentCommand.DEPLOY);

            // monitored file points to $APP_ROOT/.reload
            req.setFileSource(entry.getMonitoredFile().getParentFile());

            // application registration name
            req.setName(appName);

            // we are always trying a redeployment
            req.setForced(true);
			
			AutoDirReDeployer deployer = new AutoDirReDeployer(req);
			status = deployer.redeploy();

        } 
		catch (IASDeploymentException de) 
	{	
            _logger.log(Level.WARNING,"core.error_while_redeploying_app",de);
            return false;
        }
        return status;