FileDocCategorySizeDatePackage
ServerLifecycleModule.javaAPI DocGlassfish v2 API10344Fri May 04 22:32:54 BST 2007com.sun.appserv.server

ServerLifecycleModule

public final class ServerLifecycleModule extends Object
author
Sridatta Viswanath
version
$Id: ServerLifecycleModule.java,v 1.4 2007/05/05 05:32:53 tcfujii Exp $

Fields Summary
private LifecycleListener
slcl
private String
name
private String
className
private String
classpath
private int
loadOrder
private boolean
isFatal
private String
statusMsg
private com.sun.enterprise.server.ServerContext
ctx
private LifecycleEventContext
leContext
private ClassLoader
urlClassLoader
private Properties
props
private static Logger
_logger
private static boolean
_isTraceEnabled
private static ResourceBundle
_rb
private com.sun.enterprise.ComponentInvocation
lcmInvocation
Constructors Summary
ServerLifecycleModule(com.sun.enterprise.server.ServerContext ctx, String name, String className)


          
        this.name = name;
        this.className = className;
        this.ctx = ctx;
        this.leContext = new LifecycleEventContextImpl(ctx);

        _logger = LogDomains.getLogger(LogDomains.ROOT_LOGGER);
        _isTraceEnabled = _logger.isLoggable(Level.FINE);
        _rb = _logger.getResourceBundle();
    
Methods Summary
java.lang.StringgetClassName()

        return this.className;
    
intgetLoadOrder()

        return this.loadOrder;
    
java.lang.StringgetName()

        return this.name;
    
java.util.PropertiesgetProperties()

        return this.props;
    
public java.lang.StringgetStatus()
return status of this lifecycle module as a string

        return statusMsg;
    
private java.net.URL[]getURLs()

        return ClassLoaderUtils.getUrlsFromClasspath(this.classpath);
    
java.lang.Stringgetclasspath()

        return this.classpath;
    
booleanisFatal()

        return isFatal;
    
LifecycleListenerloadServerLifecycle()

        ClassLoader classLoader = ctx.getLifecycleParentClassLoader();

        try {
            if (this.classpath != null) {
                URL[] urls = getURLs();

                if (urls != null) {
                    StringBuffer sb = new StringBuffer(128);
                    for(int i=0;i<urls.length;i++) {
                        sb.append(urls[i].toString());
                    }
                    if (_isTraceEnabled)
                        _logger.fine("Lifecycle module = " + getName() + 
                                        " has classpath URLs = " + sb.toString());
                }

                this.urlClassLoader = new URLClassLoader(urls, classLoader);
                classLoader = this.urlClassLoader;
            }

            Class cl = Class.forName(className, true, classLoader);
            slcl = (LifecycleListener) cl.newInstance();
        } catch (Exception ee) {
            String msg = _rb.getString("lifecyclemodule.load_exception");
            Object[] params = { this.name, ee.toString() };
            msg = MessageFormat.format(msg, params);

            _logger.log(Level.WARNING, msg);
        }

        return slcl;
    
public voidonInitialization(com.sun.enterprise.server.ServerContext context)

        postEvent(LifecycleEvent.INIT_EVENT, props, null);
    
public voidonReady(com.sun.enterprise.server.ServerContext context)

        postEvent(LifecycleEvent.READY_EVENT, null, ctx.getInvocationManager());
    
public voidonShutdown()

        postEvent(LifecycleEvent.SHUTDOWN_EVENT, null, ctx.getInvocationManager());
    
public voidonStartup(com.sun.enterprise.server.ServerContext context, org.apache.catalina.Context invContext)


        /** create a ComponentInvocation for this module during startup; 
         *  as otherwise during Initialization ServerContext is not fully 
         *  established.
         */
        lcmInvocation = new ComponentInvocation(slcl, invContext);
        postEvent(LifecycleEvent.STARTUP_EVENT, null, ctx.getInvocationManager());
    
public voidonTermination()

        postEvent(LifecycleEvent.TERMINATION_EVENT, null, ctx.getInvocationManager());

        // clear the ComponentInvocation for this module
        lcmInvocation = null;
    
private voidpostEvent(int eventType, java.lang.Object data, com.sun.enterprise.InvocationManager invMgr)

        if (slcl == null) {
            if (isFatal) {
                String msg = _rb.getString("lifecyclemodule.loadExceptionIsFatal");
                Object[] params = { this.name };
                msg = MessageFormat.format(msg, params);

                throw new ServerLifecycleException(msg);
            }

            return;
        }

        if (urlClassLoader != null)
            setClassLoader();

        if (invMgr != null)
            preInvoke(invMgr);

        LifecycleEvent slcEvent= new LifecycleEvent(this, eventType, data, this.leContext);
        try {
            slcl.handleEvent(slcEvent);
        } catch (ServerLifecycleException sle) {

            String msg = _rb.getString("lifecyclemodule.event_ServerLifecycleException");

            Object[] params = { this.name };
            msg = MessageFormat.format(msg, params);

            _logger.log(Level.WARNING, msg, sle);

            if (isFatal)
                throw sle;
        } catch (Exception ee) {
            String msg = _rb.getString("lifecyclemodule.event_Exception");

            Object[] params = { this.name };
            msg = MessageFormat.format(msg, params);

            _logger.log(Level.WARNING, msg, ee);

            if (isFatal) {
                throw new ServerLifecycleException(_rb.getString("lifecyclemodule.event_exceptionIsFatal"), ee);
            }
        } finally {
            if (invMgr != null)
                postInvoke(invMgr);
        }
    
private voidpostInvoke(com.sun.enterprise.InvocationManager invMgr)

        try {
            invMgr.postInvoke(lcmInvocation);
        } catch (InvocationException ie) {
            String msg = _rb.getString("lifecyclemodule.postInvoke_exception");
            Object[] params = { this.name };
            msg = MessageFormat.format(msg, params);

            throw new ServerLifecycleException(msg, ie);
        }
    
private voidpreInvoke(com.sun.enterprise.InvocationManager invMgr)

        try {
            invMgr.preInvoke(lcmInvocation);
        } catch (InvocationException ie) {
            String msg = _rb.getString("lifecyclemodule.preInvoke_exception");
            Object[] params = { this.name };
            msg = MessageFormat.format(msg, params);

            throw new ServerLifecycleException(msg, ie);
        }
    
private voidsetClassLoader()

         // set the common class loader as the thread context class loader
        java.security.AccessController.doPrivileged(
            new java.security.PrivilegedAction() {
                public Object run() {
                    Thread.currentThread().setContextClassLoader(urlClassLoader);
                    return null;
                }
            }
        );
    
voidsetClasspath(java.lang.String classpath)

        this.classpath = classpath;
    
voidsetIsFatal(boolean isFatal)

        this.isFatal = isFatal;
    
voidsetLoadOrder(int loadOrder)

        this.loadOrder = loadOrder;
    
voidsetProperty(java.lang.String name, java.lang.String value)

        props.put(name, value);
    
public java.lang.StringtoString()

        return "Server LifecycleListener support";