use of javax.servlet.ServletContextEvent in project undertow by undertow-io.
the class ApplicationListeners method contextInitialized.
public void contextInitialized() {
if (!started) {
return;
}
//new listeners can be added here, so we don't use an iterator
final ServletContextEvent event = new ServletContextEvent(servletContext);
for (int i = 0; i < servletContextListeners.length; ++i) {
ManagedListener listener = servletContextListeners[i];
IN_PROGRAMATIC_SC_LISTENER_INVOCATION.set(listener.isProgramatic() ? PROGRAMATIC_LISTENER : DECLARED_LISTENER);
try {
this.<ServletContextListener>get(listener).contextInitialized(event);
} finally {
IN_PROGRAMATIC_SC_LISTENER_INVOCATION.remove();
}
}
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderListenerWithDefaultContext.
@Test
public void testContextLoaderListenerWithDefaultContext() {
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);
WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
assertTrue("Has father", context.containsBean("father"));
assertTrue("Has rod", context.containsBean("rod"));
assertTrue("Has kerry", context.containsBean("kerry"));
assertTrue("Not destroyed", !lb.isDestroyed());
assertFalse(context.containsBean("beans1.bean1"));
assertFalse(context.containsBean("beans1.bean2"));
listener.contextDestroyed(event);
assertTrue("Destroyed", lb.isDestroyed());
assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderListenerWithUnknownContextInitializer.
@Test
public void testContextLoaderListenerWithUnknownContextInitializer() {
MockServletContext sc = new MockServletContext("");
// config file doesn't matter. just a placeholder
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/empty-context.xml");
sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] { UnknownContextInitializer.class.getName() }));
ContextLoaderListener listener = new ContextLoaderListener();
try {
listener.contextInitialized(new ServletContextEvent(sc));
fail("expected exception");
} catch (ApplicationContextException ex) {
assertTrue(ex.getMessage().contains("not assignable"));
}
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class ContextLoaderTests method testContextLoaderListenerWithProgrammaticAndLocalInitializers.
@Test
public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() {
MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
ContextLoaderListener listener = new ContextLoaderListener();
listener.setContextInitializers(new TestWebContextInitializer());
listener.contextInitialized(new ServletContextEvent(sc));
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
TestBean testBean = wac.getBean(TestBean.class);
assertThat(testBean.getName(), equalTo("testName"));
assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.
the class Spr8510Tests method abstractRefreshableWAC_fallsBackToConventionBasedNaming.
/**
* If context config locations have been specified neither against the application
* context nor the context loader listener, then fall back to default values.
*/
@Test
public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() {
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
//ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
ContextLoaderListener cll = new ContextLoaderListener(ctx);
MockServletContext sc = new MockServletContext();
try {
cll.contextInitialized(new ServletContextEvent(sc));
fail("expected exception");
} catch (Throwable t) {
// assert that an attempt was made to load the correct XML
System.out.println(t.getMessage());
assertTrue(t.getMessage().endsWith("Could not open ServletContext resource [/WEB-INF/applicationContext.xml]"));
}
}
Aggregations