PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/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. package org.jboss.as.ejb3.deployment.processors;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.jboss.as.ee.component.BindingConfiguration;
  26. import org.jboss.as.ee.component.ComponentDescription;
  27. import org.jboss.as.ee.component.DeploymentDescriptorEnvironment;
  28. import org.jboss.as.ee.component.EEApplicationClasses;
  29. import org.jboss.as.ee.component.LookupInjectionSource;
  30. import org.jboss.as.ee.component.ResourceInjectionTarget;
  31. import org.jboss.as.ee.component.deployers.AbstractDeploymentDescriptorBindingsProcessor;
  32. import org.jboss.as.ejb3.deployment.EjbDeploymentAttachmentKeys;
  33. import org.jboss.as.server.deployment.DeploymentUnit;
  34. import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
  35. import org.jboss.as.server.deployment.reflect.DeploymentClassIndex;
  36. import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
  37. import org.jboss.metadata.javaee.spec.EJBLocalReferenceMetaData;
  38. import org.jboss.metadata.javaee.spec.EJBLocalReferencesMetaData;
  39. import org.jboss.metadata.javaee.spec.EJBReferenceMetaData;
  40. import org.jboss.metadata.javaee.spec.EJBReferencesMetaData;
  41. import org.jboss.metadata.javaee.spec.Environment;
  42. import org.jboss.metadata.javaee.spec.RemoteEnvironment;
  43. /**
  44. * Deployment processor responsible for processing ejb references from deployment descriptors
  45. *
  46. * @author Stuart Douglas
  47. */
  48. public class EjbRefProcessor extends AbstractDeploymentDescriptorBindingsProcessor {
  49. private final boolean appclient;
  50. public EjbRefProcessor(boolean appclient) {
  51. this.appclient = appclient;
  52. }
  53. /**
  54. * Resolves ejb-ref and ejb-local-ref elements
  55. *
  56. *
  57. * @param deploymentUnit
  58. * @param environment The environment to resolve the elements for
  59. * @param componentDescription
  60. *@param classLoader The deployment class loader
  61. * @param deploymentReflectionIndex The reflection index
  62. * @param applicationClasses @return The bindings for the environment entries
  63. */
  64. protected List<BindingConfiguration> processDescriptorEntries(DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ResourceInjectionTarget resourceInjectionTarget, final ComponentDescription componentDescription, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
  65. final RemoteEnvironment remoteEnvironment = environment.getEnvironment();
  66. final DeploymentClassIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.CLASS_INDEX);
  67. List<BindingConfiguration> bindingDescriptions = new ArrayList<BindingConfiguration>();
  68. EJBReferencesMetaData ejbRefs = remoteEnvironment.getEjbReferences();
  69. if (ejbRefs != null) {
  70. for (EJBReferenceMetaData ejbRef : ejbRefs) {
  71. String name = ejbRef.getEjbRefName();
  72. String ejbName = ejbRef.getLink();
  73. String lookup = ejbRef.getLookupName();
  74. String remoteInterface = ejbRef.getRemote();
  75. String home = ejbRef.getHome();
  76. Class<?> remoteInterfaceType = null;
  77. //if a home is specified this is the type that is bound
  78. if (!isEmpty(home)) {
  79. try {
  80. remoteInterfaceType = index.classIndex(home).getModuleClass();
  81. } catch (ClassNotFoundException e) {
  82. throw new DeploymentUnitProcessingException("Could not load home interface type " + home, e);
  83. }
  84. } else if (!isEmpty(remoteInterface)) {
  85. try {
  86. remoteInterfaceType = index.classIndex(remoteInterface).getModuleClass();
  87. } catch (ClassNotFoundException e) {
  88. throw new DeploymentUnitProcessingException("Could not load remote interface type " + remoteInterface, e);
  89. }
  90. }
  91. if (!name.startsWith("java:")) {
  92. name = environment.getDefaultContext() + name;
  93. }
  94. // our injection (source) comes from the local (ENC) lookup, no matter what.
  95. LookupInjectionSource injectionSource = new LookupInjectionSource(name);
  96. //add any injection targets
  97. remoteInterfaceType = processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, ejbRef, remoteInterfaceType);
  98. final BindingConfiguration bindingConfiguration;
  99. EjbInjectionSource ejbInjectionSource = null;
  100. if (!isEmpty(lookup)) {
  101. if (!lookup.startsWith("java:")) {
  102. bindingConfiguration = new BindingConfiguration(name, new EjbLookupInjectionSource(lookup, remoteInterfaceType));
  103. } else {
  104. bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
  105. }
  106. } else {
  107. if (remoteInterfaceType == null) {
  108. throw new DeploymentUnitProcessingException("Could not determine type of ejb-ref " + name + " for " + resourceInjectionTarget);
  109. }
  110. if (!isEmpty(ejbName)) {
  111. bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(ejbName, remoteInterfaceType.getName(), name, deploymentUnit, appclient));
  112. } else {
  113. bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(remoteInterfaceType.getName(), name, deploymentUnit, appclient));
  114. }
  115. }
  116. if (ejbInjectionSource != null) {
  117. deploymentUnit.addToAttachmentList(EjbDeploymentAttachmentKeys.EJB_INJECTIONS, ejbInjectionSource);
  118. }
  119. bindingDescriptions.add(bindingConfiguration);
  120. }
  121. }
  122. if (remoteEnvironment instanceof Environment && !appclient) {
  123. EJBLocalReferencesMetaData ejbLocalRefs = ((Environment) remoteEnvironment).getEjbLocalReferences();
  124. if (ejbLocalRefs != null) {
  125. for (EJBLocalReferenceMetaData ejbRef : ejbLocalRefs) {
  126. String name = ejbRef.getEjbRefName();
  127. String ejbName = ejbRef.getLink();
  128. String lookup = ejbRef.getLookupName();
  129. String localInterface = ejbRef.getLocal();
  130. String localHome = ejbRef.getLocalHome();
  131. Class<?> localInterfaceType = null;
  132. //if a home is specified this is the type that is bound
  133. if (!isEmpty(localHome)) {
  134. try {
  135. localInterfaceType = index.classIndex(localHome).getModuleClass();
  136. } catch (ClassNotFoundException e) {
  137. throw new DeploymentUnitProcessingException("Could not load local home interface type " + localHome, e);
  138. }
  139. } else if (!isEmpty(localInterface)) {
  140. try {
  141. localInterfaceType = index.classIndex(localInterface).getModuleClass();
  142. } catch (ClassNotFoundException e) {
  143. throw new DeploymentUnitProcessingException("Could not load local interface type " + localInterface, e);
  144. }
  145. }
  146. if (!name.startsWith("java:")) {
  147. name = environment.getDefaultContext() + name;
  148. }
  149. // our injection (source) comes from the local (ENC) lookup, no matter what.
  150. LookupInjectionSource injectionSource = new LookupInjectionSource(name);
  151. //add any injection targets
  152. localInterfaceType = processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, ejbRef, localInterfaceType);
  153. if (localInterfaceType == null) {
  154. throw new DeploymentUnitProcessingException("Could not determine type of ejb-local-ref " + name + " for " + resourceInjectionTarget);
  155. }
  156. final BindingConfiguration bindingConfiguration;
  157. EjbInjectionSource ejbInjectionSource = null;
  158. if (!isEmpty(lookup)) {
  159. if (!lookup.startsWith("java:")) {
  160. bindingConfiguration = new BindingConfiguration(name, new EjbLookupInjectionSource(lookup, localInterfaceType));
  161. } else {
  162. bindingConfiguration = new BindingConfiguration(name, new LookupInjectionSource(lookup));
  163. }
  164. } else if (!isEmpty(ejbName)) {
  165. bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(ejbName, localInterfaceType.getName(), name, deploymentUnit, appclient));
  166. } else {
  167. bindingConfiguration = new BindingConfiguration(name, ejbInjectionSource = new EjbInjectionSource(localInterfaceType.getName(), name, deploymentUnit, appclient));
  168. }
  169. if (ejbInjectionSource != null) {
  170. deploymentUnit.addToAttachmentList(EjbDeploymentAttachmentKeys.EJB_INJECTIONS, ejbInjectionSource);
  171. }
  172. bindingDescriptions.add(bindingConfiguration);
  173. }
  174. }
  175. }
  176. return bindingDescriptions;
  177. }
  178. private boolean isEmpty(String string) {
  179. return string == null || string.isEmpty();
  180. }
  181. }