/** Insure that timer methods can be invoked for the current operation on this Context. */
  private void checkState() throws IllegalStateException, NoSuchObjectLocalException {
    final BeanContext beanContext = ThreadContext.getThreadContext().getBeanContext();
    final BaseContext context = (BaseContext) beanContext.get(EJBContext.class);
    context.check(BaseContext.Call.timerMethod);

    if (timerData.isCancelled()) {
      throw new NoSuchObjectLocalException("Timer has been cancelled");
    }

    if (timerData.isExpired()) {
      throw new NoSuchObjectLocalException("The timer has expired");
    }
  }
Beispiel #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));
  }