Search in sources :

Example 1 with ServletContextAttributeListener

use of javax.servlet.ServletContextAttributeListener in project tomcat by apache.

the class StandardContext method listenerStart.

/**
     * 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.
     */
public boolean listenerStart() {
    if (log.isDebugEnabled())
        log.debug("Configuring application event listeners");
    // Instantiate the required listeners
    String[] listeners = findApplicationListeners();
    Object[] results = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
        if (getLogger().isDebugEnabled())
            getLogger().debug(" Configuring event listener class '" + listeners[i] + "'");
        try {
            String listener = listeners[i];
            results[i] = getInstanceManager().newInstance(listener);
        } catch (Throwable t) {
            t = ExceptionUtils.unwrapInvocationTargetException(t);
            ExceptionUtils.handleThrowable(t);
            getLogger().error(sm.getString("standardContext.applicationListener", listeners[i]), t);
            ok = false;
        }
    }
    if (!ok) {
        getLogger().error(sm.getString("standardContext.applicationSkipped"));
        return false;
    }
    // Sort listeners in two arrays
    ArrayList<Object> eventListeners = new ArrayList<>();
    ArrayList<Object> 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 HttpSessionIdListener) || (results[i] instanceof HttpSessionAttributeListener)) {
            eventListeners.add(results[i]);
        }
        if ((results[i] instanceof ServletContextListener) || (results[i] instanceof HttpSessionListener)) {
            lifecycleListeners.add(results[i]);
        }
    }
    // list.
    for (Object eventListener : getApplicationEventListeners()) {
        eventListeners.add(eventListener);
    }
    setApplicationEventListeners(eventListeners.toArray());
    for (Object lifecycleListener : getApplicationLifecycleListeners()) {
        lifecycleListeners.add(lifecycleListener);
        if (lifecycleListener instanceof ServletContextListener) {
            noPluggabilityListeners.add(lifecycleListener);
        }
    }
    setApplicationLifecycleListeners(lifecycleListeners.toArray());
    if (getLogger().isDebugEnabled())
        getLogger().debug("Sending application start events");
    // Ensure context is not null
    getServletContext();
    context.setNewServletContextListenerAllowed(false);
    Object[] instances = getApplicationLifecycleListeners();
    if (instances == null || instances.length == 0) {
        return ok;
    }
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    ServletContextEvent tldEvent = null;
    if (noPluggabilityListeners.size() > 0) {
        noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
        tldEvent = new ServletContextEvent(noPluggabilityServletContext);
    }
    for (int i = 0; i < instances.length; i++) {
        if (!(instances[i] instanceof ServletContextListener))
            continue;
        ServletContextListener listener = (ServletContextListener) instances[i];
        try {
            fireContainerEvent("beforeContextInitialized", listener);
            if (noPluggabilityListeners.contains(listener)) {
                listener.contextInitialized(tldEvent);
            } else {
                listener.contextInitialized(event);
            }
            fireContainerEvent("afterContextInitialized", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            fireContainerEvent("afterContextInitialized", listener);
            getLogger().error(sm.getString("standardContext.listenerStart", instances[i].getClass().getName()), t);
            ok = false;
        }
    }
    return (ok);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 2 with ServletContextAttributeListener

use of javax.servlet.ServletContextAttributeListener in project tomcat by apache.

the class ApplicationContext method removeAttribute.

@Override
public void removeAttribute(String name) {
    Object value = null;
    // Check for read only attribute
    if (readOnlyAttributes.containsKey(name)) {
        return;
    }
    value = attributes.remove(name);
    if (value == null) {
        return;
    }
    // Notify interested application event listeners
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0))
        return;
    ServletContextAttributeEvent event = new ServletContextAttributeEvent(context.getServletContext(), name, value);
    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof ServletContextAttributeListener))
            continue;
        ServletContextAttributeListener listener = (ServletContextAttributeListener) listeners[i];
        try {
            context.fireContainerEvent("beforeContextAttributeRemoved", listener);
            listener.attributeRemoved(event);
            context.fireContainerEvent("afterContextAttributeRemoved", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            context.fireContainerEvent("afterContextAttributeRemoved", listener);
            // FIXME - should we do anything besides log these?
            log(sm.getString("applicationContext.attributeEvent"), t);
        }
    }
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletContextAttributeEvent(javax.servlet.ServletContextAttributeEvent)

Example 3 with ServletContextAttributeListener

use of javax.servlet.ServletContextAttributeListener in project tomcat by apache.

the class ApplicationContext method setAttribute.

@Override
public void setAttribute(String name, Object value) {
    // Name cannot be null
    if (name == null)
        throw new IllegalArgumentException(sm.getString("applicationContext.setAttribute.namenull"));
    // Null value is the same as removeAttribute()
    if (value == null) {
        removeAttribute(name);
        return;
    }
    // Check for read only attribute
    if (readOnlyAttributes.containsKey(name))
        return;
    Object oldValue = attributes.put(name, value);
    boolean replaced = oldValue != null;
    // Notify interested application event listeners
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0))
        return;
    ServletContextAttributeEvent event = null;
    if (replaced)
        event = new ServletContextAttributeEvent(context.getServletContext(), name, oldValue);
    else
        event = new ServletContextAttributeEvent(context.getServletContext(), name, value);
    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof ServletContextAttributeListener))
            continue;
        ServletContextAttributeListener listener = (ServletContextAttributeListener) listeners[i];
        try {
            if (replaced) {
                context.fireContainerEvent("beforeContextAttributeReplaced", listener);
                listener.attributeReplaced(event);
                context.fireContainerEvent("afterContextAttributeReplaced", listener);
            } else {
                context.fireContainerEvent("beforeContextAttributeAdded", listener);
                listener.attributeAdded(event);
                context.fireContainerEvent("afterContextAttributeAdded", listener);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            if (replaced)
                context.fireContainerEvent("afterContextAttributeReplaced", listener);
            else
                context.fireContainerEvent("afterContextAttributeAdded", listener);
            // FIXME - should we do anything besides log these?
            log(sm.getString("applicationContext.attributeEvent"), t);
        }
    }
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletContextAttributeEvent(javax.servlet.ServletContextAttributeEvent)

Aggregations

ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)3 ServletContextAttributeEvent (javax.servlet.ServletContextAttributeEvent)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)1 ServletRequestListener (javax.servlet.ServletRequestListener)1 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)1 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)1 HttpSessionListener (javax.servlet.http.HttpSessionListener)1 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)1