javax.servlet.ServletContextAttributeListener Java Examples

The following examples show how to use javax.servlet.ServletContextAttributeListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Add the listener.
 *
 * @param <T> the type.
 * @param listener the listener
 */
@Override
public <T extends EventListener> void addListener(T listener) {
    if (listener instanceof ServletContextListener) {
        contextListeners.add((ServletContextListener) listener);
    }
    if (listener instanceof ServletContextAttributeListener) {
        contextAttributeListeners.add((ServletContextAttributeListener) listener);
    }
    if (listener instanceof ServletRequestListener) {
        requestListeners.add((ServletRequestListener) listener);
    }
    if (listener instanceof ServletRequestAttributeListener) {
        httpRequestManager.addListener((ServletRequestAttributeListener) listener);
    }
    if (listener instanceof HttpSessionAttributeListener) {
        httpSessionManager.addListener(listener);
    }
    if (listener instanceof HttpSessionIdListener) {
        httpSessionManager.addListener(listener);
    }
    if (listener instanceof HttpSessionListener) {
        httpSessionManager.addListener(listener);
    }
}
 
Example #2
Source File: DefaultWebApplication.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create the listener.
 *
 * @param <T> the type.
 * @param clazz the class of the listener to create.
 * @return the listener.
 * @throws ServletException when it fails to create the listener.
 */
@Override
public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException {
    T result = objectInstanceManager.createListener(clazz);
    boolean ok = false;
    if (result instanceof ServletContextListener || result instanceof ServletContextAttributeListener || result instanceof ServletRequestListener
            || result instanceof ServletRequestAttributeListener || result instanceof HttpSessionAttributeListener
            || result instanceof HttpSessionIdListener || result instanceof HttpSessionListener) {
        ok = true;
    }

    if (!ok) {
        LOGGER.log(WARNING, "Unable to create listener: {0}", clazz);
        throw new IllegalArgumentException("Invalid type");
    }

    return result;
}
 
Example #3
Source File: WebContext.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static boolean isWeb(final Class<?> beanClass) {
    if (Servlet.class.isAssignableFrom(beanClass)
        || Filter.class.isAssignableFrom(beanClass)) {
        return true;
    }
    if (EventListener.class.isAssignableFrom(beanClass)) {
        return HttpSessionAttributeListener.class.isAssignableFrom(beanClass)
               || ServletContextListener.class.isAssignableFrom(beanClass)
               || ServletRequestListener.class.isAssignableFrom(beanClass)
               || ServletContextAttributeListener.class.isAssignableFrom(beanClass)
               || HttpSessionListener.class.isAssignableFrom(beanClass)
               || HttpSessionBindingListener.class.isAssignableFrom(beanClass)
               || HttpSessionActivationListener.class.isAssignableFrom(beanClass)
               || HttpSessionIdListener.class.isAssignableFrom(beanClass)
               || ServletRequestAttributeListener.class.isAssignableFrom(beanClass);
    }

    return false;
}
 
Example #4
Source File: ApplicationContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void removeAttribute(String name) {

    Object value = null;

    // Remove the specified attribute
    // 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 (Object obj : listeners) {
        if (!(obj instanceof ServletContextAttributeListener)) {
            continue;
        }
        ServletContextAttributeListener listener = (ServletContextAttributeListener) obj;
        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);
        }
    }
}
 
Example #5
Source File: ApplicationContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionIdListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener ||
            (t instanceof ServletContextListener && newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example #6
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void servletContextAttributeAdded(final String name, final Object value) {
    if(!started) {
        return;
    }
    final ServletContextAttributeEvent sre = new ServletContextAttributeEvent(servletContext, name, value);
    for (int i = 0; i < servletContextAttributeListeners.length; ++i) {
        this.<ServletContextAttributeListener>get(servletContextAttributeListeners[i]).attributeAdded(sre);
    }
}
 
Example #7
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void servletContextAttributeRemoved(final String name, final Object value) {
    if(!started) {
        return;
    }
    final ServletContextAttributeEvent sre = new ServletContextAttributeEvent(servletContext, name, value);
    for (int i = 0; i < servletContextAttributeListeners.length; ++i) {
        this.<ServletContextAttributeListener>get(servletContextAttributeListeners[i]).attributeRemoved(sre);
    }
}
 
Example #8
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void servletContextAttributeReplaced(final String name, final Object value) {
    if(!started) {
        return;
    }
    final ServletContextAttributeEvent sre = new ServletContextAttributeEvent(servletContext, name, value);
    for (int i = 0; i < servletContextAttributeListeners.length; ++i) {
        this.<ServletContextAttributeListener>get(servletContextAttributeListeners[i]).attributeReplaced(sre);
    }
}
 
Example #9
Source File: ApplicationContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener
            || (t instanceof ServletContextListener &&
                    newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example #10
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void servletContextAttributeAdded(final String name, final Object value) {
    if(!started) {
        return;
    }
    final ServletContextAttributeEvent sre = new ServletContextAttributeEvent(servletContext, name, value);
    for (int i = 0; i < servletContextAttributeListeners.length; ++i) {
        this.<ServletContextAttributeListener>get(servletContextAttributeListeners[i]).attributeAdded(sre);
    }
}
 
Example #11
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void servletContextAttributeRemoved(final String name, final Object value) {
    if(!started) {
        return;
    }
    final ServletContextAttributeEvent sre = new ServletContextAttributeEvent(servletContext, name, value);
    for (int i = 0; i < servletContextAttributeListeners.length; ++i) {
        this.<ServletContextAttributeListener>get(servletContextAttributeListeners[i]).attributeRemoved(sre);
    }
}
 
Example #12
Source File: ApplicationListeners.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void servletContextAttributeReplaced(final String name, final Object value) {
    if(!started) {
        return;
    }
    final ServletContextAttributeEvent sre = new ServletContextAttributeEvent(servletContext, name, value);
    for (int i = 0; i < servletContextAttributeListeners.length; ++i) {
        this.<ServletContextAttributeListener>get(servletContextAttributeListeners[i]).attributeReplaced(sre);
    }
}
 
Example #13
Source File: ApplicationContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T t) {
    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        throw new IllegalStateException(
                sm.getString("applicationContext.addListener.ise",
                        getContextPath()));
    }

    boolean match = false;
    if (t instanceof ServletContextAttributeListener ||
            t instanceof ServletRequestListener ||
            t instanceof ServletRequestAttributeListener ||
            t instanceof HttpSessionAttributeListener) {
        context.addApplicationEventListener(t);
        match = true;
    }

    if (t instanceof HttpSessionListener
            || (t instanceof ServletContextListener &&
                    newServletContextListenerAllowed)) {
        // Add listener directly to the list of instances rather than to
        // the list of class names.
        context.addApplicationLifecycleListener(t);
        match = true;
    }

    if (match) return;

    if (t instanceof ServletContextListener) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.sclNotAllowed",
                t.getClass().getName()));
    } else {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.addListener.iae.wrongType",
                t.getClass().getName()));
    }
}
 
Example #14
Source File: ApplicationContext.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Remove the context attribute with the specified name, if any.
 *
 * @param name Name of the context attribute to be removed
 */
@Override
public void removeAttribute(String name) {

    Object value = null;

    // Remove the specified attribute
    // 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);
        }
    }

}
 
Example #15
Source File: ApplicationContext.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Remove the context attribute with the specified name, if any.
 *
 * @param name Name of the context attribute to be removed
 */
@Override
public void removeAttribute(String name) {

    Object value = null;

    // Remove the specified attribute
    // 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);
        }
    }

}