FileDocCategorySizeDatePackage
StandAloneEJBModulesManager.javaAPI DocGlassfish v2 API20657Fri May 04 22:35:42 BST 2007com.sun.enterprise.server

StandAloneEJBModulesManager

public class StandAloneEJBModulesManager extends AbstractManager implements com.sun.enterprise.admin.event.ModuleDeployEventListener
Manages all stand alone EJB modules. It also acts as an listener for the deployment events.
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
Constructors Summary
StandAloneEJBModulesManager(com.sun.enterprise.instance.EjbModulesManager ejbManager, ClassLoader parentClassLoader)
This manager is responsible for loading all stand alone ejb modules in an instance. It creates EJBModuleLoader for each deployed ejb module. EJBModuleLoader is responsible for further loading ejb containers for each module.

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

param
ejbManager encapsulates application related info
param
commonclassLoader the top level class loader. For now this can be the MainThread's ClassLoader.


                                                                                                                   
     
              

        super(parentClassLoader, ejbManager);

        AdminEventListenerRegistry.addModuleDeployEventListener(this);
    
Methods Summary
protected AbstractLoadergetLoader(java.lang.String moduleId)
Returns a loader for stand alone ejb module.

param
moduleId registration name of the ejb module
return
a loader for the stand alone ejb module

        return new EJBModuleLoader(moduleId, this.parentClassLoader, 
            (EjbModulesManager)this.configManager);
    
private voidholdRequest()

 
private voidholdRequest(java.lang.String appID)

 
public synchronized voidmoduleDeployed(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is deployed.


        boolean jsr77 = false;

        if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {
            
            DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event);

            String modID = event.getModuleName();

            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                            "Deploying EJB Module: " + modID);
            }

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

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

                if (isEnabled(event.getConfigContext(), modID) && 
                      !moduleDeployed(jsr77, modID, event.getConfigContext())) {
                          
                    String msg = localStrings.getString(
                        "standaloneejbmodulesmgr.ejbmodule_deployed_failed", 
                        modID);
                    registerException(event, msg);
                }
            } catch (ConfigException ce) {
                throw new AdminEventListenerException(ce.getMessage()); 
            }
        }
    
private booleanmoduleDeployed(boolean jsr77, java.lang.String moduleName, com.sun.enterprise.config.ConfigContext dynamicConfigContext)

	return moduleDeployed(jsr77, moduleName, true, dynamicConfigContext);
    
private booleanmoduleDeployed(boolean jsr77, java.lang.String moduleName, boolean addToRM, com.sun.enterprise.config.ConfigContext dynamicConfigContext)

        boolean result = false;
        boolean loadJSR77 = jsr77 || loadJSR77(moduleName, DeployableObjectType.EJB);

        AbstractLoader modLoader = null;
        try {

            modLoader = getLoader(moduleName);
	    modLoader.setConfigContext(dynamicConfigContext);

            // create root mBean for this module
            if (loadJSR77) {
                try {
                    modLoader.createRootMBean();
                } catch (MBeanException mbe) {
                    _logger.log(Level.WARNING,
                        "core.error_while_creating_jsr77_root_mbean",mbe);
                }
            }

            if (isEnabled(dynamicConfigContext, moduleName)) {

                if (_logger.isLoggable(Level.FINEST)) {
                   _logger.log(Level.FINEST,
                           "Loading enabled ejb module: " + moduleName);
                }

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

                // set jsr77 state STARTING 
                try {
                    modLoader.setState(StateManageable.STARTING_STATE);
                } catch (MBeanException mbe) {
                    _logger.log(Level.WARNING,
                            "core.error_while_setting_jsr77_state",mbe);
                }

                boolean retSts = modLoader.load(jsr77);

                if (retSts) {
                    this.id2loader.put(moduleName, modLoader);

                    // adds the ejb module to be monitored if ON
                    if (addToRM) {
                        addToReloadMonitor(moduleName);
                    }

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

                    result = true;
                } else {
                    if (loadJSR77) {
                        // delete root and leaf mBeans for this module
                        try {
                            modLoader.deleteLeafAndRootMBeans();
                        } catch (MBeanException mbe) {
                            _logger.log(Level.WARNING,
                            "core.error_while_deleting_jsr77_root_and_leaf_mbeans",mbe);
                        }
                    } else {
                        // set jsr77 state FAILED 
                        try {
                            modLoader.setState(StateManageable.FAILED_STATE);
                        } catch (MBeanException mbe) {
                            _logger.log(Level.WARNING,
                                "core.error_while_setting_jsr77_state",mbe);
                        }
                    }

                    _logger.log(Level.WARNING,
                                "core.error_while_loading_ejb_module");
                }
            } else {
                _logger.log(Level.INFO, "core.ejb_module_disabled", moduleName);
                //setting result to false since module was disabled
                result = false;
            }
        } catch (ConfigException ce) {
                        _logger.log(Level.WARNING,"core.error_while_loading_ejb_module",ce);
            result = false;
        } finally {
	    //To ensure that the config context is not reused...
	    if (modLoader != null) {
		modLoader.setConfigContext(null);
	    }
	}
        return result;
    
