下面列出了怎么用javax.servlet.http.HttpSessionIdListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
}
/**
* 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;
}
/**
* 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);
}
}
}
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;
}
/**
* Inform the listeners about the change session ID.
*
* @param newId new session ID
* @param oldId old session ID
* @param notifySessionListeners Should any associated sessionListeners be
* notified that session ID has been changed?
* @param notifyContainerListeners Should any associated ContainerListeners
* be notified that session ID has been changed?
*/
@Override
public void tellChangedSessionId(String newId, String oldId,
boolean notifySessionListeners, boolean notifyContainerListeners) {
Context context = manager.getContext();
// notify ContainerListeners
if (notifyContainerListeners) {
context.fireContainerEvent(Context.CHANGE_SESSION_ID_EVENT,
new String[] {oldId, newId});
}
// notify HttpSessionIdListener
if (notifySessionListeners) {
Object listeners[] = context.getApplicationEventListeners();
if (listeners != null && listeners.length > 0) {
HttpSessionEvent event =
new HttpSessionEvent(getSession());
for(Object listener : listeners) {
if (!(listener instanceof HttpSessionIdListener))
continue;
HttpSessionIdListener idListener =
(HttpSessionIdListener)listener;
try {
idListener.sessionIdChanged(event, oldId);
} catch (Throwable t) {
manager.getContext().getLogger().error
(sm.getString("standardSession.sessionEvent"), t);
}
}
}
}
}
@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()));
}
}
public void httpSessionIdChanged(final HttpSession session, final String oldSessionId) {
if(!started) {
return;
}
final HttpSessionEvent sre = new HttpSessionEvent(session);
for (int i = 0; i < httpSessionIdListeners.length; ++i) {
this.<HttpSessionIdListener>get(httpSessionIdListeners[i]).sessionIdChanged(sre, oldSessionId);
}
}
@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());
}
}
/**
* 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);
}
}
public void httpSessionIdChanged(final HttpSession session, final String oldSessionId) {
if(!started) {
return;
}
final HttpSessionEvent sre = new HttpSessionEvent(session);
for (int i = 0; i < httpSessionIdListeners.length; ++i) {
this.<HttpSessionIdListener>get(httpSessionIdListeners[i]).sessionIdChanged(sre, oldSessionId);
}
}
@Override
public void sessionIdChanged(RepositoryBackedSession session, String oldId) {
// Session id change is only supported for Servlet 3.1+
if (!ServletLevel.isServlet31) {
return;
}
if (session instanceof HttpSession) {
HttpSessionEvent event = new HttpSessionEvent((HttpSession)session);
for (EventListener listener : descriptor.getHttpSessionIdListeners()) {
((HttpSessionIdListener)listener).sessionIdChanged(event, oldId);
}
}
}
@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));
}
/**
* Call any session id listeners registered.
* Usually done by renewSessionId() method, but that is not used in appengine.
* @param session
* @param oldId
*/
public void callSessionIdListeners (AbstractSession session, String oldId) {
HttpSessionEvent event = new HttpSessionEvent(session);
for (HttpSessionIdListener l:_sessionIdListeners)
{
l.sessionIdChanged(event, oldId);
}
}