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

WebModuleDeployEventListener

public class WebModuleDeployEventListener extends DummyWebModuleManager implements com.sun.enterprise.admin.event.ModuleDeployEventListener
A ModuleDeployEventListener for dynamic deployment support of web module.
author
Amy Roh

Fields Summary
static Logger
_logger
logger to log core messages
private static com.sun.enterprise.util.i18n.StringManager
localStrings
local string manager
private Hashtable
cachedWebModules
protected com.sun.enterprise.web.WebContainer
webContainer
The WebContainer instance.
Constructors Summary
public WebModuleDeployEventListener(com.sun.enterprise.instance.WebModulesManager manager, ClassLoader loader)
Standard constructor.



           
         
        super(manager, loader);
        AdminEventListenerRegistry.addModuleDeployEventListener(this);
    
Methods Summary
private com.sun.enterprise.web.WebContainergetWebContainer()

        if (webContainer == null) {
            this.webContainer = WebContainer.getInstance();
        }
        return this.webContainer;
    
private synchronized com.sun.enterprise.config.serverbeans.WebModulegetWebModuleAndUpdateCache(com.sun.enterprise.config.ConfigContext config, java.lang.String moduleName, boolean isAddToCache)

        WebModule webModule = (WebModule)cachedWebModules.get(moduleName);
        if ( webModule  == null ) {
            try {
              webModule = (WebModule) ApplicationHelper.findApplication(
                  config, moduleName);
            } catch (ConfigException ce) {
                throw new AdminEventListenerException(ce);
            }
 
            if (webModule == null) {
                String msg = localStrings.getString(
                    "webmodule.module_not_found",
                    moduleName);
                throw new AdminEventListenerException(msg);
            }
            if (isAddToCache) {
                cachedWebModules.put(moduleName, webModule);
            }
        } else {
            if (!isAddToCache) {
                cachedWebModules.remove(moduleName);
            }
        }
        return webModule;
    
private com.sun.enterprise.instance.WebModulesManagergetWebModulesManager()

        return (WebModulesManager) this.configManager;
    
protected booleanisEnabled(com.sun.enterprise.config.ConfigContext config, java.lang.String moduleName)
Whether or not a web module should be enabled is defined by the "enable" attribute on both the module element and the application-ref element.

param
config The dynamic ConfigContext
param
moduleName The name of the component (application or module)
return
boolean

        try {
            ConfigBean app = ApplicationHelper.findApplication(config, moduleName);

            Server server = ServerBeansFactory.getServerBean(config);
            ApplicationRef appRef = server.getApplicationRefByRef(moduleName);

            return ((app != null && app.isEnabled()) && 
                        (appRef != null && appRef.isEnabled()));
        } catch (ConfigException e) {
            AdminEventListenerException ex = new AdminEventListenerException();
            ex.initCause(e);
            _logger.log(Level.FINE, "Error in finding " + moduleName, e);

            //If there is anything wrong, do not enable the module
            return false;
        }
    
private com.sun.enterprise.web.WebModuleConfigloadWebModuleConfig(com.sun.enterprise.config.serverbeans.WebModule wm, com.sun.enterprise.config.ConfigContext config)
Creates and returns an object that contains information about the web module's configuration such as the information specified in server.xml, the deployment descriptor objects etc.

return
null if an error occured while reading/parsing the deployment descriptors.

    
    
    // --------------------------------------------------------- Private Methods
 
 
                                                           
        
          

        WebModuleConfig wmInfo = new WebModuleConfig();
        wmInfo.setBean(wm);
        String wmID = wm.getName();
        String location = wm.getLocation();
        try {
	    Application app = getWebModulesManager().getDescriptor(wmID, 
                location);
            WebBundleDescriptor wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor();                
            wmInfo.setDescriptor(wbd);
            String vs = ServerBeansFactory.getVirtualServersByAppName(
                    config, wmID);
            wmInfo.setVirtualServers(vs);
        } catch (ConfigException ce) {
            wmInfo = null;
        }
        return wmInfo;
        
    
private voidmoduleDeployed(com.sun.enterprise.config.ConfigContext config, java.lang.String moduleName)


        WebModule webModule = getWebModuleAndUpdateCache(
            config, moduleName, true); 

        //Do not deploy if enable is false
        if (!isEnabled(config, moduleName)) {
            return;
        }
        String location = webModule.getLocation();   
        // If module root is relative then prefix it with the 
        // location of where all the standalone modules for 
        // this server instance are deployed
        File moduleBase = new File(location);
        String modulesRoot = getWebContainer().getModulesRoot();
        if (!moduleBase.isAbsolute()) {
            location = modulesRoot+File.separator+location;
            webModule.setLocation(location);
        }
        WebModuleConfig wmInfo = loadWebModuleConfig(webModule, config);
        List<Throwable> throwables = 
            getWebContainer().loadWebModule(wmInfo, "null");
        if (throwables != null && !throwables.isEmpty()) {
            //we don't have the ability to return all errors.  so return the
            //first one, enough to signal the problem.
            String msg = throwables.get(0).getMessage();
            AdminEventListenerException ex = 
                new AdminEventListenerException(msg);
            ex.initCause(throwables.get(0)); 
            throw ex;
        }
    
