Search in sources :

Example 1 with Listener

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);
    }
}
Also used : Listener(org.jboss.weld.environment.servlet.Listener) WeldContainer(org.jboss.weld.environment.se.WeldContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 2 with Listener

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);
}
Also used : Listener(org.jboss.weld.environment.servlet.Listener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 3 with Listener

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;
}
Also used : Listener(org.jboss.weld.environment.servlet.Listener) BeanManagerResourceBindingListener(org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener) Server(org.eclipse.jetty.server.Server) BeanManagerResourceBindingListener(org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType) CXFCdiServlet(org.apache.cxf.cdi.CXFCdiServlet)

Example 4 with Listener

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();
        }
    }
}
Also used : Listener(org.jboss.weld.environment.servlet.Listener) Server(org.eclipse.jetty.server.Server) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Page(com.gargoylesoftware.htmlunit.Page) ContainerInitialized(org.jboss.weld.environment.se.events.ContainerInitialized) WebClient(com.gargoylesoftware.htmlunit.WebClient) Weld(org.jboss.weld.environment.se.Weld) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)4 Listener (org.jboss.weld.environment.servlet.Listener)4 Weld (org.jboss.weld.environment.se.Weld)3 Test (org.junit.Test)3 Server (org.eclipse.jetty.server.Server)2 WeldContainer (org.jboss.weld.environment.se.WeldContainer)2 Page (com.gargoylesoftware.htmlunit.Page)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 DispatcherType (javax.servlet.DispatcherType)1 CXFCdiServlet (org.apache.cxf.cdi.CXFCdiServlet)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 ContainerInitialized (org.jboss.weld.environment.se.events.ContainerInitialized)1 BeanManagerResourceBindingListener (org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener)1