use of javax.servlet.ServletContextEvent in project jetty.project by eclipse.
the class WebAppContextTest method testServletContextListener.
@Test
public void testServletContextListener() throws Exception {
Server server = new Server();
HotSwapHandler swap = new HotSwapHandler();
server.setHandler(swap);
server.start();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("java.io.tmpdir"));
final List<String> history = new ArrayList<>();
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I0");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D0");
}
});
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I1");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D1");
throw new RuntimeException("Listener1 destroy broken");
}
});
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I2");
throw new RuntimeException("Listener2 init broken");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D2");
}
});
context.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
history.add("I3");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
history.add("D3");
}
});
try {
swap.setHandler(context);
context.start();
} catch (Exception e) {
history.add(e.getMessage());
} finally {
try {
swap.setHandler(null);
} catch (Exception e) {
while (e.getCause() instanceof Exception) e = (Exception) e.getCause();
history.add(e.getMessage());
} finally {
}
}
Assert.assertThat(history, Matchers.contains("I0", "I1", "I2", "Listener2 init broken", "D1", "D0", "Listener1 destroy broken"));
server.stop();
}
use of javax.servlet.ServletContextEvent in project jetty.project by eclipse.
the class ContextHandler method startContext.
/* ------------------------------------------------------------ */
/**
* Extensible startContext. this method is called from {@link ContextHandler#doStart()} instead of a call to super.doStart(). This allows derived classes to
* insert additional handling (Eg configuration) before the call to super.doStart by this method will start contained handlers.
* @throws Exception if unable to start the context
*
* @see org.eclipse.jetty.server.handler.ContextHandler.Context
*/
protected void startContext() throws Exception {
String managedAttributes = _initParams.get(MANAGED_ATTRIBUTES);
if (managedAttributes != null)
addEventListener(new ManagedAttributeListener(this, StringUtil.csvSplit(managedAttributes)));
super.doStart();
// Call context listeners
_destroySerletContextListeners.clear();
if (!_servletContextListeners.isEmpty()) {
ServletContextEvent event = new ServletContextEvent(_scontext);
for (ServletContextListener listener : _servletContextListeners) {
callContextInitialized(listener, event);
_destroySerletContextListeners.add(listener);
}
}
}
use of javax.servlet.ServletContextEvent in project jetty.project by eclipse.
the class ServletContextHandlerTest method testReplaceHandler.
@Test
public void testReplaceHandler() throws Exception {
ServletContextHandler servletContextHandler = new ServletContextHandler();
ServletHolder sh = new ServletHolder(new TestServlet());
servletContextHandler.addServlet(sh, "/foo");
final AtomicBoolean contextInit = new AtomicBoolean(false);
final AtomicBoolean contextDestroy = new AtomicBoolean(false);
servletContextHandler.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
if (sce.getServletContext() != null)
contextInit.set(true);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
if (sce.getServletContext() != null)
contextDestroy.set(true);
}
});
ServletHandler shandler = servletContextHandler.getServletHandler();
ResourceHandler rh = new ResourceHandler();
servletContextHandler.insertHandler(rh);
assertEquals(shandler, servletContextHandler.getServletHandler());
assertEquals(rh, servletContextHandler.getHandler());
assertEquals(rh.getHandler(), shandler);
_server.setHandler(servletContextHandler);
_server.start();
assertTrue(contextInit.get());
_server.stop();
assertTrue(contextDestroy.get());
}
use of javax.servlet.ServletContextEvent in project che by eclipse.
the class CheBootstrapTest method environment_variables_prefixed_with_che_underscore_convert_double_underscores_into_one_underscore_in_variable_name.
@Test
public void environment_variables_prefixed_with_che_underscore_convert_double_underscores_into_one_underscore_in_variable_name() throws Exception {
Properties cheProperties = new Properties();
cheProperties.put("che.some.other.name_with_underscores", "che_value");
cheProperties.put("che.some.name", "NULL");
writePropertiesFile(che, "che.properties", cheProperties);
Properties userProperties = new Properties();
userProperties.put("che.some.other.name_with_underscores", "user_value");
writePropertiesFile(userCongDir, "user.properties", userProperties);
systemPropertiesHelper.property("che.some.other.name_with_underscores", "che_dot_system_property_value");
ModuleScanner.modules.add(binder -> binder.bind(TestConfOverrideWithUnderscoresComponent.class));
cheBootstrap.contextInitialized(new ServletContextEvent(servletContext));
Injector injector = retrieveComponentFromServletContext(Injector.class);
TestConfOverrideWithUnderscoresComponent testComponent = injector.getInstance(TestConfOverrideWithUnderscoresComponent.class);
assertEquals(testComponent.otherString, System.getenv("CHE_SOME_OTHER_NAME__WITH__UNDERSCORES"));
}
use of javax.servlet.ServletContextEvent in project che by eclipse.
the class CheBootstrapTest method system_properties_prefixed_with_che_dot_override_user_specified_and_che_properties.
@Test
public void system_properties_prefixed_with_che_dot_override_user_specified_and_che_properties() throws Exception {
Properties cheProperties = new Properties();
cheProperties.put("che.some.name", "che_value");
cheProperties.put("che.some.other.name", "NULL");
writePropertiesFile(che, "che.properties", cheProperties);
Properties userProperties = new Properties();
userProperties.put("che.some.name", "user_value");
writePropertiesFile(userCongDir, "user.properties", userProperties);
systemPropertiesHelper.property("che.some.name", "che_dot_system_property_value");
ModuleScanner.modules.add(binder -> binder.bind(TestConfOverrideComponent.class));
cheBootstrap.contextInitialized(new ServletContextEvent(servletContext));
Injector injector = retrieveComponentFromServletContext(Injector.class);
TestConfOverrideComponent testComponent = injector.getInstance(TestConfOverrideComponent.class);
assertEquals(testComponent.string, "che_dot_system_property_value");
}
Aggregations