Skip to content

Commit

Permalink
Stash - refactor BeanArchiveProcessor to allow for implict bean archives
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Mar 16, 2013
1 parent e3b128a commit a002cc8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 23 deletions.
Expand Up @@ -27,6 +27,7 @@
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.CDI11BeanDeploymentArchive;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.util.reflection.Reflections;
Expand All @@ -47,10 +48,12 @@
* @author Stuart Douglas
*
*/
public class BeanDeploymentArchiveImpl implements BeanDeploymentArchive {
public class BeanDeploymentArchiveImpl implements CDI11BeanDeploymentArchive {

private final Set<String> beanClasses;

private final Set<String> additionalTypes;

private final Set<BeanDeploymentArchive> beanDeploymentArchives;

private final BeansXml beansXml;
Expand All @@ -67,12 +70,13 @@ public class BeanDeploymentArchiveImpl implements BeanDeploymentArchive {

private final boolean root; // indicates whether this is a root BDA

public BeanDeploymentArchiveImpl(Set<String> beanClasses, BeansXml beansXml, Module module, String id) {
this(beanClasses, beansXml, module, id, false);
public BeanDeploymentArchiveImpl(Set<String> beanClasses, Set<String> additionalTypes, BeansXml beansXml, Module module, String id) {
this(beanClasses, additionalTypes, beansXml, module, id, false);
}

public BeanDeploymentArchiveImpl(Set<String> beanClasses, BeansXml beansXml, Module module, String id, boolean root) {
public BeanDeploymentArchiveImpl(Set<String> beanClasses, Set<String> additionalTypes, BeansXml beansXml, Module module, String id, boolean root) {
this.beanClasses = new ConcurrentSkipListSet<String>(beanClasses);
this.additionalTypes = additionalTypes;
this.beanDeploymentArchives = new CopyOnWriteArraySet<BeanDeploymentArchive>();
this.beansXml = beansXml;
this.id = id;
Expand Down Expand Up @@ -200,4 +204,9 @@ public boolean isAccessible(BeanDeploymentArchive target) {
public String toString() {
return "BeanDeploymentArchiveImpl [id=" + id + "]";
}

@Override
public Collection<String> getAdditionalTypes() {
return additionalTypes;
}
}
Expand Up @@ -178,7 +178,7 @@ protected BeanDeploymentArchiveImpl createAndRegisterAdditionalBeanDeploymentArc
id = module.getIdentifier() + ADDITIONAL_CLASSES_BDA_SUFFIX;
}
BeanDeploymentArchiveImpl newBda = new BeanDeploymentArchiveImpl(Collections.singleton(beanClass.getName()),
BeansXml.EMPTY_BEANS_XML, module, id, false);
Collections.<String>emptySet(), BeansXml.EMPTY_BEANS_XML, module, id, false);
newBda.addBeanClass(beanClass);
newBda.getServices().addAll(serviceRegistry.entrySet());
// handle BDAs visible from the new BDA
Expand Down
Expand Up @@ -24,18 +24,23 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.as.ee.component.ComponentDescription;
import org.jboss.as.ee.component.EEModuleDescription;
import org.jboss.as.ee.structure.DeploymentType;
import org.jboss.as.ee.structure.DeploymentTypeMarker;
import org.jboss.as.ejb3.component.EJBComponentDescription;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.SubDeploymentMarker;
import org.jboss.as.server.deployment.annotation.AnnotationIndexUtils;
import org.jboss.as.server.deployment.module.ModuleRootMarker;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
import org.jboss.as.weld.WeldDeploymentMarker;
Expand All @@ -51,6 +56,7 @@
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.modules.Module;
import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.injection.spi.JpaInjectionServices;

Expand All @@ -67,14 +73,14 @@ public class BeanArchiveProcessor implements DeploymentUnitProcessor {
@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
final ExplicitBeanArchiveMetadataContainer cdiDeploymentMetadata = deploymentUnit
.getAttachment(ExplicitBeanArchiveMetadataContainer.ATTACHMENT_KEY);
final DeploymentReflectionIndex reflectionIndex = deploymentUnit.getAttachment(Attachments.REFLECTION_INDEX);

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

final ExplicitBeanArchiveMetadataContainer cdiDeploymentMetadata = deploymentUnit.getAttachment(ExplicitBeanArchiveMetadataContainer.ATTACHMENT_KEY);
final DeploymentReflectionIndex reflectionIndex = deploymentUnit.getAttachment(Attachments.REFLECTION_INDEX);

final String beanArchiveIdPrefix;
if (deploymentUnit.getParent() == null) {
beanArchiveIdPrefix = deploymentUnit.getName();
Expand All @@ -88,25 +94,46 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
final Map<ResourceRoot, Index> indexes = AnnotationIndexUtils.getAnnotationIndexes(deploymentUnit);
final Map<ResourceRoot, BeanDeploymentArchiveImpl> bdaMap = new HashMap<ResourceRoot, BeanDeploymentArchiveImpl>();

final Module module = phaseContext.getDeploymentUnit().getAttachment(Attachments.MODULE);
final Module module = deploymentUnit.getAttachment(Attachments.MODULE);

final WeldTypeDiscoveryConfiguration typeDiscoveryConfiguration = deploymentUnit.getAttachment(WeldAttachments.WELD_TYPE_DISCOVERY_CONFIGURATION);

BeanDeploymentArchiveImpl rootBda = null;
if (cdiDeploymentMetadata != null) {
// this can be null for ear deployments
// however we still want to create a module level bean manager
for (ExplicitBeanArchiveMetadata beanArchiveMetadata : cdiDeploymentMetadata.getBeanArchiveMetadata().values()) {
boolean isRootBda = beanArchiveMetadata.isDeploymentRoot();
BeanDeploymentArchiveImpl bda = createBeanDeploymentArchive(indexes.get(beanArchiveMetadata.getResourceRoot()),
beanArchiveMetadata, module, beanArchiveIdPrefix, isRootBda);

List<ResourceRoot> structure = deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS);
for (ResourceRoot resourceRoot : structure) {
if (ModuleRootMarker.isModuleRoot(resourceRoot) && !SubDeploymentMarker.isSubDeployment(resourceRoot)) {
if ("classes".equals(resourceRoot.getRootName())) {
continue; // this is handled below
}
BeanDeploymentArchiveImpl bda = processResourceRoot(resourceRoot, cdiDeploymentMetadata, module, beanArchiveIdPrefix, indexes);
if (bda != null) {
beanDeploymentArchives.add(bda);
bdaMap.put(resourceRoot, bda);
if (bda.isRoot()) {
rootBda = bda;
deploymentUnit.putAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
}
}
}
}

// handle jar deployments
if (!DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
ResourceRoot resourceRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
BeanDeploymentArchiveImpl bda = processResourceRoot(resourceRoot, cdiDeploymentMetadata, module, beanArchiveIdPrefix, indexes);
if (bda != null) {
beanDeploymentArchives.add(bda);
bdaMap.put(beanArchiveMetadata.getResourceRoot(), bda);
if (isRootBda) {
bdaMap.put(resourceRoot, bda);
if (bda.isRoot()) {
rootBda = bda;
deploymentUnit.putAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
}
}
}

if (rootBda == null) {
BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(Collections.<String>emptySet(),
BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(Collections.<String>emptySet(), Collections.<String>emptySet(),
BeansXml.EMPTY_BEANS_XML, module, beanArchiveIdPrefix, true);
beanDeploymentArchives.add(bda);
deploymentUnit.putAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
Expand All @@ -121,6 +148,23 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
deploymentUnit.putAttachment(WeldAttachments.BEAN_DEPLOYMENT_MODULE, bdm);
}

private BeanDeploymentArchiveImpl processResourceRoot(ResourceRoot resourceRoot, ExplicitBeanArchiveMetadataContainer explicitBeanArchives, Module module, String prefix, Map<ResourceRoot, Index> indexes) throws DeploymentUnitProcessingException {
ExplicitBeanArchiveMetadata metadata = null;
if (explicitBeanArchives != null) {
metadata = explicitBeanArchives.getBeanArchiveMetadata().get(resourceRoot);
}
if (metadata == null || metadata.getBeansXml().getBeanDiscoveryMode().equals(BeanDiscoveryMode.ANNOTATED)) {
// this is either an implicit bean archive or not a bean archive at all!
return null; // for now
} else if (metadata.getBeansXml().getBeanDiscoveryMode().equals(BeanDiscoveryMode.NONE)) {
// scanning suppressed in this archive
return null;
} else {
boolean isRootBda = metadata.isDeploymentRoot();
return createBeanDeploymentArchive(indexes.get(metadata.getResourceRoot()), metadata, module, prefix, isRootBda);
}
}

private void processEEComponents(DeploymentUnit deploymentUnit, Map<ResourceRoot, BeanDeploymentArchiveImpl> bdaMap, BeanDeploymentArchiveImpl rootBda, Map<ResourceRoot, Index> indexes, DeploymentReflectionIndex reflectionIndex) {
final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
for (ComponentDescription component : moduleDescription.getComponentDescriptions()) {
Expand Down Expand Up @@ -171,7 +215,7 @@ private BeanDeploymentArchiveImpl createBeanDeploymentArchive(final Index index,
if (beanArchiveMetadata.getResourceRoot() != null) {
beanArchiveId += beanArchiveMetadata.getResourceRoot().getRoot().getPathName();
}
return new BeanDeploymentArchiveImpl(classNames, beanArchiveMetadata.getBeansXml(), module, beanArchiveId, root);
return new BeanDeploymentArchiveImpl(classNames, Collections.<String>emptySet(), beanArchiveMetadata.getBeansXml(), module, beanArchiveId, root);
}

@Override
Expand Down
Expand Up @@ -142,7 +142,7 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
continue;
}

final BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(new HashSet<String>(discoveredClasses), beansXml, module, beanArchiveIdPrefix + entry.getKey().toExternalForm());
final BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(new HashSet<String>(discoveredClasses), Collections.<String>emptySet(), beansXml, module, beanArchiveIdPrefix + entry.getKey().toExternalForm());

final BeanDeploymentModule bdm = new BeanDeploymentModule(Collections.singleton(bda));
final JpaInjectionServices jpaInjectionServices = new WeldJpaInjectionServices(deploymentUnit, deploymentUnit.getServiceRegistry());
Expand Down
Expand Up @@ -104,14 +104,14 @@ public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitPro
if (beansXmlPresent) {
// warn that it is not portable to use both locations at the same time
WeldLogger.DEPLOYMENT_LOGGER.duplicateBeansXml();
beanArchiveMetadata.put(classesRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, beansXml, classesRoot, parseBeansXml(rootBeansXml, parser, deploymentUnit), true));
beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, beansXml, classesRoot, parseBeansXml(rootBeansXml, parser, deploymentUnit), true));
} else {
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", rootBeansXml);
beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(rootBeansXml, classesRoot, parseBeansXml(rootBeansXml, parser, deploymentUnit), true));
}
} else if (beansXmlPresent) {
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", beansXml);
beanArchiveMetadata.put(classesRoot, new ExplicitBeanArchiveMetadata(beansXml, classesRoot, parseBeansXml(beansXml, parser, deploymentUnit), true));
beanArchiveMetadata.put(deploymentRoot, new ExplicitBeanArchiveMetadata(beansXml, classesRoot, parseBeansXml(beansXml, parser, deploymentUnit), true));
}
} else if (!DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
final VirtualFile rootBeansXml = deploymentRoot.getRoot().getChild(META_INF_BEANS_XML);
Expand Down

0 comments on commit a002cc8

Please sign in to comment.