@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
final DeploymentUnit parent = deploymentUnit.getParent() == null ? deploymentUnit : deploymentUnit.getParent();
final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
final ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
return;
}
//add a dependency on the weld service to web deployments
final ServiceName weldServiceName = parent.getServiceName().append(WeldService.SERVICE_NAME);
deploymentUnit.addToAttachmentList(Attachments.WEB_DEPENDENCIES, weldServiceName);
final Set<ServiceName> jpaServices = new HashSet<ServiceName>();
// we only start weld on top level deployments
if (deploymentUnit.getParent() != null) {
return;
}
WeldLogger.DEPLOYMENT_LOGGER.startingServicesForCDIDeployment(phaseContext.getDeploymentUnit().getName());
final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
final ModuleSpecification moduleSpecification = deploymentUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
final Set<BeanDeploymentArchiveImpl> beanDeploymentArchives = new HashSet<BeanDeploymentArchiveImpl>();
final Map<ModuleIdentifier, BeanDeploymentModule> bdmsByIdentifier = new HashMap<ModuleIdentifier, BeanDeploymentModule>();
final Map<ModuleIdentifier, ModuleSpecification> moduleSpecByIdentifier = new HashMap<ModuleIdentifier, ModuleSpecification>();
// the root module only has access to itself. For most deployments this will be the only module
// for ear deployments this represents the ear/lib directory.
// war and jar deployment visibility will depend on the dependencies that
// exist in the application, and mirror inter module dependencies
final BeanDeploymentModule rootBeanDeploymentModule = deploymentUnit.getAttachment(WeldAttachments.BEAN_DEPLOYMENT_MODULE);
final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
final EEApplicationDescription eeApplicationDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_APPLICATION_DESCRIPTION);
bdmsByIdentifier.put(module.getIdentifier(), rootBeanDeploymentModule);
moduleSpecByIdentifier.put(module.getIdentifier(), moduleSpecification);
beanDeploymentArchives.addAll(rootBeanDeploymentModule.getBeanDeploymentArchives());
final List<DeploymentUnit> subDeployments = deploymentUnit.getAttachmentList(Attachments.SUB_DEPLOYMENTS);
final Set<ClassLoader> subDeploymentLoaders = new HashSet<ClassLoader>();
getJpaDependencies(deploymentUnit, jpaServices);
for (DeploymentUnit subDeployment : subDeployments) {
getJpaDependencies(deploymentUnit, jpaServices);
final Module subDeploymentModule = subDeployment.getAttachment(Attachments.MODULE);
if (subDeploymentModule == null) {
continue;
}
subDeploymentLoaders.add(subDeploymentModule.getClassLoader());
final ModuleSpecification subDeploymentModuleSpec = subDeployment.getAttachment(Attachments.MODULE_SPECIFICATION);
final BeanDeploymentModule bdm = subDeployment.getAttachment(WeldAttachments.BEAN_DEPLOYMENT_MODULE);
if (bdm == null) {
continue;
}
// add the modules bdas to the global set of bdas
beanDeploymentArchives.addAll(bdm.getBeanDeploymentArchives());
bdmsByIdentifier.put(subDeploymentModule.getIdentifier(), bdm);
moduleSpecByIdentifier.put(subDeploymentModule.getIdentifier(), subDeploymentModuleSpec);
//we have to do this here as the aggregate components are not available in earlier phases
final ResourceRoot subDeploymentRoot = subDeployment.getAttachment(Attachments.DEPLOYMENT_ROOT);
final EjbInjectionServices ejbInjectionServices = new WeldEjbInjectionServices(deploymentUnit.getServiceRegistry(), eeModuleDescription, eeApplicationDescription, subDeploymentRoot.getRoot());
bdm.addService(EjbInjectionServices.class, ejbInjectionServices);
}
for (Map.Entry<ModuleIdentifier, BeanDeploymentModule> entry : bdmsByIdentifier.entrySet()) {
final ModuleSpecification bdmSpec = moduleSpecByIdentifier.get(entry.getKey());
final BeanDeploymentModule bdm = entry.getValue();
if (bdm == rootBeanDeploymentModule) {
continue; // the root module only has access to itself
}
for (ModuleDependency dependency : bdmSpec.getSystemDependencies()) {
BeanDeploymentModule other = bdmsByIdentifier.get(dependency.getIdentifier());
if (other != null && other != bdm) {
bdm.addBeanDeploymentModule(other);
}
}
}
final List<Metadata<Extension>> extensions = deploymentUnit.getAttachmentList(WeldAttachments.PORTABLE_EXTENSIONS);
final WeldDeployment deployment = new WeldDeployment(beanDeploymentArchives, extensions, module, subDeploymentLoaders);
final WeldContainer weldContainer = new WeldContainer(deployment, Environments.EE_INJECT);
//hook up validation service
//TODO: we need to change weld so this is a per-BDA service
final ValidatorFactory factory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
weldContainer.addWeldService(ValidationServices.class, new WeldValidationServices(factory));
final EjbInjectionServices ejbInjectionServices = new WeldEjbInjectionServices(deploymentUnit.getServiceRegistry(), eeModuleDescription, eeApplicationDescription, deploymentRoot.getRoot());
weldContainer.addWeldService(EjbInjectionServices.class, ejbInjectionServices);
rootBeanDeploymentModule.addService(EjbInjectionServices.class, ejbInjectionServices);
weldContainer.addWeldService(EjbServices.class, new WeldEjbServices(deploymentUnit.getServiceRegistry()));
final JpaInjectionServices rootJpaInjectionServices = new WeldJpaInjectionServices(deploymentUnit, deploymentUnit.getServiceRegistry());
weldContainer.addWeldService(JpaInjectionServices.class, rootJpaInjectionServices);
final WeldService weldService = new WeldService(weldContainer, deploymentUnit.getName());
// add the weld service
final ServiceBuilder<WeldContainer> weldServiceBuilder = serviceTarget.addService(weldServiceName, weldService);
weldServiceBuilder.addDependencies(TCCLSingletonService.SERVICE_NAME);
installResourceInjectionService(serviceTarget, deploymentUnit, weldService, weldServiceBuilder);
installSecurityService(serviceTarget, deploymentUnit, weldService, weldServiceBuilder);