use of org.jboss.weld.environment.servlet.Listener in project core by weld.
the class WeldSeBuilderTest method testPassingWeldSeContainerToWeldServletViaParam.
@Test
public void testPassingWeldSeContainerToWeldServletViaParam() throws Exception {
try (WeldContainer weld = new Weld().disableDiscovery().beanClasses(Cat.class).initialize()) {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addEventListener(new Listener());
context.setAttribute(Listener.CONTAINER_ATTRIBUTE_NAME, weld);
test(context);
}
}
use of org.jboss.weld.environment.servlet.Listener in project core by weld.
the class WeldSeBuilderTest method testPassingWeldSeBuilderToWeldServletViaParam.
@Test
public void testPassingWeldSeBuilderToWeldServletViaParam() throws Exception {
Weld builder = new Weld().disableDiscovery().beanClasses(Cat.class);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addEventListener(new Listener());
context.setAttribute(Listener.CONTAINER_ATTRIBUTE_NAME, builder);
test(context);
}
use of org.jboss.weld.environment.servlet.Listener in project fabric8 by fabric8io.
the class Servers method startServer.
public static Server startServer(String appName, Function<ServletContextHandler, Void> contextCallback, String defaultPort) throws Exception {
String port = Systems.getEnvVarOrSystemProperty("HTTP_PORT", "HTTP_PORT", defaultPort);
Integer num = Integer.parseInt(port);
String service = Systems.getEnvVarOrSystemProperty("WEB_CONTEXT_PATH", "WEB_CONTEXT_PATH", "");
String servicesPath = "cxf/servicesList";
String servletContextPath = "/" + service;
ManagedApi.setSingletonCxfServletContext(servletContextPath);
String url = "http://localhost:" + port + servletContextPath;
if (!url.endsWith("/")) {
url += "/";
}
System.out.println();
System.out.println("-------------------------------------------------------------");
System.out.println(appName + " is now running at: " + url);
System.out.println("-------------------------------------------------------------");
System.out.println();
final Server server = new Server(num);
// Register and map the dispatcher servlet
final ServletHolder servletHolder = new ServletHolder(new CXFCdiServlet());
// change default service list URI
servletHolder.setInitParameter("service-list-path", "/" + servicesPath);
final ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addEventListener(new Listener());
context.addEventListener(new BeanManagerResourceBindingListener());
String servletPath = "/*";
if (Strings.isNotBlank(service)) {
servletPath = servletContextPath + "/*";
}
context.addServlet(servletHolder, servletPath);
server.setHandler(context);
EnumSet<DispatcherType> dispatches = EnumSet.allOf(DispatcherType.class);
context.addFilter(RestCorsFilter.class, "/*", dispatches);
if (contextCallback != null) {
contextCallback.apply(context);
}
server.start();
return server;
}
use of org.jboss.weld.environment.servlet.Listener in project core by weld.
the class BootstrapNotNeededTest method testBootstrapNotNeeded.
@Test
public void testBootstrapNotNeeded() throws Exception {
String id = UUID.randomUUID().toString();
// First boostrap Weld SE
try (WeldContainer container = new Weld(id).initialize()) {
TestBean testBean = container.instance().select(TestBean.class).get();
assertNotNull(testBean);
// @Initialized(ApplicationScoped.class) ContainerInitialized
List<Object> initEvents = testBean.getInitEvents();
assertEquals(1, initEvents.size());
Object event = initEvents.get(0);
assertTrue(event instanceof ContainerInitialized);
assertEquals(id, ((ContainerInitialized) event).getContainerId());
// Test CDIProvider
CDI<Object> cdi = CDI.current();
assertTrue(cdi instanceof WeldContainer);
// Then start Jetty
Server server = new Server(InetSocketAddress.createUnresolved("localhost", 8080));
try {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(TestServlet.class, "/test");
context.setAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME, container.getBeanManager());
context.addEventListener(new Listener());
server.start();
// @Initialized(ApplicationScoped.class) ServletContext not fired
assertEquals(1, initEvents.size());
WebClient webClient = new WebClient();
webClient.setThrowExceptionOnFailingStatusCode(true);
Page page = webClient.getPage("http://localhost:8080/test");
assertEquals(testBean.getId(), page.getWebResponse().getContentAsString().trim());
} finally {
server.stop();
}
}
}
Aggregations