/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/deployment/processors/EjbRefProcessor.java
Java | 206 lines | 138 code | 25 blank | 43 comment | 38 complexity | 2f5f0604ee8323a9b007681f01879cd2 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.jboss.as.ejb3.deployment.processors;
24
25import java.util.ArrayList;
26import java.util.List;
27
28import org.jboss.as.ee.component.BindingConfiguration;
29import org.jboss.as.ee.component.ComponentDescription;
30import org.jboss.as.ee.component.DeploymentDescriptorEnvironment;
31import org.jboss.as.ee.component.EEApplicationClasses;
32import org.jboss.as.ee.component.LookupInjectionSource;
33import org.jboss.as.ee.component.ResourceInjectionTarget;
34import org.jboss.as.ee.component.deployers.AbstractDeploymentDescriptorBindingsProcessor;
35import org.jboss.as.ejb3.deployment.EjbDeploymentAttachmentKeys;
36import org.jboss.as.server.deployment.DeploymentUnit;
37import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
38import org.jboss.as.server.deployment.reflect.DeploymentClassIndex;
39import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
40import org.jboss.metadata.javaee.spec.EJBLocalReferenceMetaData;
41import org.jboss.metadata.javaee.spec.EJBLocalReferencesMetaData;
42import org.jboss.metadata.javaee.spec.EJBReferenceMetaData;
43import org.jboss.metadata.javaee.spec.EJBReferencesMetaData;
44import org.jboss.metadata.javaee.spec.Environment;
45import org.jboss.metadata.javaee.spec.RemoteEnvironment;
46
47/**
48 * Deployment processor responsible for processing ejb references from deployment descriptors
49 *
50 * @author Stuart Douglas
51 */
52public class EjbRefProcessor extends AbstractDeploymentDescriptorBindingsProcessor {
53
54 private final boolean appclient;
55
56 public EjbRefProcessor(boolean appclient) {
57 this.appclient = appclient;
58 }
59
60 /**
61 * Resolves ejb-ref and ejb-local-ref elements
62 *
63 *
64 * @param deploymentUnit
65 * @param environment The environment to resolve the elements for
66 * @param componentDescription
67 *@param classLoader The deployment class loader
68 * @param deploymentReflectionIndex The reflection index
69 * @param applicationClasses @return The bindings for the environment entries
70 */
71 protected List<BindingConfiguration> processDescriptorEntries(DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ResourceInjectionTarget resourceInjectionTarget, final ComponentDescription componentDescription, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
72 final RemoteEnvironment remoteEnvironment = environment.getEnvironment();
73 final DeploymentClassIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.CLASS_INDEX);
74 List<BindingConfiguration> bindingDescriptions = new ArrayList<BindingConfiguration>();
75
76 EJBReferencesMetaData ejbRefs = remoteEnvironment.getEjbReferences();
77 if (ejbRefs != null) {
78 for (EJBReferenceMetaData ejbRef : ejbRefs) {
79 String name = ejbRef.getEjbRefName();
80 String ejbName = ejbRef.getLink();
81 String lookup = ejbRef.getLookupName();
82 String remoteInterface = ejbRef.getRemote();
83 String home = ejbRef.getHome();
84 Class<?> remoteInterfaceType = null;
85
86 //if a home is specified this is the type that is bound
87 if (!isEmpty(home)) {
88 try {
89 remoteInterfaceType = index.classIndex(home).getModuleClass();
90 } catch (ClassNotFoundException e) {
91 throw new DeploymentUnitProcessingException("Could not load home interface type " + home, e);
92 }
93 } else if (!isEmpty(remoteInterface)) {
94 try {
95 remoteInterfaceType = index.classIndex(remoteInterface).getModuleClass();
96 } catch (ClassNotFoundException e) {
97 throw new DeploymentUnitProcessingException("Could not load remote interface type " + remoteInterface, e);
98 }
99 }
100
101 if (!name.startsWith("java:")) {
102 name = environment.getDefaultContext() + name;
103 }
104
105 // our injection (source) comes from the local (ENC) lookup, no matter what.
106 LookupInjectionSource injectionSource = new LookupInjectionSource(name);
107
108 //add any injection targets
109 remoteInterfaceType = processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, ejbRef, remoteInterfaceType);
110
111 final BindingConfiguration bindingConfiguration;
112 EjbInjectionSource ejbInjectionSource = null;
113
114 if (!isEmpty(lookup)) {
115 if (!lookup.startsWith("java:")) {
116 bindingConfiguration = new BindingConfiguration(name, new EjbLookupInjectionSource(lookup, remoteInterfaceType));
117 } else {
118 bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
119 }
120 } else {
121
122 if (remoteInterfaceType == null) {
123 throw new DeploymentUnitProcessingException("Could not determine type of ejb-ref " + name + " for " + resourceInjectionTarget);
124 }
125 if (!isEmpty(ejbName)) {
126 bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(ejbName, remoteInterfaceType.getName(), name, deploymentUnit, appclient));
127 } else {
128 bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(remoteInterfaceType.getName(), name, deploymentUnit, appclient));
129 }
130 }
131
132 if (ejbInjectionSource != null) {
133 deploymentUnit.addToAttachmentList(EjbDeploymentAttachmentKeys.EJB_INJECTIONS, ejbInjectionSource);
134 }
135 bindingDescriptions.add(bindingConfiguration);
136 }
137 }
138
139 if (remoteEnvironment instanceof Environment && !appclient) {
140 EJBLocalReferencesMetaData ejbLocalRefs = ((Environment) remoteEnvironment).getEjbLocalReferences();
141 if (ejbLocalRefs != null) {
142 for (EJBLocalReferenceMetaData ejbRef : ejbLocalRefs) {
143 String name = ejbRef.getEjbRefName();
144 String ejbName = ejbRef.getLink();
145 String lookup = ejbRef.getLookupName();
146 String localInterface = ejbRef.getLocal();
147 String localHome = ejbRef.getLocalHome();
148 Class<?> localInterfaceType = null;
149
150 //if a home is specified this is the type that is bound
151 if (!isEmpty(localHome)) {
152 try {
153 localInterfaceType = index.classIndex(localHome).getModuleClass();
154 } catch (ClassNotFoundException e) {
155 throw new DeploymentUnitProcessingException("Could not load local home interface type " + localHome, e);
156 }
157 } else if (!isEmpty(localInterface)) {
158 try {
159 localInterfaceType = index.classIndex(localInterface).getModuleClass();
160 } catch (ClassNotFoundException e) {
161 throw new DeploymentUnitProcessingException("Could not load local interface type " + localInterface, e);
162 }
163 }
164
165 if (!name.startsWith("java:")) {
166 name = environment.getDefaultContext() + name;
167 }
168
169 // our injection (source) comes from the local (ENC) lookup, no matter what.
170 LookupInjectionSource injectionSource = new LookupInjectionSource(name);
171
172 //add any injection targets
173 localInterfaceType = processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, ejbRef, localInterfaceType);
174
175 if (localInterfaceType == null) {
176 throw new DeploymentUnitProcessingException("Could not determine type of ejb-local-ref " + name + " for " + resourceInjectionTarget);
177 }
178 final BindingConfiguration bindingConfiguration;
179 EjbInjectionSource ejbInjectionSource = null;
180
181 if (!isEmpty(lookup)) {
182 if (!lookup.startsWith("java:")) {
183 bindingConfiguration = new BindingConfiguration(name, new EjbLookupInjectionSource(lookup, localInterfaceType));
184 } else {
185 bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
186 }
187 } else if (!isEmpty(ejbName)) {
188 bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(ejbName, localInterfaceType.getName(), name, deploymentUnit, appclient));
189 } else {
190 bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(localInterfaceType.getName(), name, deploymentUnit, appclient));
191 }
192 if (ejbInjectionSource != null) {
193 deploymentUnit.addToAttachmentList(EjbDeploymentAttachmentKeys.EJB_INJECTIONS, ejbInjectionSource);
194 }
195 bindingDescriptions.add(bindingConfiguration);
196 }
197 }
198 }
199 return bindingDescriptions;
200 }
201
202 private boolean isEmpty(String string) {
203 return string == null || string.isEmpty();
204 }
205
206}