public synchronized voidmoduleDeployed(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is deployed.

param
event the module deployment event
throws
AdminEventListenerException when the listener is unable to process the event.


        if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                    "[WebModuleDeployEventListener] Handling event " + event.toString());
            }

            DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event);

            ConfigContext config = event.getConfigContext(); 
            String moduleName = event.getModuleName(); 

            // refreshes the config context with the context from this event
            try {
                getWebModulesManager().refreshConfigContext(config);
            } catch (ConfigException ce) {
                throw new AdminEventListenerException(ce.getMessage());
            }

            // The only job of dummyLoader is to inform the ondemand framework
            // about this module. It does not actually load the app.
            AbstractLoader dummyLoader = getLoader(moduleName);
            if (dummyLoader.load(false)) {
                moduleDeployed(config, moduleName);
            }
        }
        
    
public synchronized voidmoduleDisabled(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is disabled.

param
event the module deployment event
throws
AdminEventListenerException when the listener is unable to process the event.


        if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                    "[WebModuleDeployEventListener] Handling event " + event.toString());
            }
            ConfigContext config = event.getConfigContext(); 
            String moduleName = event.getModuleName(); 

            WebModule webModule = getWebModuleAndUpdateCache(
                config, moduleName, true); 

            String appName = null;
            WebBundleDescriptor wbd;
            try {
                Application app = getWebModulesManager().getDescriptor(
                    webModule.getName(), webModule.getLocation());
                wbd = 
                    (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
                appName = app.getRegistrationName();
            } catch (ConfigException ce) {
                throw new AdminEventListenerException(ce);
            }
            String contextRoot = webModule.getContextRoot();
            String virtualServers = null;
            try {
                virtualServers = ServerBeansFactory
                                .getVirtualServersByAppName(config,moduleName);
            } catch(ConfigException ce) {
                if (_logger.isLoggable(Level.FINEST)) {
                    _logger.log(Level.FINEST,
                    "Exception getting virtual servers by app name "
                    + moduleName, ce);
                }
            }
            getWebContainer().disableWebModule(contextRoot,appName,
                virtualServers);
        }  
        
    
public synchronized voidmoduleEnabled(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is enabled.

param
event the module deployment event
throws
AdminEventListenerException when the listener is unable to process the event.


        if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) { 
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                    "[WebModuleDeployEventListener] Handling event " + event.toString());
            }
            ConfigContext config = event.getConfigContext(); 
            String moduleName = event.getModuleName(); 

            WebModule webModule = getWebModuleAndUpdateCache(
                config, moduleName, true); 

            //Do not deploy if enable is false
            if (!isEnabled(event.getConfigContext(), moduleName)) {
                return;
            }

            String location = webModule.getLocation();
            // If module root is relative then prefix it with the
            // location of where all the standalone modules for
            // this server instance are deployed
            File moduleBase = new File(location);
            String modulesRoot = getWebContainer().getModulesRoot();
            if (!moduleBase.isAbsolute()) {
                location = modulesRoot+File.separator+location;
                webModule.setLocation(location);
            }
            WebModuleConfig wmInfo = loadWebModuleConfig(webModule, config);
            getWebContainer().enableWebModule(wmInfo, "null");
        }
        
    
public synchronized voidmoduleRedeployed(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is redeployed.

param
event the module deployment event
throws
AdminEventListenerException when the listener is unable to process the event.


        if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {  
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                    "[WebModuleDeployEventListener] Handling event " + event.toString());
            }
            String moduleName = event.getModuleName(); 
                
            // unload from old location 
            ConfigContext oldConfig = event.getOldConfigContext();
            moduleUndeployed(oldConfig, moduleName);
               
            // load to new location
            ConfigContext config = event.getConfigContext(); 
            moduleDeployed(config, moduleName);
        }
        
    
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 voidmoduleUndeployed(com.sun.enterprise.config.ConfigContext config, java.lang.String moduleName)


        WebModule webModule = getWebModuleAndUpdateCache(
            config, moduleName, false); 

        String appName = null;
        WebBundleDescriptor wbd;
        try {
	    Application app = getWebModulesManager().getDescriptor(
                webModule.getName(), webModule.getLocation());
            RoleMapper.removeRoleMapper(app.getRoleMapper().getName());
            wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
            appName = app.getRegistrationName();
        } catch (ConfigException ce) {
            throw new AdminEventListenerException(ce);
        }            
        String contextRoot = webModule.getContextRoot();       
        String virtualServers = null;
        try {
            virtualServers = ServerBeansFactory
                                .getVirtualServersByAppName(config,moduleName);
        } catch(ConfigException ce) {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST, 
                "Exception getting virtual servers by app name " 
                    + moduleName, ce);
            }
        }
        try {
            getWebContainer().unloadWebModule(contextRoot, appName,
                                         virtualServers, wbd);
        } finally {
            try {
              Switch.getSwitch().getNamingManager().unbindObjects(wbd);
            } catch (javax.naming.NamingException nameEx) {
                _logger.log(Level.FINEST, "[WebModuleDeployEventListener] "
                + " Exception during namingManager.unbindObject",
                nameEx);
            }
        } 
    
public synchronized voidmoduleUndeployed(com.sun.enterprise.admin.event.ModuleDeployEvent event)
Invoked when a standalone J2EE module is undeployed.

param
event the module deployment event
throws
AdminEventListenerException when the listener is unable to process the event.


        if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST,
                    "[WebModuleDeployEventListener] Handling event " + event.toString());
            }
            ConfigContext config = event.getOldConfigContext(); 
            String moduleName = event.getModuleName(); 

            // refreshes the config context with the context from this event
            try {
                getWebModulesManager().refreshConfigContext(config);
            } catch (ConfigException ce) {
                throw new AdminEventListenerException(ce.getMessage());
            }

            AbstractLoader dummyLoader = getLoader(moduleName);
            dummyLoader.unload(false);
            moduleUndeployed(config, moduleName);
        }