javax.servlet.http.HttpSessionListener Java Examples

The following examples show how to use javax.servlet.http.HttpSessionListener. 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: 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 #2
Source File: TestHttpSessionNotifier.java    From HttpSessionReplacer with MIT License 6 votes vote down vote up
@Test
public void testSessionDestroyed() {
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  String dummy = "dummy";
  when(session.getAttribute("binding")).thenReturn(bindingListener);
  when(session.getAttribute("attribute")).thenReturn(dummy);
  when(session.getAttributeNamesWithValues()).thenReturn(Arrays.asList("binding", "attribute"));
  HttpSessionListener listener = mock(HttpSessionListener.class);
  descriptor.addHttpSessionListener(listener);
  notifier.sessionDestroyed(session, false);
  verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class));
  verify(listener).sessionDestroyed(any(HttpSessionEvent.class));
  HttpSessionListener listener2 = mock(HttpSessionListener.class);
  HttpSessionBindingListener bindingListener2 = mock(HttpSessionBindingListener.class);
  when(session.getAttribute("binding2")).thenReturn(bindingListener2);
  when(session.getAttributeNamesWithValues()).thenReturn(Arrays.asList("binding", "attribute", "binding2"));
  descriptor.addHttpSessionListener(listener2);
  notifier.sessionDestroyed(session, false);
  verify(listener, times(2)).sessionDestroyed(any(HttpSessionEvent.class));
  verify(bindingListener, times(2)).valueUnbound(any(HttpSessionBindingEvent.class));
  verify(bindingListener2).valueUnbound(any(HttpSessionBindingEvent.class));
  verify(listener2).sessionDestroyed(any(HttpSessionEvent.class));
}
 
Example #3
Source File: AbstractSessionManager.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public final void removeSession(final AbstractSession session, boolean invalidate) {
    final String clusterId = getClusterId(session);
    final boolean removed = removeSession(clusterId);
    if (removed) {
        _sessionsStats.decrement();
        _sessionTimeStats.set(Math.round((System.currentTimeMillis() - session.getCreationTime()) / 1000.0));
        _sessionIdManager.removeSession(session);
        if (invalidate) {
            _sessionIdManager.invalidateAll(session.getClusterId());
        }

        if (invalidate && _sessionListeners != null) {
            final HttpSessionEvent event = new HttpSessionEvent(session);
            for (int i = LazyList.size(_sessionListeners); i-- > 0;) {
                ((HttpSessionListener) LazyList.get(_sessionListeners, i)).sessionDestroyed(event);
            }
        }

        if (!invalidate) {
            session.willPassivate();
        }
    }
}
 
Example #4
Source File: SessionEventHttpSessionListenerAdapter.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(AbstractSessionEvent event) {
	if (this.listeners.isEmpty()) {
		return;
	}

	HttpSessionEvent httpSessionEvent = createHttpSessionEvent(event);

	for (HttpSessionListener listener : this.listeners) {
		if (event instanceof SessionDestroyedEvent) {
			listener.sessionDestroyed(httpSessionEvent);
		}
		else if (event instanceof SessionCreatedEvent) {
			listener.sessionCreated(httpSessionEvent);
		}
	}
}
 
Example #5
Source File: HttpSessionListenerService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Registers a listener.
 *
 * @param entry
 *            the entry
 */
public static void registerListener( HttpSessionListenerEntry entry )
{
    String strListenerClass = entry.getListenerClass( );

    try
    {
        HttpSessionListener listener = (HttpSessionListener) Class.forName( strListenerClass ).newInstance( );
        LIST_LISTENERS.add( listener );
        AppLogService.info( "New Listener registered : " + strListenerClass );
    }
    catch( InstantiationException | IllegalAccessException | ClassNotFoundException e )
    {
        AppLogService.error( "Error registering the listener " + strListenerClass + " : " + e.getMessage( ), e );
    }
}
 
Example #6
Source File: SessionHelpers.java    From HttpSessionReplacer with MIT License 6 votes vote down vote up
/**
 * This method is used by injected code to register listeners for {@link ServletContext}. If object argument is a
 * {@link ServletContext} and listener argument contains {@link HttpSessionListener} or
 * {@link HttpSessionAttributeListener}, the method will add them to list of known listeners associated to
 * {@link ServletContext}
 *
 * @param servletContext
 *          the active servlet context
 * @param listener
 *          the listener to use
 */
