/
ExternalBeanArchiveProcessor.java
165 lines (144 loc) · 7.48 KB
/
ExternalBeanArchiveProcessor.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
/*
* 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.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
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.weld.WeldDeploymentMarker;
import org.jboss.as.weld.WeldLogger;
import org.jboss.as.weld.deployment.AS7BeansXmlParser;
import org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadata;
import org.jboss.as.weld.deployment.BeanDeploymentArchiveImpl;
import org.jboss.as.weld.deployment.BeanDeploymentModule;
import org.jboss.as.weld.deployment.UrlScanner;
import org.jboss.as.weld.deployment.WeldAttachments;
import org.jboss.as.weld.deployment.ExplicitBeanArchiveMetadataContainer;
import org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices;
import org.jboss.modules.Module;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.injection.spi.JpaInjectionServices;
import org.jboss.weld.xml.BeansXmlParser;
/**
* Deployment processor that builds bean archives from external deployments.
* <p/>
* This is only run at the top level, as multiple sub deployments can reference the same
* beans.xml information, so we have to iterate through all bean deployment archives in this processor, to prevent
* beans.xml from being potentially parsed twice.
* <p/>
*
* @author Stuart Douglas
*/
public class ExternalBeanArchiveProcessor implements DeploymentUnitProcessor {
private static final String META_INF_BEANS_XML = "META-INF/beans.xml";
@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
return;
}
if (deploymentUnit.getParent() != null) {
return;
}
final String beanArchiveIdPrefix = deploymentUnit.getName() + ".external.";
final List<DeploymentUnit> deploymentUnits = new ArrayList<DeploymentUnit>();
deploymentUnits.add(deploymentUnit);
deploymentUnits.addAll(deploymentUnit.getAttachmentList(Attachments.SUB_DEPLOYMENTS));
AS7BeansXmlParser parser = new AS7BeansXmlParser();
final Map<URL, List<DeploymentUnit>> deploymentUnitMap = new HashMap<URL, List<DeploymentUnit>>();
final HashSet<URL> existing = new HashSet<URL>();
for (DeploymentUnit deployment : deploymentUnits) {
try {
final ExplicitBeanArchiveMetadataContainer weldDeploymentMetadata = deployment.getAttachment(ExplicitBeanArchiveMetadataContainer.ATTACHMENT_KEY);
if (weldDeploymentMetadata != null) {
for (ExplicitBeanArchiveMetadata md : weldDeploymentMetadata.getBeanArchiveMetadata().values()) {
existing.add(md.getBeansXmlFile().toURL());
if (md.getAdditionalBeansXmlFile() != null) {
existing.add(md.getAdditionalBeansXmlFile().toURL());
}
}
}
} catch (MalformedURLException e) {
throw new DeploymentUnitProcessingException(e);
}
}
for (DeploymentUnit deployment : deploymentUnits) {
final Module module = deployment.getAttachment(Attachments.MODULE);
if (module == null) {
return;
}
try {
Enumeration<URL> resources = module.getClassLoader().getResources(META_INF_BEANS_XML);
while (resources.hasMoreElements()) {
final URL beansXml = resources.nextElement();
if (existing.contains(beansXml)) {
continue;
}
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found external beans.xml: %s", beansXml.toString());
List<DeploymentUnit> dus = deploymentUnitMap.get(beansXml);
if (dus == null) {
deploymentUnitMap.put(beansXml, dus = new ArrayList<DeploymentUnit>());
}
dus.add(deployment);
}
} catch (IOException e) {
throw new DeploymentUnitProcessingException(e);
}
}
for (final Map.Entry<URL, List<DeploymentUnit>> entry : deploymentUnitMap.entrySet()) {
//we just take the first module, it should not make any difference. The idea that
//the same beans.xml is accessible via two different CL's is not something we can deal with
final Module module = entry.getValue().get(0).getAttachment(Attachments.MODULE);
final BeansXml beansXml = parseBeansXml(entry.getKey(), parser, deploymentUnit);
final UrlScanner urlScanner = new UrlScanner();
final List<String> discoveredClasses = new ArrayList<String>();
if(!urlScanner.handleBeansXml(entry.getKey(), discoveredClasses)) {
continue;
}
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());
bdm.addService(JpaInjectionServices.class, jpaInjectionServices);
deploymentUnit.addToAttachmentList(WeldAttachments.ADDITIONAL_BEAN_DEPLOYMENT_MODULES, bdm);
for (DeploymentUnit du : entry.getValue()) {
du.addToAttachmentList(WeldAttachments.VISIBLE_ADDITIONAL_BEAN_DEPLOYMENT_MODULE, bdm);
}
}
}
@Override
public void undeploy(DeploymentUnit context) {
}
private BeansXml parseBeansXml(URL beansXmlFile, BeansXmlParser parser, final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
return parser.parse(beansXmlFile);
}
}