protected <T> void processBeanRegistration(
      BeanRegistration registration, GlobalEnablementBuilder globalEnablementBuilder) {
    Bean<?> bean = registration.getBean();
    BeanManagerImpl beanManager = registration.getBeanManager();
    if (beanManager == null) {
      // Get the bean manager for beans added via ABD#addBean()
      beanManager = getOrCreateBeanDeployment(bean.getBeanClass()).getBeanManager();
    } else {
      // Also validate the bean produced by a builder
      ExternalBeanAttributesFactory.validateBeanAttributes(bean, getBeanManager());
      validateBean(bean);
    }

    // Custom beans (alternatives, interceptors, decorators) may also implement
    // javax.enterprise.inject.spi.Prioritized
    Integer priority = (bean instanceof Prioritized) ? ((Prioritized) bean).getPriority() : null;

    if (bean instanceof Interceptor<?>) {
      beanManager.addInterceptor((Interceptor<?>) bean);
      if (priority != null) {
        globalEnablementBuilder.addInterceptor(bean.getBeanClass(), priority);
      }
    } else if (bean instanceof Decorator<?>) {
      beanManager.addDecorator(CustomDecoratorWrapper.of((Decorator<?>) bean, beanManager));
      if (priority != null) {
        globalEnablementBuilder.addDecorator(bean.getBeanClass(), priority);
      }
    } else {
      beanManager.addBean(bean);
      if (priority != null && bean.isAlternative()) {
        globalEnablementBuilder.addAlternative(bean.getBeanClass(), priority);
      }
    }
    containerLifecycleEvents.fireProcessBean(beanManager, bean, registration.getExtension());
  }
 @Test(expected = InjectionException.class)
 @Category(Broken.class)
 public void testSameBeanTypeInChildAsParentInjection() {
   BeanManagerImpl childActivity = beanManager.createActivity();
   Bean<?> anotherMyBean = createDummyBean(childActivity);
   childActivity.addBean(anotherMyBean);
 }
示例#3
0
  public void startInitialization() {
    if (deploymentManager == null) {
      throw BootstrapLogger.LOG.managerNotInitialized();
    }

    Set<BeanDeployment> physicalBeanDeploymentArchives =
        new HashSet<BeanDeployment>(getBeanDeployments());

    ExtensionBeanDeployer extensionBeanDeployer =
        new ExtensionBeanDeployer(deploymentManager, deployment, bdaMapping, contexts);
    extensionBeanDeployer.addExtensions(extensions);
    extensionBeanDeployer.deployBeans();

    installFastProcessAnnotatedTypeResolver(deploymentManager.getServices());

    // Add the Deployment BeanManager Bean to the Deployment BeanManager
    deploymentManager.addBean(new BeanManagerBean(deploymentManager));
    deploymentManager.addBean(new BeanManagerImplBean(deploymentManager));

    // Re-Read the deployment structure, bdaMapping will be the physical
    // structure, and will add in BDAs for any extensions outside a
    // physical BDA
    deploymentVisitor.visit();

    BeforeBeanDiscoveryImpl.fire(deploymentManager, deployment, bdaMapping, contexts);

    // for each physical BDA transform its classes into AnnotatedType instances
    for (BeanDeployment beanDeployment : physicalBeanDeploymentArchives) {
      beanDeployment.createClasses();
    }

    // Re-Read the deployment structure, bdaMapping will be the physical
    // structure, extensions and any classes added using addAnnotatedType
    // outside the physical BDA
    deploymentVisitor.visit();

    for (BeanDeployment beanDeployment : getBeanDeployments()) {
      beanDeployment.createTypes();
    }

    AfterTypeDiscoveryImpl.fire(deploymentManager, deployment, bdaMapping, contexts);

    for (BeanDeployment beanDeployment : getBeanDeployments()) {
      beanDeployment.createEnablement();
    }
  }
