类javax.servlet.http.HttpSessionAttributeListener源码实例Demo

下面列出了怎么用javax.servlet.http.HttpSessionAttributeListener的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: piranha   文件: DefaultWebApplication.java
/**
 * 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);
    }
}
 
源代码2 项目: piranha   文件: DefaultWebApplication.java
/**
 * 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;
}
 
源代码3 项目: HttpSessionReplacer   文件: SessionHelpers.java
/**
 * 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);
    }
  }
}
 
源代码4 项目: tomee   文件: WebContext.java
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;
}
 
源代码5 项目: Tomcat8-Source-Read   文件: ApplicationContext.java
@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()));
    }
}
 
源代码6 项目: quarkus-http   文件: ApplicationListeners.java
public void httpSessionAttributeAdded(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeAdded(sre);
    }
}
 
源代码7 项目: quarkus-http   文件: ApplicationListeners.java
public void httpSessionAttributeRemoved(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeRemoved(sre);
    }
}
 
源代码8 项目: quarkus-http   文件: ApplicationListeners.java
public void httpSessionAttributeReplaced(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeReplaced(sre);
    }
}
 
源代码9 项目: spring-boot-protocol   文件: ServletContext.java
@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());
    }
}
 
源代码10 项目: piranha   文件: DefaultHttpSessionManager.java
/**
 * 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);
    }
}
 
源代码11 项目: Tomcat7.0.67   文件: ApplicationContext.java
@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()));
    }
}
 
源代码12 项目: lams   文件: ApplicationListeners.java
public void httpSessionAttributeAdded(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeAdded(sre);
    }
}
 
源代码13 项目: lams   文件: ApplicationListeners.java
public void httpSessionAttributeRemoved(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeRemoved(sre);
    }
}
 
源代码14 项目: lams   文件: ApplicationListeners.java
public void httpSessionAttributeReplaced(final HttpSession session, final String name, final Object value) {
    if(!started) {
        return;
    }
    final HttpSessionBindingEvent sre = new HttpSessionBindingEvent(session, name, value);
    for (int i = 0; i < httpSessionAttributeListeners.length; ++i) {
        this.<HttpSessionAttributeListener>get(httpSessionAttributeListeners[i]).attributeReplaced(sre);
    }
}
 
源代码15 项目: HttpSessionReplacer   文件: HttpSessionNotifier.java
/**
 * Notifies listeners that attribute was added. See {@link SessionNotifier}
 * {@link #attributeAdded(RepositoryBackedSession, String, Object)}.
 * <p>
 * If the added attribute <code>value</code> is a HttpSessionBindingListener,
 * it will receive the {@link HttpSessionBindingEvent}. If there are
 * {@link HttpSessionAttributeListener} instances associated to
 * {@link ServletContext}, they will be notified via
 * {@link HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)}
 * .
 */
@Override
public void attributeAdded(RepositoryBackedSession session, String key, Object value) {
  // If the
  if (session instanceof HttpSession && value instanceof HttpSessionBindingListener) {
    ((HttpSessionBindingListener)value).valueBound(new HttpSessionBindingEvent((HttpSession)session, key));
  }
  HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key, value);
  for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) {
    listener.attributeAdded(event);
  }
}
 
源代码16 项目: HttpSessionReplacer   文件: TestSessionHelpers.java
@Test
public void testOnAddListener() {
  ServletContextDescriptor scd = new ServletContextDescriptor(servletContext);
  when(servletContext.getAttribute(Attributes.SERVLET_CONTEXT_DESCRIPTOR)).thenReturn(scd);
  sessionHelpers.onAddListener(servletContext, "Dummy");
  assertTrue(scd.getHttpSessionListeners().isEmpty());
  assertTrue(scd.getHttpSessionIdListeners().isEmpty());
  assertTrue(scd.getHttpSessionAttributeListeners().isEmpty());
  HttpSessionListener listener = mock(HttpSessionListener.class);
  HttpSessionIdListener idListener = mock(HttpSessionIdListener.class);
  HttpSessionAttributeListener attributeListener = mock(HttpSessionAttributeListener.class);
  HttpSessionListener multiListener = mock(HttpSessionListener.class,
      withSettings().extraInterfaces(HttpSessionAttributeListener.class));
  HttpSessionAttributeListener attributeMultiListener = (HttpSessionAttributeListener)multiListener;
  sessionHelpers.onAddListener(servletContext, listener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertTrue(scd.getHttpSessionIdListeners().isEmpty());
  assertTrue(scd.getHttpSessionAttributeListeners().isEmpty());
  sessionHelpers.onAddListener(servletContext, idListener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener));
  assertTrue(scd.getHttpSessionAttributeListeners().isEmpty());
  sessionHelpers.onAddListener(servletContext, attributeListener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener));
  assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeListener));
  sessionHelpers.onAddListener(servletContext, multiListener);
  assertThat(scd.getHttpSessionListeners(), hasItem(listener));
  assertThat(scd.getHttpSessionListeners(), hasItem(multiListener));
  assertThat(scd.getHttpSessionIdListeners(), hasItem(idListener));
  assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeListener));
  assertThat(scd.getHttpSessionAttributeListeners(), hasItem(attributeMultiListener));
}
 
