Esempio n. 1
0
  @Override
  public void stopApplication(final Object endObject) {
    logger.debug("OpenWebBeans Container is stopping.");

    try {
      // Fire shut down
      if (WebappBeanManager.class.isInstance(beanManager)) {
        WebappBeanManager.class.cast(beanManager).beforeStop();
      }

      webBeansContext.getContextsService().endContext(RequestScoped.class, endObject);
      webBeansContext.getContextsService().endContext(ConversationScoped.class, endObject);
      webBeansContext.getContextsService().endContext(SessionScoped.class, endObject);
      webBeansContext.getContextsService().endContext(ApplicationScoped.class, endObject);
      webBeansContext.getContextsService().endContext(Singleton.class, endObject);

      // clean up the EL caches after each request
      ELContextStore elStore = ELContextStore.getInstance(false);
      if (elStore != null) {
        elStore.destroyELContextStore();
      }

      this.beanManager.fireEvent(new BeforeShutdownImpl(), true);

      // this will now even destroy the ExtensionBeans and other internal stuff
      this.contextsService.destroy(endObject);

      // Unbind BeanManager
      if (jndiService != null) {
        jndiService.unbind(WebBeansConstants.WEB_BEANS_MANAGER_JNDI_NAME);
      }

      // Free all plugin resources
      ((CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin()).clearProxies();
      webBeansContext.getPluginLoader().shutDown();

      // Clear extensions
      webBeansContext.getExtensionLoader().clear();

      // Delete Resolutions Cache
      beanManager.getInjectionResolver().clearCaches();

      // Delete AnnotateTypeCache
      webBeansContext.getAnnotatedElementFactory().clear();

      // After Stop
      // Clear the resource injection service
      final ResourceInjectionService injectionServices =
          webBeansContext.getService(ResourceInjectionService.class);
      if (injectionServices != null) {
        injectionServices.clear();
      }

      // Comment out for commit OWB-502
      // ContextFactory.cleanUpContextFactory();

      CdiAppContextsService.class.cast(contextsService).removeThreadLocals();

      WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader());

      // Clear BeanManager
      this.beanManager.clear();

      // Clear singleton list
      WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader());

    } catch (final Exception e) {
      logger.error("An error occured while stopping the container.", e);
    }
  }
Esempio n. 2
0
  @Override
  public void startApplication(final Object startupObject) {
    if (ServletContextEvent.class.isInstance(startupObject)) {
      startServletContext(
          ServletContext.class.cast(
              getServletContext(startupObject))); // TODO: check it is relevant
      return;
    } else if (!StartupObject.class.isInstance(startupObject)) {
      logger.debug("startupObject is not of StartupObject type; ignored");
      return;
    }

    final StartupObject stuff = (StartupObject) startupObject;
    final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();

    // Initalize Application Context
    logger.info("OpenWebBeans Container is starting...");

    final long begin = System.currentTimeMillis();

    try {
      Thread.currentThread().setContextClassLoader(stuff.getClassLoader());

      final AppContext appContext = stuff.getAppContext();
      if (stuff.getWebContext()
          == null) { // do it before any other things to keep our singleton finder working
        appContext.setWebBeansContext(webBeansContext);
      }

      // Load all plugins
      webBeansContext.getPluginLoader().startUp();

      // Get Plugin
      final CdiPlugin cdiPlugin = (CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin();

      cdiPlugin.setClassLoader(stuff.getClassLoader());
      cdiPlugin.setWebBeansContext(webBeansContext);

      // Configure EJB Deployments
      cdiPlugin.configureDeployments(stuff.getBeanContexts());

      // Resournce Injection Service
      final CdiResourceInjectionService injectionService =
          (CdiResourceInjectionService) webBeansContext.getService(ResourceInjectionService.class);
      // todo use startupObject allDeployments to find Comp in priority (otherwise we can keep N
      // times comps and loose time at injection time
      injectionService.setAppContext(
          stuff.getAppContext(),
          stuff.getBeanContexts() != null
              ? stuff.getBeanContexts()
              : Collections.<BeanContext>emptyList());

      // Deploy the beans
      CdiScanner cdiScanner = null;
      try {
        // Scanning process
        logger.debug("Scanning classpaths for beans artifacts.");

        if (CdiScanner.class.isInstance(scannerService)) {
          cdiScanner = CdiScanner.class.cast(scannerService);
          cdiScanner.setContext(webBeansContext);
          cdiScanner.init(startupObject);
        } else {
          cdiScanner = new CdiScanner();
          cdiScanner.setContext(webBeansContext);
          cdiScanner.init(startupObject);
        }

        // Scan
        this.scannerService.scan();

        // just to let us write custom CDI Extension using our internals easily
        CURRENT_APP_INFO.set(stuff.getAppInfo());

        addInternalBeans(); // before next event which can register custom beans (JAX-RS)
        SystemInstance.get().fireEvent(new WebBeansContextBeforeDeploy(webBeansContext));

        // Deploy bean from XML. Also configures deployments, interceptors, decorators.
        deployer.deploy(scannerService);
        contextsService.init(
            startupObject); // fire app event and also starts SingletonContext and
                            // ApplicationContext
      } catch (final Exception e1) {
        SystemInstance.get()
            .getComponent(Assembler.class)
            .logger
            .error("CDI Beans module deployment failed", e1);
        throw new OpenEJBRuntimeException(e1);
      } finally {
        CURRENT_APP_INFO.remove();
      }

      final Collection<Class<?>> ejbs = new ArrayList<>(stuff.getBeanContexts().size());
      for (final BeanContext bc : stuff.getBeanContexts()) {
        final CdiEjbBean cdiEjbBean = bc.get(CdiEjbBean.class);
        if (cdiEjbBean == null) {
          continue;
        }

        ejbs.add(bc.getManagedClass());

        if (AbstractProducer.class.isInstance(cdiEjbBean)) {
          AbstractProducer.class
              .cast(cdiEjbBean)
              .defineInterceptorStack(
                  cdiEjbBean, cdiEjbBean.getAnnotatedType(), cdiEjbBean.getWebBeansContext());
        }
        bc.mergeOWBAndOpenEJBInfo();
        bc.set(
            InterceptorResolutionService.BeanInterceptorInfo.class,
            InjectionTargetImpl.class.cast(cdiEjbBean.getInjectionTarget()).getInterceptorInfo());
        cdiEjbBean.initInternals();
      }

      // Start actual starting on sub-classes
      if (beanManager instanceof WebappBeanManager) {
        ((WebappBeanManager) beanManager).afterStart();
      }

      for (final Class<?> clazz : cdiScanner.getStartupClasses()) {
        if (ejbs.contains(clazz)) {
          continue;
        }
        starts(beanManager, clazz);
      }
    } finally {
      Thread.currentThread().setContextClassLoader(oldCl);

      // cleanup threadlocal used to enrich cdi context manually
      OptimizedLoaderService.ADDITIONAL_EXTENSIONS.remove();
    }

    logger.info(
        "OpenWebBeans Container has started, it took {0} ms.",
        Long.toString(System.currentTimeMillis() - begin));
  }