Example usage for javax.servlet ServletContextListener contextInitialized

List of usage examples for javax.servlet ServletContextListener contextInitialized

Introduction

In this page you can find the example usage for javax.servlet ServletContextListener contextInitialized.

Prototype

default public void contextInitialized(ServletContextEvent sce) 

Source Link

Document

Receives notification that the web application initialization process is starting.

Usage

From source file:org.ireland.jnetty.webapp.WebApp.java

/**
 * Adds the listener object.//from   w w w .  j  a  v a2s .  c  o  m
 */
private void addListenerObject(Object listenerObj, boolean start) {

    // ServletContextListener
    if (listenerObj instanceof ServletContextListener) {
        ServletContextListener scListener = (ServletContextListener) listenerObj;
        _contextListeners.add(scListener);

        // ? ServletContextEvent#contextInitialized 
        if (start) {
            ServletContextEvent event = new ServletContextEvent(this);

            try {
                scListener.contextInitialized(event);
            } catch (Exception e) {
                e.printStackTrace();
                log.debug(e.toString(), e);
            }
        }
    }

    // ServletContextAttributeListener
    if (listenerObj instanceof ServletContextAttributeListener)
        addAttributeListener((ServletContextAttributeListener) listenerObj);

    // ServletRequestListener
    if (listenerObj instanceof ServletRequestListener) {
        _requestListeners.add((ServletRequestListener) listenerObj);
    }

    // ServletRequestAttributeListener
    if (listenerObj instanceof ServletRequestAttributeListener) {
        _requestAttributeListeners.add((ServletRequestAttributeListener) listenerObj);
    }

    // HttpSessionListener
    if (listenerObj instanceof HttpSessionListener)
        getSessionManager().addListener((HttpSessionListener) listenerObj);

    // HttpSessionListener
    if (listenerObj instanceof HttpSessionAttributeListener)
        getSessionManager().addAttributeListener((HttpSessionAttributeListener) listenerObj);

    // HttpSessionActivationListener
    if (listenerObj instanceof HttpSessionActivationListener)
        getSessionManager().addActivationListener((HttpSessionActivationListener) listenerObj);
}

From source file:org.apache.catalina.core.StandardContext.java

/**
 * Configure the set of instantiated application event listeners
 * for this Context.  Return <code>true</code> if all listeners wre
 * initialized successfully, or <code>false</code> otherwise.
 *///from w w w.ja  v a 2 s.co m
public boolean listenerStart() {

    if (log.isDebugEnabled())
        log.debug("Configuring application event listeners");

    // Instantiate the required listeners
    ClassLoader loader = getLoader().getClassLoader();
    String listeners[] = findApplicationListeners();
    Object results[] = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
        if (log.isDebugEnabled())
            log.debug(" Configuring event listener class '" + listeners[i] + "'");
        try {
            Class clazz = loader.loadClass(listeners[i]);
            results[i] = clazz.newInstance();
        } catch (Throwable t) {
            getServletContext().log(sm.getString("standardContext.applicationListener", listeners[i]), t);
            ok = false;
        }
    }
    if (!ok) {
        log.error(sm.getString("standardContext.applicationSkipped"));
        return (false);
    }

    // Sort listeners in two arrays
    ArrayList eventListeners = new ArrayList();
    ArrayList lifecycleListeners = new ArrayList();
    for (int i = 0; i < results.length; i++) {
        if ((results[i] instanceof ServletContextAttributeListener)
                || (results[i] instanceof ServletRequestAttributeListener)
                || (results[i] instanceof ServletRequestListener)
                || (results[i] instanceof HttpSessionAttributeListener)) {
            eventListeners.add(results[i]);
        }
        if ((results[i] instanceof ServletContextListener) || (results[i] instanceof HttpSessionListener)) {
            lifecycleListeners.add(results[i]);
        }
    }

    setApplicationEventListeners(eventListeners.toArray());
    setApplicationLifecycleListeners(lifecycleListeners.toArray());

    // Send application start events

    if (log.isDebugEnabled())
        log.debug("Sending application start events");

    Object instances[] = getApplicationLifecycleListeners();
    if (instances == null)
        return (ok);
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    for (int i = 0; i < instances.length; i++) {
        if (instances[i] == null)
            continue;
        if (!(instances[i] instanceof ServletContextListener))
            continue;
        ServletContextListener listener = (ServletContextListener) instances[i];
        try {
            fireContainerEvent("beforeContextInitialized", listener);
            listener.contextInitialized(event);
            fireContainerEvent("afterContextInitialized", listener);
        } catch (Throwable t) {
            fireContainerEvent("afterContextInitialized", listener);
            getServletContext()
                    .log(sm.getString("standardContext.listenerStart", instances[i].getClass().getName()), t);
            ok = false;
        }
    }
    return (ok);

}