下面列出了怎么用javax.servlet.ServletContextAttributeListener的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;
}
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;
}
@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);
}
}
}
@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 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);
}
}
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);
}
}
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);
}
}
@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()));
}
}
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);
}
}
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);
}
}
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);
}
}
@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()));
}
}
/**
* 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);
}
}
}
/**
* 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);
}
}
}