public void onAddListener(ServletContext servletContext, Object listener) {
  String contextPath = servletContext.getContextPath();
  ServletContextDescriptor scd = getDescriptor(servletContext);
  logger.debug("Registering listener {} for context {}", listener, contextPath);
  // As theoretically one class can implement many listener interfaces we
  // check if it implements each of supported ones
  if (listener instanceof HttpSessionListener) {
    scd.addHttpSessionListener((HttpSessionListener)listener);
  }
  if (listener instanceof HttpSessionAttributeListener) {
    scd.addHttpSessionAttributeListener((HttpSessionAttributeListener)listener);
  }
  if (ServletLevel.isServlet31) {
    // Guard the code inside block to avoid use of classes
    // that are not available in versions before Servlet 3.1
    if (listener instanceof HttpSessionIdListener) { // NOSONAR
      scd.addHttpSessionIdListener((HttpSessionIdListener)listener);
    }
  }
}
 
Example #7
Source File: BootstrapListener.java    From rice with Educational Community License v2.0 6 votes vote down vote up
private void addListener(String name, String classname) {
    LOG.debug("Adding listener: " + name + "=" + classname);
    
    Object listenerObject = GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(classname));
    
    if (listenerObject == null) {
        LOG.error("Listener '" + name + "' class not found: " + classname);
        return;

    }
    if (!(listenerObject instanceof HttpSessionListener)) {
        LOG.error("Class '" + listenerObject.getClass() + "' does not implement servlet javax.servlet.http.HttpSessionListener");
        return;
    }
    
    HttpSessionListener listener = (HttpSessionListener) listenerObject;
    listeners.put(name, listener);
}
 
Example #8
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 #9
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 #10
Source File: BootstrapListener.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
 */
public void sessionDestroyed(final HttpSessionEvent event) {
    LOG.debug("Begin BootstrapListener session destroyed...");
    
    init();
    
    for (HttpSessionListener listener : listeners.values()) {
        listener.sessionDestroyed(event);
    }
    
    LOG.debug("...end BootstrapListener session destroyed.");
}
 
Example #11
Source File: BootstrapListener.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
 */
public void sessionCreated(final HttpSessionEvent event) {
    LOG.debug("Begin BootstrapListener session created...");
    
    init();
    
    for (HttpSessionListener listener : listeners.values()) {
        listener.sessionCreated(event);
    }
    
    LOG.debug("...end BootstrapListener session created.");
}
 
Example #12
Source File: ProxyServletListener.java    From lemon with Apache License 2.0 5 votes vote down vote up
public void sessionCreated(HttpSessionEvent se) {
    if (ctx == null) {
        logger.warn("cannot find applicationContext");

        return;
    }

    Collection<HttpSessionListener> httpSessionListeners = ctx
            .getBeansOfType(HttpSessionListener.class).values();

    for (HttpSessionListener httpSessionListener : httpSessionListeners) {
        httpSessionListener.sessionCreated(se);
    }
}
 
Example #13
Source File: MainHttpSessionListener.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void sessionDestroyed( HttpSessionEvent se )
{
    for ( HttpSessionListener listener : HttpSessionListenerService.getListeners( ) )
    {
        listener.sessionDestroyed( se );
    }
}
 
Example #14
Source File: MainHttpSessionListener.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void sessionCreated( HttpSessionEvent se )
{
    for ( HttpSessionListener listener : HttpSessionListenerService.getListeners( ) )
    {
        listener.sessionCreated( se );
    }
}
 
Example #15
Source File: ProxyServletListener.java    From lemon with Apache License 2.0 5 votes vote down vote up
public void sessionDestroyed(HttpSessionEvent se) {
    if (ctx == null) {
        logger.warn("cannot find applicationContext");

        return;
    }

    Collection<HttpSessionListener> httpSessionListeners = ctx
            .getBeansOfType(HttpSessionListener.class).values();

    for (HttpSessionListener httpSessionListener : httpSessionListeners) {
        httpSessionListener.sessionDestroyed(se);
    }
}
 
Example #16
Source File: HttpSessionImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
public HttpSessionImpl(final String contextPath, final long timeout) {
    this.timeout = timeout;
    if (contextPath == null) {
        return;
    }

    this.listeners = LightweightWebAppBuilderListenerExtractor.findByTypeForContext(contextPath, HttpSessionListener.class);
}
 