public synchronized voidmoduleDisabled(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is disabled.


        if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {

            String modID = event.getModuleName();

                        if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST,
                            "Disabling EJB Module: " + modID);
            }

            moduleUndeployed(event);
        }
    
public synchronized voidmoduleEnabled(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is enabled.


        if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {

            String modID = event.getModuleName();

            if (!isEnabled(event.getConfigContext(), modID)) {
                return;
            }

            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST, "Enabling EJB Module: " + modID);
            }

            moduleDeployed(event);
        }
    
public synchronized voidmoduleRedeployed(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is redeployed.


        if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {

            String modID = event.getModuleName();

                        if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST,
                            "Redeploying EJB Module: " + modID);
            }

            moduleUndeployed(event);
            moduleDeployed(event);
        }
    
public voidmoduleReferenceAdded(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a reference is created from a server instance (or cluster) to a particular module.

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

                
public voidmoduleReferenceRemoved(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a reference is removed from a server instance (or cluster) to a particular module.

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

                
    
private booleanmoduleUnDeployed(boolean jsr77, java.lang.String moduleName, boolean clearRM)
Unloads the given ejb module.

param
jsr77 delete jsr77 mBeans if true
param
moduleName name of the stand alone ejb module
param
clearRM reloadMonitor, reload if true
return
true if module was unloaded successfully
throws
ConfigException if an error while reading data from config


        EJBModuleLoader modLoader = 
            (EJBModuleLoader) this.id2loader.remove(moduleName);

        // removes this ejb module from reload monitor
        if (clearRM) {
            removeFromReloadMonitor(moduleName);
        }

        // module is not in the registry - must have been undeployed 
        if (modLoader == null) {
            return true;
        }

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

        Application app = modLoader.getApplication();
        RoleMapper.removeRoleMapper(app.getRoleMapper().getName());

        if (jsr77) {
            // delete jsr77 mBean for this ejbModule
            try {
                modLoader.deleteRootMBean();
            } catch (MBeanException mbe) {
                _logger.log(Level.WARNING,
                        "core.error_while_deleting_jsr77_root_mbean",mbe);
            }
        } else {
            // set jsr77 state to STOPPED
            try {
                modLoader.setState(StateManageable.STOPPED_STATE);
            } catch (MBeanException mbe) {
                _logger.log(Level.WARNING,
                        "core.error_while_setting_jsr77_state",mbe);
            }
        }

        boolean undeployed = modLoader.unload(jsr77);

        if (undeployed) {
            _logger.log(Level.INFO, 
                        "core.ejb_module_unload_successful", moduleName);
        } else {
            _logger.log(Level.INFO, 
                        "core.ejb_module_not_unloaded", moduleName);
        }

        return undeployed;
    
private booleanmoduleUnDeployed(java.lang.String moduleName)
Unloads the given stand alone ejb module. This removes this ejb module from reload monitor list. This gets called when the ejb module is undeployed.

param
moduleName name of the stand alone ejb module
return
true if module was unloaded successfully
throws
ConfigException if an error while reading data from config

        return moduleUnDeployed(false, moduleName);
    
private booleanmoduleUnDeployed(boolean jsr77, java.lang.String moduleName)
Unloads the given stand alone ejb module. This removes this ejb module from reload monitor list. This gets called when the ejb module is undeployed.

param
jsr77 delete jsr77 mBeans if true
param
moduleName name of the stand alone ejb module
return
true if module was unloaded successfully
throws
ConfigException if an error while reading data from config

        return moduleUnDeployed(jsr77, moduleName, true);
    
public synchronized voidmoduleUndeployed(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is undeployed.


        boolean jsr77 = false;

        String action = event.getAction();
        if ((action.equals(BaseDeployEvent.UNDEPLOY)) ||
            (action.equals(BaseDeployEvent.REDEPLOY))) {
            jsr77 = true;
        }

        try {
            if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {

                String modID = event.getModuleName();

                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST,
                                "Undeploying EJB Module: " + modID);
                }

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

                boolean undeployed = moduleUnDeployed(jsr77, modID);

                if (!undeployed) {
                    String msg = localStrings.getString(
                        "standaloneejbmodulesmgr.ejbmodule_undeployed_failed", 
                        modID);
                    registerException(event, msg);
                }
            }
        } catch (ConfigException ce) {
            throw new AdminEventListenerException(ce.getMessage()); 
        }
    
public synchronized booleanreload(MonitorableEntry entry)
This is a callback from reload monitor when a change in time stamp for a stand alone ejb module is detected.

param
entry monitored entry with a change in time stamp
return
if stand alone ejb module was reloaded successfully


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

        // redeploys the stand alone ejb module
        try {

            DeploymentRequest req = new DeploymentRequest(
                            this.configManager.getInstanceEnvironment(), 
                            DeployableObjectType.EJB, 
                            DeploymentCommand.DEPLOY);

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

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

            // 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_in_reload_ejb_module",de);
            return false;
        }
        return status;