@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);
  }
Example #2
0
 /**
  * @param id
  * @param manager
  * @param bootstrap
  */
 private WeldContainer(String id, WeldManager manager, Bootstrap bootstrap) {
   super();
   this.id = id;
   this.manager = manager;
   this.bootstrap = bootstrap;
   this.creationalContext = manager.createCreationalContext(null);
   BeanManagerImpl beanManagerImpl = ((BeanManagerImpl) manager.unwrap());
   this.instance = beanManagerImpl.getInstance(creationalContext);
   this.event = beanManagerImpl.event();
 }
Example #3
0
 public static <T extends Context> T getActiveContext(WeldManager beanManager, Class<T> type) {
   for (T context : beanManager.instance().select(type)) {
     if (context.isActive()) {
       return context;
     }
   }
   throw new ContextNotActiveException();
 }
  public void createRequest(@Observes Before event, WeldManager manager) {
    BoundRequestContext requestContext = manager.instance().select(BoundRequestContext.class).get();

    CDIRequestMap map = new CDIRequestMap();
    requestContext.associate(map);
    requestContext.activate();
    requestMap.set(map);
  }
  public void createSession(@Observes AfterDeploy event, WeldManager manager) {
    BoundSessionContext sessionContext = manager.instance().select(BoundSessionContext.class).get();

    CDISessionMap map = new CDISessionMap();
    sessionContext.associate(map);
    sessionContext.activate();
    sessionMap.set(map);
  }
  public void destroySession(@Observes BeforeUnDeploy event, WeldManager manager) {
    BoundSessionContext sessionContext = manager.instance().select(BoundSessionContext.class).get();

    CDISessionMap map = sessionMap.get();
    if (map != null) {
      try {
        sessionContext.invalidate();
        sessionContext.deactivate();
      } finally {
        sessionContext.dissociate(map);
      }
    }
  }
  public void destroyRequest(@Observes After event, WeldManager manager) {
    BoundRequestContext requestContext = manager.instance().select(BoundRequestContext.class).get();

    CDIRequestMap map = requestMap.get();
    if (map != null) {
      try {
        requestContext.invalidate();
        requestContext.deactivate();
      } finally {
        requestContext.dissociate(map);
        map.clear();
      }
    }
  }
  public void createConversation(@Observes(precedence = -1) Before event, WeldManager manager) {
    if (!enableConversationScope) {
      return;
    }

    CDIConversationID id = conversationId.get();
    if (id == null) {
      id = new CDIConversationID(null); // when null creates a new empty conversation id.
    }

    BoundRequest request = new MutableBoundRequest(requestMap.get(), sessionMap.get());
    this.boundRequest.set(request);

    BoundConversationContext conversationContext =
        manager.instance().select(BoundConversationContext.class).get();
    conversationContext.associate(request);
    conversationContext.activate(id.getId());
  }
Example #9
0
 /**
  * @param id
  * @param manager
  * @param bootstrap
  * @return the initialized Weld container
  */
 static WeldContainer initialize(String id, WeldManager manager, Bootstrap bootstrap) {
   if (SINGLETON.isSet(id)) {
     throw WeldSELogger.LOG.weldContainerAlreadyRunning(id);
   }
   WeldContainer weldContainer = new WeldContainer(id, manager, bootstrap);
   SINGLETON.set(id, weldContainer);
   RUNNING_CONTAINER_IDS.add(id);
   WeldSELogger.LOG.weldContainerInitialized(id);
   manager.fireEvent(new ContainerInitialized(id), InitializedLiteral.APPLICATION);
   // If needed, register one shutdown hook for all containers
   if (isShutdownHookNeeded() && shutdownHook == null) {
     synchronized (LOCK) {
       if (shutdownHook == null) {
         shutdownHook = new ShutdownHook();
         Runtime.getRuntime().addShutdownHook(shutdownHook);
       }
     }
   }
   return weldContainer;
 }
