@Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {

    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final DeploymentUnit topLevelDeployment =
        deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();
    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
    if (!WeldDeploymentMarker.isPartOfWeldDeployment(topLevelDeployment)) {
      return;
    }

    final Collection<ServiceName> dependencies =
        deploymentUnit.getAttachment(Attachments.JNDI_DEPENDENCIES);

    BeanDeploymentArchiveImpl rootBda =
        deploymentUnit.getAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE);
    if (rootBda == null) {
      // this archive is not actually a bean archive.
      // then use the top level root bda
      rootBda =
          topLevelDeployment.getAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE);
    }
    if (rootBda == null) {
      WeldLogger.ROOT_LOGGER.couldNotFindBeanManagerForDeployment(deploymentUnit.getName());
      return;
    }

    final ServiceName weldServiceName =
        topLevelDeployment.getServiceName().append(WeldBootstrapService.SERVICE_NAME);

    // add the BeanManager service
    final ServiceName beanManagerServiceName = BeanManagerService.serviceName(deploymentUnit);
    BeanManagerService beanManagerService = new BeanManagerService(rootBda.getId());
    serviceTarget
        .addService(beanManagerServiceName, beanManagerService)
        .addDependency(
            weldServiceName, WeldBootstrapService.class, beanManagerService.getWeldContainer())
        .install();
    ;

    final EEModuleDescription moduleDescription =
        deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);

    if (moduleDescription == null) {
      return;
    }

    // hack to set up a java:comp binding for jar deployments as well as wars
    if (DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)
        || deploymentUnit.getName().endsWith(".jar")) {
      // bind the bean manager to JNDI
      final ServiceName moduleContextServiceName =
          ContextNames.contextServiceNameOfModule(
              moduleDescription.getApplicationName(), moduleDescription.getModuleName());
      bindBeanManager(
          serviceTarget,
          beanManagerServiceName,
          moduleContextServiceName,
          dependencies,
          phaseContext.getServiceRegistry());
    }

    // bind the bm into java:comp for all components that require it
    for (ComponentDescription component : moduleDescription.getComponentDescriptions()) {
      if (component.getNamingMode() == ComponentNamingMode.CREATE) {
        final ServiceName compContextServiceName =
            ContextNames.contextServiceNameOfComponent(
                moduleDescription.getApplicationName(),
                moduleDescription.getModuleName(),
                component.getComponentName());
        bindBeanManager(
            serviceTarget,
            beanManagerServiceName,
            compContextServiceName,
            dependencies,
            phaseContext.getServiceRegistry());
      }
    }
    deploymentUnit.addToAttachmentList(Attachments.SETUP_ACTIONS, new WeldContextSetup());
  }
Пример #2
0
  @Override
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final DeploymentUnit topLevelDeployment =
        deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();
    final WeldDeploymentMetadata cdiDeploymentMetadata =
        deploymentUnit.getAttachment(WeldDeploymentMetadata.ATTACHMENT_KEY);

    if (!WeldDeploymentMarker.isWeldDeployment(deploymentUnit)) {
      return;
    }

    // create a CDI injection factory
    EEModuleDescription eeModuleDescription =
        deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    final Module topLevelModule = topLevelDeployment.getAttachment(Attachments.MODULE);
    if (eeModuleDescription != null) {
      eeModuleDescription.addInjectionFactory(
          new WeldInjectionFactory(
              phaseContext.getServiceTarget(), deploymentUnit, topLevelModule.getClassLoader()));
    }
    final String beanArchiveIdPrefix;
    if (deploymentUnit.getParent() == null) {
      beanArchiveIdPrefix = deploymentUnit.getName();
    } else {
      beanArchiveIdPrefix = deploymentUnit.getParent().getName() + "." + deploymentUnit.getName();
    }

    final Set<BeanDeploymentArchiveImpl> beanDeploymentArchives =
        new HashSet<BeanDeploymentArchiveImpl>();
    log.info("Processing CDI deployment: " + phaseContext.getDeploymentUnit().getName());

    final Map<ResourceRoot, Index> indexes =
        AnnotationIndexUtils.getAnnotationIndexes(deploymentUnit);

    final Module module = phaseContext.getDeploymentUnit().getAttachment(Attachments.MODULE);
    boolean rootArchiveFound = false;
    if (cdiDeploymentMetadata != null) {
      // this can be null for ear deployments
      // however we still want to create a module level bean manager
      for (BeanArchiveMetadata beanArchiveMetadata :
          cdiDeploymentMetadata.getBeanArchiveMetadata()) {
        BeanDeploymentArchiveImpl bda =
            createBeanDeploymentArchive(
                indexes.get(beanArchiveMetadata.getResourceRoot()),
                beanArchiveMetadata,
                module,
                beanArchiveIdPrefix);
        beanDeploymentArchives.add(bda);
        if (beanArchiveMetadata.isDeploymentRoot()) {
          rootArchiveFound = true;
          deploymentUnit.putAttachment(
              WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
        }
      }
    }
    if (!rootArchiveFound) {
      BeanDeploymentArchiveImpl bda =
          new BeanDeploymentArchiveImpl(
              Collections.<String>emptySet(),
              BeansXml.EMPTY_BEANS_XML,
              module,
              beanArchiveIdPrefix);
      beanDeploymentArchives.add(bda);
      deploymentUnit.putAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
    }

    deploymentUnit.putAttachment(
        WeldAttachments.BEAN_DEPLOYMENT_MODULE, new BeanDeploymentModule(beanDeploymentArchives));
  }