use of jakarta.servlet.ServletContextListener in project tomcat by apache.
the class StandardContext method listenerStop.
/**
* Send an application stop event to all interested listeners.
* @return <code>true</code> if all events were sent successfully,
* or <code>false</code> otherwise.
*/
public boolean listenerStop() {
if (log.isDebugEnabled()) {
log.debug("Sending application stop events");
}
boolean ok = true;
Object[] listeners = getApplicationLifecycleListeners();
if (listeners != null && listeners.length > 0) {
ServletContextEvent event = new ServletContextEvent(getServletContext());
ServletContextEvent tldEvent = null;
if (noPluggabilityServletContext != null) {
tldEvent = new ServletContextEvent(noPluggabilityServletContext);
}
for (int i = 0; i < listeners.length; i++) {
int j = (listeners.length - 1) - i;
if (listeners[j] == null) {
continue;
}
if (listeners[j] instanceof ServletContextListener) {
ServletContextListener listener = (ServletContextListener) listeners[j];
try {
fireContainerEvent("beforeContextDestroyed", listener);
if (noPluggabilityListeners.contains(listener)) {
listener.contextDestroyed(tldEvent);
} else {
listener.contextDestroyed(event);
}
fireContainerEvent("afterContextDestroyed", listener);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
fireContainerEvent("afterContextDestroyed", listener);
getLogger().error(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
ok = false;
}
}
try {
if (getInstanceManager() != null) {
getInstanceManager().destroyInstance(listeners[j]);
}
} catch (Throwable t) {
t = ExceptionUtils.unwrapInvocationTargetException(t);
ExceptionUtils.handleThrowable(t);
getLogger().error(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
ok = false;
}
}
}
// Annotation processing
listeners = getApplicationEventListeners();
if (listeners != null) {
for (int i = 0; i < listeners.length; i++) {
int j = (listeners.length - 1) - i;
if (listeners[j] == null) {
continue;
}
try {
if (getInstanceManager() != null) {
getInstanceManager().destroyInstance(listeners[j]);
}
} catch (Throwable t) {
t = ExceptionUtils.unwrapInvocationTargetException(t);
ExceptionUtils.handleThrowable(t);
getLogger().error(sm.getString("standardContext.listenerStop", listeners[j].getClass().getName()), t);
ok = false;
}
}
}
setApplicationEventListeners(null);
setApplicationLifecycleListeners(null);
noPluggabilityServletContext = null;
noPluggabilityListeners.clear();
return ok;
}
use of jakarta.servlet.ServletContextListener in project tomcat by apache.
the class StandardContext method listenerStart.
/**
* Configure the set of instantiated application event listeners
* for this Context.
* @return <code>true</code> if all listeners wre
* initialized successfully, or <code>false</code> otherwise.
*/
public boolean listenerStart() {
if (log.isDebugEnabled()) {
log.debug("Configuring application event listeners");
}
// Instantiate the required listeners
String[] listeners = findApplicationListeners();
Object[] results = new Object[listeners.length];
boolean ok = true;
for (int i = 0; i < results.length; i++) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(" Configuring event listener class '" + listeners[i] + "'");
}
try {
String listener = listeners[i];
results[i] = getInstanceManager().newInstance(listener);
} catch (Throwable t) {
t = ExceptionUtils.unwrapInvocationTargetException(t);
ExceptionUtils.handleThrowable(t);
getLogger().error(sm.getString("standardContext.applicationListener", listeners[i]), t);
ok = false;
}
}
if (!ok) {
getLogger().error(sm.getString("standardContext.applicationSkipped"));
return false;
}
// Sort listeners in two arrays
List<Object> eventListeners = new ArrayList<>();
List<Object> lifecycleListeners = new ArrayList<>();
for (Object result : results) {
if ((result instanceof ServletContextAttributeListener) || (result instanceof ServletRequestAttributeListener) || (result instanceof ServletRequestListener) || (result instanceof HttpSessionIdListener) || (result instanceof HttpSessionAttributeListener)) {
eventListeners.add(result);
}
if ((result instanceof ServletContextListener) || (result instanceof HttpSessionListener)) {
lifecycleListeners.add(result);
}
}
// Listener instances may have been added directly to this Context by
// ServletContextInitializers and other code via the pluggability APIs.
// Put them these listeners after the ones defined in web.xml and/or
// annotations then overwrite the list of instances with the new, full
// list.
eventListeners.addAll(Arrays.asList(getApplicationEventListeners()));
setApplicationEventListeners(eventListeners.toArray());
for (Object lifecycleListener : getApplicationLifecycleListeners()) {
lifecycleListeners.add(lifecycleListener);
if (lifecycleListener instanceof ServletContextListener) {
noPluggabilityListeners.add(lifecycleListener);
}
}
setApplicationLifecycleListeners(lifecycleListeners.toArray());
if (getLogger().isDebugEnabled()) {
getLogger().debug("Sending application start events");
}
// Ensure context is not null
getServletContext();
context.setNewServletContextListenerAllowed(false);
Object[] instances = getApplicationLifecycleListeners();
if (instances == null || instances.length == 0) {
return ok;
}
ServletContextEvent event = new ServletContextEvent(getServletContext());
ServletContextEvent tldEvent = null;
if (noPluggabilityListeners.size() > 0) {
noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
tldEvent = new ServletContextEvent(noPluggabilityServletContext);
}
for (Object instance : instances) {
if (!(instance instanceof ServletContextListener)) {
continue;
}
ServletContextListener listener = (ServletContextListener) instance;
try {
fireContainerEvent("beforeContextInitialized", listener);
if (noPluggabilityListeners.contains(listener)) {
listener.contextInitialized(tldEvent);
} else {
listener.contextInitialized(event);
}
fireContainerEvent("afterContextInitialized", listener);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
fireContainerEvent("afterContextInitialized", listener);
getLogger().error(sm.getString("standardContext.listenerStart", instance.getClass().getName()), t);
ok = false;
}
}
return ok;
}
use of jakarta.servlet.ServletContextListener in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderWithCustomContext.
@Test
void contextLoaderWithCustomContext() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.servlet.SimpleWebApplicationContext");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
boolean condition = wc instanceof SimpleWebApplicationContext;
assertThat(condition).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
}
use of jakarta.servlet.ServletContextListener in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderWithInvalidContext.
@Test
void contextLoaderWithInvalidContext() throws Exception {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.context.support.InvalidWebApplicationContext");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> listener.contextInitialized(event)).withCauseInstanceOf(ClassNotFoundException.class);
}
use of jakarta.servlet.ServletContextListener in project spring-framework by spring-projects.
the class ContextLoaderTests method contextLoaderListenerWithDefaultContext.
@Test
void contextLoaderListenerWithDefaultContext() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
boolean condition1 = context instanceof XmlWebApplicationContext;
assertThat(condition1).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
assertThat(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext).isTrue();
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
assertThat(context.containsBean("father")).as("Has father").isTrue();
assertThat(context.containsBean("rod")).as("Has rod").isTrue();
assertThat(context.containsBean("kerry")).as("Has kerry").isTrue();
boolean condition = !lb.isDestroyed();
assertThat(condition).as("Not destroyed").isTrue();
assertThat(context.containsBean("beans1.bean1")).isFalse();
assertThat(context.containsBean("beans1.bean2")).isFalse();
listener.contextDestroyed(event);
assertThat(lb.isDestroyed()).as("Destroyed").isTrue();
assertThat(sc.getAttribute(contextAttr)).isNull();
assertThat(WebApplicationContextUtils.getWebApplicationContext(sc)).isNull();
}
Aggregations