Example #10
0
 /**
  * Shutdown the container.
  *
  * @see Weld#initialize()
  */
 public synchronized void shutdown() {
   if (isRunning()) {
     try {
       manager.fireEvent(new ContainerShutdown(id), DestroyedLiteral.APPLICATION);
     } finally {
       SINGLETON.clear(id);
       RUNNING_CONTAINER_IDS.remove(id);
       // Destroy all the dependent beans correctly
       creationalContext.release();
       bootstrap.shutdown();
       WeldSELogger.LOG.weldContainerShutdown(id);
     }
   } else {
     if (WeldSELogger.LOG.isTraceEnabled()) {
       WeldSELogger.LOG.tracev(
           "Spurious call to shutdown from: {0}",
           (Object[]) Thread.currentThread().getStackTrace());
     }
     throw WeldSELogger.LOG.weldContainerAlreadyShutDown(id);
   }
 }
  public void destroyConversation(@Observes(precedence = 1) After event, WeldManager manager) {
    if (!enableConversationScope) {
      return;
    }

    BoundConversationContext conversationContext =
        manager.instance().select(BoundConversationContext.class).get();
    if (!conversationContext.getCurrentConversation().isTransient()) {
      conversationId.set(
          new CDIConversationID(conversationContext.getCurrentConversation().getId()));
    } else {
      conversationId.set(new CDIConversationID(null));
    }

    BoundRequest request = boundRequest.get();

    try {
      conversationContext.invalidate();
      conversationContext.deactivate();
    } finally {
      conversationContext.dissociate(request);
    }
  }
Example #12
0
  @Override
  public void contextInitialized(ServletContextEvent sce) {

    ClassLoader classLoader = Reflections.getClassLoader();
    ServletContext context = sce.getServletContext();

    URLScanner scanner = createUrlScanner(classLoader, context);
    if (scanner != null) {
      context.setAttribute(URLScanner.class.getName(), scanner);
    }

    ServletDeployment deployment = createServletDeployment(context, bootstrap);
    try {
      deployment
          .getWebAppBeanDeploymentArchive()
          .getServices()
          .add(ResourceInjectionServices.class, new ServletResourceInjectionServices() {});
    } catch (NoClassDefFoundError e) {
      // Support GAE
      log.warn("@Resource injection not available in simple beans");
    }

    bootstrap.startContainer(Environments.SERVLET, deployment).startInitialization();
    WeldManager manager = bootstrap.getManager(deployment.getWebAppBeanDeploymentArchive());

    ContainerContext cc = new ContainerContext(sce, manager);
    StringBuilder dump = new StringBuilder();
    Container container = findContainer(cc, dump);
    if (container == null) {
      log.info(
          "No supported servlet container detected, CDI injection will NOT be available in Servlets, Filters or Listeners");
      log.debug("Exception dump from Container lookup: {}", dump);
    } else {
      container.initialize(cc);
      this.container = container;
    }

    // Push the manager into the servlet context so we can access in JSF
    context.setAttribute(BEAN_MANAGER_ATTRIBUTE_NAME, manager);

    if (JspFactory.getDefaultFactory() != null) {
      JspApplicationContext jspApplicationContext =
          JspFactory.getDefaultFactory().getJspApplicationContext(context);

      // Register the ELResolver with JSP
      jspApplicationContext.addELResolver(manager.getELResolver());

      // Register ELContextListener with JSP
      jspApplicationContext.addELContextListener(
          Reflections.<ELContextListener>newInstance("org.jboss.weld.el.WeldELContextListener"));

      // Push the wrapped expression factory into the servlet context so that Tomcat or Jetty can
      // hook it in using a container code
      context.setAttribute(
          EXPRESSION_FACTORY_NAME,
          manager.wrapExpressionFactory(jspApplicationContext.getExpressionFactory()));
    }

    bootstrap.deployBeans().validateBeans().endInitialization();
    super.contextInitialized(sce);
  }