@Override public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); cdiContainer = (CdiContainer) context.getAttribute("org.ops4j.pax.cdi.container"); cdiContainer.start(context); WeldManager manager = cdiContainer.unwrap(WeldManager.class); CdiInstanceFactoryBuilder builder = new CdiInstanceFactoryBuilder(manager); @SuppressWarnings("unchecked") Map<String, Object> attributes = (Map<String, Object>) context.getAttribute("org.ops4j.pax.web.attributes"); if (attributes != null) { attributes.put("org.ops4j.pax.cdi.ClassIntrospecter", builder); log.info("registered CdiInstanceFactoryBuilder for Undertow"); } context.setAttribute("org.ops4j.pax.cdi.BeanManager", cdiContainer.getBeanManager()); JspFactory jspFactory = JspFactory.getDefaultFactory(); if (jspFactory != null) { JspApplicationContext jspApplicationContext = jspFactory.getJspApplicationContext(context); jspApplicationContext.addELResolver(manager.getELResolver()); jspApplicationContext.addELContextListener(new WeldELContextListener()); } super.contextInitialized(sce); }
@Test public void checkContainers() throws InterruptedException { // assertThat(containerFactory.getProviderName(), // is("org.apache.webbeans.config.WebBeansContext")); assertThat(containerFactory.getContainers().size(), is(2)); List<String> beanBundles = new ArrayList<String>(); for (CdiContainer container : containerFactory.getContainers()) { beanBundles.add(container.getBundle().getSymbolicName()); } assertThat( beanBundles, hasItems("org.ops4j.pax.cdi.sample1", "org.ops4j.pax.cdi.sample1.client")); assertThat(beanBundles.size(), is(2)); }
@Override public void contextDestroyed(ServletContextEvent sce) { cdiContainer.stop(); ServletContext context = sce.getServletContext(); context.removeAttribute("org.ops4j.pax.cdi.ClassIntrospecter"); context.removeAttribute("org.ops4j.pax.cdi.BeanManager"); super.contextDestroyed(sce); }