示例#4
0
  protected Collection<ContextHolder<? extends Context>> createContexts(ServiceRegistry services) {
    List<ContextHolder<? extends Context>> contexts =
        new ArrayList<ContextHolder<? extends Context>>();

    BeanIdentifierIndex beanIdentifierIndex = services.get(BeanIdentifierIndex.class);

    /*
     * Register a full set of bound and unbound contexts. Although we may not use all of
     * these (e.g. if we are running in a servlet environment) they may be
     * useful for an application.
     */
    contexts.add(
        new ContextHolder<ApplicationContext>(
            new ApplicationContextImpl(contextId),
            ApplicationContext.class,
            UnboundLiteral.INSTANCE));
    contexts.add(
        new ContextHolder<SingletonContext>(
            new SingletonContextImpl(contextId), SingletonContext.class, UnboundLiteral.INSTANCE));
    contexts.add(
        new ContextHolder<BoundSessionContext>(
            new BoundSessionContextImpl(contextId, beanIdentifierIndex),
            BoundSessionContext.class,
            BoundLiteral.INSTANCE));
    contexts.add(
        new ContextHolder<BoundConversationContext>(
            new BoundConversationContextImpl(contextId, services),
            BoundConversationContext.class,
            BoundLiteral.INSTANCE));
    contexts.add(
        new ContextHolder<BoundRequestContext>(
            new BoundRequestContextImpl(contextId),
            BoundRequestContext.class,
            BoundLiteral.INSTANCE));
    contexts.add(
        new ContextHolder<RequestContext>(
            new RequestContextImpl(contextId), RequestContext.class, UnboundLiteral.INSTANCE));
    contexts.add(
        new ContextHolder<DependentContext>(
            new DependentContextImpl(services.get(ContextualStore.class)),
            DependentContext.class,
            UnboundLiteral.INSTANCE));

    services.get(WeldModules.class).postContextRegistration(contextId, services, contexts);

    /*
     * Register the contexts with the bean manager and add the beans to the
     * deployment manager so that they are easily accessible (contexts are app
     * scoped)
     */
    for (ContextHolder<? extends Context> context : contexts) {
      deploymentManager.addContext(context.getContext());
      deploymentManager.addBean(ContextBean.of(context, deploymentManager));
    }

    return contexts;
  }
  public BeanDeployment(
      BeanDeploymentArchive beanDeploymentArchive,
      BeanManagerImpl deploymentManager,
      ServiceRegistry deploymentServices,
      Collection<ContextHolder<? extends Context>> contexts) {
    this.beanDeploymentArchive = beanDeploymentArchive;
    EjbDescriptors ejbDescriptors = new EjbDescriptors();
    beanDeploymentArchive.getServices().add(EjbDescriptors.class, ejbDescriptors);
    if (beanDeploymentArchive.getServices().get(ResourceLoader.class) == null) {
      beanDeploymentArchive.getServices().add(ResourceLoader.class, DefaultResourceLoader.INSTANCE);
    }
    ServiceRegistry services = new SimpleServiceRegistry();
    services.addAll(deploymentServices.entrySet());
    services.addAll(beanDeploymentArchive.getServices().entrySet());

    services.add(
        EJBApiAbstraction.class,
        new EJBApiAbstraction(beanDeploymentArchive.getServices().get(ResourceLoader.class)));
    services.add(
        JsfApiAbstraction.class,
        new JsfApiAbstraction(beanDeploymentArchive.getServices().get(ResourceLoader.class)));
    services.add(
        PersistenceApiAbstraction.class,
        new PersistenceApiAbstraction(
            beanDeploymentArchive.getServices().get(ResourceLoader.class)));
    services.add(
        WSApiAbstraction.class,
        new WSApiAbstraction(beanDeploymentArchive.getServices().get(ResourceLoader.class)));
    this.beanManager =
        BeanManagerImpl.newManager(
            deploymentManager,
            beanDeploymentArchive.getId(),
            services,
            Enabled.of(
                beanDeploymentArchive.getBeansXml(),
                beanDeploymentArchive.getServices().get(ResourceLoader.class)));
    log.debug(
        ENABLED_ALTERNATIVES,
        this.beanManager,
        beanManager.getEnabled().getAlternativeClasses(),
        beanManager.getEnabled().getAlternativeStereotypes());
    log.debug(ENABLED_DECORATORS, this.beanManager, beanManager.getEnabled().getDecorators());
    log.debug(ENABLED_INTERCEPTORS, this.beanManager, beanManager.getEnabled().getInterceptors());
    if (beanManager.getServices().contains(EjbServices.class)) {
      // Must populate EJB cache first, as we need it to detect whether a
      // bean is an EJB!
      ejbDescriptors.addAll(beanDeploymentArchive.getEjbs());
    }
    beanDeployer = new BeanDeployer(beanManager, ejbDescriptors);

    // Must at the Manager bean straight away, as it can be injected during startup!
    beanManager.addBean(new BeanManagerBean(beanManager));
    this.contexts = contexts;
  }