@Test
public void testAttributeAdded() {
  HttpSessionAttributeListener listener = mock(HttpSessionAttributeListener.class);
  descriptor.addHttpSessionAttributeListener(listener);
  notifier.attributeAdded(session, "Test", "value");
  verify(listener).attributeAdded(any(HttpSessionBindingEvent.class));
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  notifier.attributeAdded(session, "Test", bindingListener);
  verify(listener, times(2)).attributeAdded(any(HttpSessionBindingEvent.class));
  verify(bindingListener).valueBound(any(HttpSessionBindingEvent.class));
}
 
@Test
public void testAttributeReplaced() {
  HttpSessionAttributeListener listener = mock(HttpSessionAttributeListener.class);
  notifier.attributeReplaced(session, "Test", "very-old-value");
  verify(listener, never()).attributeReplaced(any(HttpSessionBindingEvent.class));
  descriptor.addHttpSessionAttributeListener(listener);
  notifier.attributeReplaced(session, "Test", "old-value");
  verify(listener).attributeReplaced(any(HttpSessionBindingEvent.class));
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  notifier.attributeReplaced(session, "Test", bindingListener);
  verify(listener, times(2)).attributeReplaced(any(HttpSessionBindingEvent.class));
  verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class));
}
 
@Test
public void testAttributeRemoved() {
  notifier.attributeRemoved(session, "Test", "very-old-value");
  HttpSessionAttributeListener listener = mock(HttpSessionAttributeListener.class);
  descriptor.addHttpSessionAttributeListener(listener);
  notifier.attributeRemoved(session, "Test", "old-value");
  verify(listener).attributeRemoved(any(HttpSessionBindingEvent.class));
  HttpSessionBindingListener bindingListener = mock(HttpSessionBindingListener.class);
  notifier.attributeRemoved(session, "Test", bindingListener);
  verify(listener, times(2)).attributeRemoved(any(HttpSessionBindingEvent.class));
  verify(bindingListener).valueUnbound(any(HttpSessionBindingEvent.class));
}
 
源代码20 项目: tomcatsrc   文件: ApplicationContext.java
@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()));
    }
}
 
源代码21 项目: HttpSessionReplacer   文件: HttpSessionNotifier.java
/**
 * Notifies listeners that attribute was replaced. See {@link SessionNotifier}
 * {@link #attributeReplaced(RepositoryBackedSession, String, Object)}.
 * <p>
 * If the the old value of attribute <code>replacedValue</code> is a
 * HttpSessionBindingListener, it will receive the
 * {@link HttpSessionBindingEvent}. If there are
 * {@link HttpSessionAttributeListener} instances associated to
 * {@link ServletContext}, they will be notified via
 * {@link HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)}
 * .
 */
@Override
public void attributeReplaced(RepositoryBackedSession session, String key, Object replacedValue) {
  if (session instanceof HttpSession && replacedValue instanceof HttpSessionBindingListener) {
    ((HttpSessionBindingListener)replacedValue).valueUnbound(new HttpSessionBindingEvent((HttpSession)session, key));
  }
  HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key, replacedValue);
  for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) {
    listener.attributeReplaced(event);
  }
}
 
源代码22 项目: HttpSessionReplacer   文件: HttpSessionNotifier.java
/**
 * Notifies listeners that attribute was removed. See {@link SessionNotifier}
 * {@link #attributeRemoved(RepositoryBackedSession, String, Object)}.
 * <p>
 * If the the old value of attribute <code>removedValue</code> is a
 * HttpSessionBindingListener, it will receive the
 * {@link HttpSessionBindingEvent}. If there are
 * {@link HttpSessionAttributeListener} instances associated to
 * {@link ServletContext}, they will be notified via
 * {@link HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)}
 * .
 */
@Override
public void attributeRemoved(RepositoryBackedSession session, String key, Object removedValue) {
  if (session instanceof HttpSession && removedValue instanceof HttpSessionBindingListener) {
    ((HttpSessionBindingListener)removedValue).valueUnbound(new HttpSessionBindingEvent((HttpSession)session, key));
  }
  HttpSessionBindingEvent event = new HttpSessionBindingEvent((HttpSession)session, key);
  for (HttpSessionAttributeListener listener : descriptor.getHttpSessionAttributeListeners()) {
    listener.attributeRemoved(event);
  }
}
 
/**
 * Adds {@link HttpSessionAttributeListener} associated to
 * {@link ServletContext} to list of known listeners. Listener is added only
 * if it has not been seen before.
 *
 * @param listener
 */
void addHttpSessionAttributeListener(HttpSessionAttributeListener listener) {
  if (httpSessionAttributeListenersSet.add(listener)) {
    httpSessionAttributeListeners.add(listener);
    logger.info("Registered HttpSessionAttributeListener {} for context {}", listener, contextPath);
  }
}
 
/**
 * Returns list of {@link HttpSessionAttributeListener}
 *
 * @return list of listeners or empty if there are nor registered listeners
 */
List<HttpSessionAttributeListener> getHttpSessionAttributeListeners() {
  return httpSessionAttributeListeners;
}