-
Notifications
You must be signed in to change notification settings - Fork 8
/
BeanArchiveProcessor.java
182 lines (163 loc) · 9.05 KB
/
BeanArchiveProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.weld.deployment.processors;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
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.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.annotation.AnnotationIndexUtils;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
import org.jboss.as.weld.WeldDeploymentMarker;
import org.jboss.as.weld.WeldLogger;
import org.jboss.as.weld.deployment.BeanArchiveMetadata;
import org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl;
import org.jboss.as.weld.deployment.BeanDeploymentModule;
import org.jboss.as.weld.deployment.WeldAttachments;
import org.jboss.as.weld.deployment.WeldDeploymentMetadata;
import org.jboss.as.weld.ejb.EjbDescriptorImpl;
import org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.modules.Module;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.injection.spi.JpaInjectionServices;
/**
* Deployment processor that builds bean archives and attaches them to the deployment
* <p/>
* Currently this is done by pulling the information out of the jandex {@link Index}.
* <p/>
*
* @author Stuart Douglas
*/
public class BeanArchiveProcessor implements DeploymentUnitProcessor {
@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
final WeldDeploymentMetadata cdiDeploymentMetadata = deploymentUnit
.getAttachment(WeldDeploymentMetadata.ATTACHMENT_KEY);
final DeploymentReflectionIndex reflectionIndex = deploymentUnit.getAttachment(Attachments.REFLECTION_INDEX);
if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
return;
}
final String beanArchiveIdPrefix;
if (deploymentUnit.getParent() == null) {
beanArchiveIdPrefix = deploymentUnit.getName();
} else {
beanArchiveIdPrefix = deploymentUnit.getParent().getName() + "." + deploymentUnit.getName();
}
final Set<BeanDeploymentArchiveImpl> beanDeploymentArchives = new HashSet<BeanDeploymentArchiveImpl>();
WeldLogger.DEPLOYMENT_LOGGER.processingWeldDeployment(phaseContext.getDeploymentUnit().getName());
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);
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 (BeanArchiveMetadata beanArchiveMetadata : cdiDeploymentMetadata.getBeanArchiveMetadata()) {
boolean isRootBda = beanArchiveMetadata.isDeploymentRoot();
BeanDeploymentArchiveImpl bda = createBeanDeploymentArchive(indexes.get(beanArchiveMetadata.getResourceRoot()),
beanArchiveMetadata, module, beanArchiveIdPrefix, isRootBda);
beanDeploymentArchives.add(bda);
bdaMap.put(beanArchiveMetadata.getResourceRoot(), bda);
if (isRootBda) {
rootBda = bda;
deploymentUnit.putAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
}
}
}
if (rootBda == null) {
BeanDeploymentArchiveImpl bda = new BeanDeploymentArchiveImpl(Collections.<String>emptySet(),
BeansXml.EMPTY_BEANS_XML, module, beanArchiveIdPrefix, true);
beanDeploymentArchives.add(bda);
deploymentUnit.putAttachment(WeldAttachments.DEPLOYMENT_ROOT_BEAN_DEPLOYMENT_ARCHIVE, bda);
rootBda = bda;
}
processEEComponents(deploymentUnit, bdaMap, rootBda, indexes, reflectionIndex);
final JpaInjectionServices jpaInjectionServices = new WeldJpaInjectionServices(deploymentUnit, deploymentUnit.getServiceRegistry());
final BeanDeploymentModule bdm = new BeanDeploymentModule(beanDeploymentArchives);
bdm.addService(JpaInjectionServices.class, jpaInjectionServices);
deploymentUnit.putAttachment(WeldAttachments.BEAN_DEPLOYMENT_MODULE, bdm);
}
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()) {
BeanDeploymentArchiveImpl bda = resolveComponentBda(component.getComponentClassName(), bdaMap, rootBda, indexes);
component.setBeanDeploymentArchiveId(bda.getId());
if (component instanceof EJBComponentDescription) {
final EJBComponentDescription componentDescription = (EJBComponentDescription) component;
//first we need to resolve the correct BDA for the bean
bda.addEjbDescriptor(new EjbDescriptorImpl<Object>(componentDescription, bda, reflectionIndex));
}
}
}
/**
* Resolves the bean deployment archive for a session bean
*
* @param ejbClassName the session bean's class
* @param bdaMap The BDA's keyed by resource root
* @param rootBda The root bda, this is used as the BDA of last resort if the correct BDA cannot be found
* @param indexes The jandex indexes
* @return The correct BDA for the EJB
*/
private BeanDeploymentArchiveImpl resolveComponentBda(String ejbClassName, Map<ResourceRoot, BeanDeploymentArchiveImpl> bdaMap, BeanDeploymentArchiveImpl rootBda, Map<ResourceRoot, Index> indexes) {
final DotName className = DotName.createSimple(ejbClassName);
for (Map.Entry<ResourceRoot, BeanDeploymentArchiveImpl> entry : bdaMap.entrySet()) {
final Index index = indexes.get(entry.getKey());
if (index != null) {
if (index.getClassByName(className) != null) {
return entry.getValue();
}
}
}
return rootBda;
}
private BeanDeploymentArchiveImpl createBeanDeploymentArchive(final Index index, BeanArchiveMetadata beanArchiveMetadata,
Module module, String beanArchivePrefix, boolean root) throws DeploymentUnitProcessingException {
Set<String> classNames = new HashSet<String>();
// index may be null if a war has a beans.xml but no WEB-INF/classes
if (index != null) {
for (ClassInfo classInfo : index.getKnownClasses()) {
classNames.add(classInfo.name().toString());
}
}
String beanArchiveId = beanArchivePrefix;
if (beanArchiveMetadata.getResourceRoot() != null) {
beanArchiveId += beanArchiveMetadata.getResourceRoot().getRoot().getPathName();
}
return new BeanDeploymentArchiveImpl(classNames, beanArchiveMetadata.getBeansXml(), module, beanArchiveId, root);
}
@Override
public void undeploy(DeploymentUnit context) {
}
}