Example #17
Source File: HttpSessionImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void callListeners() {
    if (!this.listeners.isEmpty()) {
        final HttpSessionEvent event = new HttpSessionEvent(this);
        for (final HttpSessionListener o : this.listeners) {
            HttpSessionListener.class.cast(o).sessionCreated(event);
        }
    }
}
 
Example #18
Source File: HttpSessionImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidate() {
    if (!valid) {
        return;
    }

    synchronized (this) {
        if (!valid) {
            return;
        }

        if (!listeners.isEmpty()) {
            final HttpSessionEvent event = new HttpSessionEvent(this);
            for (final HttpSessionListener o : listeners) {
                try {
                    HttpSessionListener.class.cast(o).sessionDestroyed(event);
                } catch (final Throwable th) {
                    // ignore, may be undeployed
                }
            }
        }

        final SessionManager sessionManager = SystemInstance.get().getComponent(SessionManager.class);
        if (sessionManager != null) {
            sessionManager.removeSession(sessionId);
        }
        valid = false;
    }
}
 
Example #19
Source File: StandardSession.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Inform the listeners about the new session.
 *
 */
public void tellNew() {

    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_CREATED_EVENT, null);

    // Notify interested application event listeners
    Context context = manager.getContext();
    Object listeners[] = context.getApplicationLifecycleListeners();
    if (listeners != null && listeners.length > 0) {
        HttpSessionEvent event =
            new HttpSessionEvent(getSession());
        for (int i = 0; i < listeners.length; i++) {
            if (!(listeners[i] instanceof HttpSessionListener))
                continue;
            HttpSessionListener listener =
                (HttpSessionListener) listeners[i];
            try {
                context.fireContainerEvent("beforeSessionCreated",
                        listener);
                listener.sessionCreated(event);
                context.fireContainerEvent("afterSessionCreated", listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                try {
                    context.fireContainerEvent("afterSessionCreated",
                            listener);
                } catch (Exception e) {
                    // Ignore
                }
                manager.getContext().getLogger().error
                    (sm.getString("standardSession.sessionEvent"), t);
            }
        }
    }

}
 
Example #20
Source File: StandardSession.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Inform the listeners about the new session.
 *
 */
public void tellNew() {

    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_CREATED_EVENT, null);

    // Notify interested application event listeners
    Context context = (Context) manager.getContainer();
    Object listeners[] = context.getApplicationLifecycleListeners();
    if (listeners != null) {
        HttpSessionEvent event =
            new HttpSessionEvent(getSession());
        for (int i = 0; i < listeners.length; i++) {
            if (!(listeners[i] instanceof HttpSessionListener))
                continue;
            HttpSessionListener listener =
                (HttpSessionListener) listeners[i];
            try {
                context.fireContainerEvent("beforeSessionCreated",
                        listener);
                listener.sessionCreated(event);
                context.fireContainerEvent("afterSessionCreated", listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                try {
                    context.fireContainerEvent("afterSessionCreated",
                            listener);
                } catch (Exception e) {
                    // Ignore
                }
                manager.getContainer().getLogger().error
                    (sm.getString("standardSession.sessionEvent"), t);
            }
        }
    }

}
 
Example #21
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 #22
Source File: TestHttpSessionNotifier.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Test
public void testSessionCreated() {
  HttpSessionListener listener = mock(HttpSessionListener.class);
  descriptor.addHttpSessionListener(listener);
  notifier.sessionCreated(session);
  verify(listener).sessionCreated(any(HttpSessionEvent.class));
  HttpSessionListener listener2 = mock(HttpSessionListener.class);
  descriptor.addHttpSessionListener(listener2);
  notifier.sessionCreated(session);
  verify(listener, times(2)).sessionCreated(any(HttpSessionEvent.class));
  verify(listener2).sessionCreated(any(HttpSessionEvent.class));
}
 
Example #23
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 #24
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void sessionCreated(final HttpSession session) {
    if(!started) {
        return;
    }
    final HttpSessionEvent sre = new HttpSessionEvent(session);
    for (int i = 0; i < httpSessionListeners.length; ++i) {
        this.<HttpSessionListener>get(httpSessionListeners[i]).sessionCreated(sre);
    }
}
 
Example #25
Source File: ApplicationListeners.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void sessionDestroyed(final HttpSession session) {
    if(!started) {
        return;
    }
    final HttpSessionEvent sre = new HttpSessionEvent(session);
    for (int i = httpSessionListeners.length - 1; i >= 0; --i) {
        ManagedListener listener = httpSessionListeners[i];
        this.<HttpSessionListener>get(listener).sessionDestroyed(sre);
    }
}
 
Example #26
Source File: JettyFactory.java    From freeacs with MIT License 5 votes vote down vote up
public JettyFactory(boolean httpOnly, int maxHttpPostSize, int maxFormKeys,
                    HttpSessionListener httpSessionEventListener) {
  this.httpOnly = httpOnly;
  this.maxHttpPostSize = maxHttpPostSize;
  this.maxFormKeys = maxFormKeys;
  this.httpSessionEventListener = httpSessionEventListener;
}
 
Example #27
Source File: ServletContext.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends EventListener> void addListener(T listener) {
    Objects.requireNonNull(listener);

    boolean addFlag = false;
    ServletEventListenerManager listenerManager = getServletEventListenerManager();
    if(listener instanceof ServletContextAttributeListener){
        listenerManager.addServletContextAttributeListener((ServletContextAttributeListener) listener);
        addFlag = true;
    }
    if(listener instanceof ServletRequestListener){
        listenerManager.addServletRequestListener((ServletRequestListener) listener);
        addFlag = true;
    }
    if(listener instanceof ServletRequestAttributeListener){
        listenerManager.addServletRequestAttributeListener((ServletRequestAttributeListener) listener);
        addFlag = true;
    }
    if(listener instanceof HttpSessionIdListener){
        listenerManager.addHttpSessionIdListenerListener((HttpSessionIdListener) listener);
        addFlag = true;
    }
    if(listener instanceof HttpSessionAttributeListener){
        listenerManager.addHttpSessionAttributeListener((HttpSessionAttributeListener) listener);
        addFlag = true;
    }
    if(listener instanceof HttpSessionListener){
        listenerManager.addHttpSessionListener((HttpSessionListener) listener);
        addFlag = true;
    }
    if(listener instanceof ServletContextListener){
        listenerManager.addServletContextListener((ServletContextListener) listener);
        addFlag = true;
    }
    if(!addFlag){
        throw new IllegalArgumentException("applicationContext.addListener.iae.wrongType"+
                listener.getClass().getName());
    }
}
 
Example #28
Source File: DefaultHttpSessionManager.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a listener.
 *
 * @param <T> the type.
 * @param listener the listener.
 */
@Override
public <T extends EventListener> void addListener(T listener) {
    if (listener instanceof HttpSessionAttributeListener) {
        attributeListeners.add((HttpSessionAttributeListener) listener);
    }

    if (listener instanceof HttpSessionIdListener) {
        idListeners.add((HttpSessionIdListener) listener);
    }

    if (listener instanceof HttpSessionListener) {
        sessionListeners.add((HttpSessionListener) listener);
    }
}
 
Example #29
Source File: StandardSession.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Inform the listeners about the new session.
 *
 */
public void tellNew() {

    // Notify interested session event listeners
    fireSessionEvent(Session.SESSION_CREATED_EVENT, null);

    // Notify interested application event listeners
    Context context = (Context) manager.getContainer();
    Object listeners[] = context.getApplicationLifecycleListeners();
    if (listeners != null) {
        HttpSessionEvent event =
            new HttpSessionEvent(getSession());
        for (int i = 0; i < listeners.length; i++) {
            if (!(listeners[i] instanceof HttpSessionListener))
                continue;
            HttpSessionListener listener =
                (HttpSessionListener) listeners[i];
            try {
                context.fireContainerEvent("beforeSessionCreated",
                        listener);
                listener.sessionCreated(event);
                context.fireContainerEvent("afterSessionCreated", listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                try {
                    context.fireContainerEvent("afterSessionCreated",
                            listener);
                } catch (Exception e) {
                    // Ignore
                }
                manager.getContainer().getLogger().error
                    (sm.getString("standardSession.sessionEvent"), t);
            }
        }
    }

}
 
Example #30
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()));
    }
}