PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/component/entity/EntityBeanComponentCreateService.java

#
Java | 235 lines | 175 code | 36 blank | 24 comment | 36 complexity | 98bbfacaa21e634314e777f1a1c6b8f3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2010, Red Hat Inc., and individual contributors as indicated
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * 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.component.entity;
  23. import java.lang.reflect.Method;
  24. import javax.ejb.EJBHome;
  25. import javax.ejb.EJBLocalHome;
  26. import javax.ejb.EJBLocalObject;
  27. import javax.ejb.EJBObject;
  28. import org.jboss.as.ee.component.BasicComponent;
  29. import org.jboss.as.ee.component.BasicComponentCreateService;
  30. import org.jboss.as.ee.component.ComponentConfiguration;
  31. import org.jboss.as.ee.component.ComponentCreateServiceFactory;
  32. import org.jboss.as.ee.component.TCCLInterceptor;
  33. import org.jboss.as.ejb3.EjbMessages;
  34. import org.jboss.as.ejb3.component.EJBComponentCreateService;
  35. import org.jboss.as.ejb3.component.EJBComponentCreateServiceFactory;
  36. import org.jboss.as.ejb3.component.InvokeMethodOnTargetInterceptor;
  37. import org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor;
  38. import org.jboss.as.ejb3.component.pool.PoolConfig;
  39. import org.jboss.as.ejb3.deployment.ApplicationExceptions;
  40. import org.jboss.invocation.ImmediateInterceptorFactory;
  41. import org.jboss.invocation.InterceptorFactory;
  42. import org.jboss.invocation.Interceptors;
  43. import org.jboss.metadata.ejb.spec.EntityBeanMetaData;
  44. import org.jboss.msc.value.InjectedValue;
  45. /**
  46. * @author Stuart Douglas
  47. */
  48. public class EntityBeanComponentCreateService extends EJBComponentCreateService {
  49. private final Class<EJBHome> homeClass;
  50. private final Class<EJBLocalHome> localHomeClass;
  51. private final Class<EJBObject> remoteClass;
  52. private final Class<EJBLocalObject> localClass;
  53. private final Class<Object> primaryKeyClass;
  54. private final Method ejbStoreMethod;
  55. private final Method ejbLoadMethod;
  56. private final Method ejbActivateMethod;
  57. private final Method ejbPassivateMethod;
  58. private final Method unsetEntityContextMethod;
  59. private final InterceptorFactory ejbStore;
  60. private final InterceptorFactory ejbLoad;
  61. private final InterceptorFactory ejbActivate;
  62. private final InterceptorFactory ejbPassivate;
  63. private final InterceptorFactory unsetEntityContext;
  64. private final InjectedValue<PoolConfig> poolConfig = new InjectedValue<PoolConfig>();
  65. private final InjectedValue<Boolean> defaultOptimisticLocking = new InjectedValue<Boolean>();
  66. public EntityBeanComponentCreateService(final ComponentConfiguration componentConfiguration, final ApplicationExceptions ejbJarConfiguration) {
  67. super(componentConfiguration, ejbJarConfiguration);
  68. final EntityBeanComponentDescription description = EntityBeanComponentDescription.class.cast(componentConfiguration.getComponentDescription());
  69. final EntityBeanMetaData beanMetaData = EntityBeanMetaData.class.cast(description.getDescriptorData());
  70. final ClassLoader classLoader = componentConfiguration.getComponentClass().getClassLoader();
  71. homeClass = (Class<EJBHome>) load(classLoader, beanMetaData.getHome());
  72. localHomeClass = (Class<EJBLocalHome>) load(classLoader, beanMetaData.getLocalHome());
  73. localClass = (Class<EJBLocalObject>) load(classLoader, beanMetaData.getLocal());
  74. remoteClass = (Class<EJBObject>) load(classLoader, beanMetaData.getRemote());
  75. primaryKeyClass = (Class<Object>) load(classLoader, beanMetaData.getPrimKeyClass());
  76. final InterceptorFactory tcclInterceptorFactory = new ImmediateInterceptorFactory(new TCCLInterceptor(componentConfiguration.getModuleClassLoader()));
  77. final InterceptorFactory namespaceContextInterceptorFactory = componentConfiguration.getNamespaceContextInterceptorFactory();
  78. Method ejbStore = null;
  79. Method ejbLoad = null;
  80. Method ejbActivate = null;
  81. Method ejbPassivate = null;
  82. Method unsetEntityContext = null;
  83. for (final Method method : componentConfiguration.getDefinedComponentMethods()) {
  84. if (method.getName().equals("ejbStore") && method.getParameterTypes().length == 0) {
  85. ejbStore = method;
  86. } else if (method.getName().equals("ejbLoad") && method.getParameterTypes().length == 0) {
  87. ejbLoad = method;
  88. } else if (method.getName().equals("ejbActivate") && method.getParameterTypes().length == 0) {
  89. ejbActivate = method;
  90. } else if (method.getName().equals("ejbPassivate") && method.getParameterTypes().length == 0) {
  91. ejbPassivate = method;
  92. } else if (method.getName().equals("unsetEntityContext") && method.getParameterTypes().length == 0) {
  93. unsetEntityContext = method;
  94. }
  95. }
  96. if (ejbStore == null) {
  97. throw EjbMessages.MESSAGES.couldNotFindEntityBeanMethod("ejbStore");
  98. } else if (ejbLoad == null) {
  99. throw EjbMessages.MESSAGES.couldNotFindEntityBeanMethod("ejbLoad");
  100. } else if (ejbActivate == null) {
  101. throw EjbMessages.MESSAGES.couldNotFindEntityBeanMethod("ejbActivate");
  102. } else if (ejbPassivate == null) {
  103. throw EjbMessages.MESSAGES.couldNotFindEntityBeanMethod("ejbPassivate");
  104. } else if (unsetEntityContext == null) {
  105. throw EjbMessages.MESSAGES.couldNotFindEntityBeanMethod("unsetEntityContext");
  106. }
  107. this.ejbActivateMethod = ejbActivate;
  108. this.ejbLoadMethod = ejbLoad;
  109. this.ejbStoreMethod = ejbStore;
  110. this.ejbPassivateMethod = ejbPassivate;
  111. this.unsetEntityContextMethod = unsetEntityContext;
  112. this.ejbActivate = Interceptors.getChainedInterceptorFactory(tcclInterceptorFactory, namespaceContextInterceptorFactory, CurrentInvocationContextInterceptor.FACTORY, invokeMethodOnTarget(ejbActivate));
  113. this.ejbLoad = Interceptors.getChainedInterceptorFactory(tcclInterceptorFactory, namespaceContextInterceptorFactory, CurrentInvocationContextInterceptor.FACTORY, invokeMethodOnTarget(ejbLoad));
  114. this.ejbStore = Interceptors.getChainedInterceptorFactory(tcclInterceptorFactory, namespaceContextInterceptorFactory, CurrentInvocationContextInterceptor.FACTORY, invokeMethodOnTarget(ejbStore));
  115. this.ejbPassivate = Interceptors.getChainedInterceptorFactory(tcclInterceptorFactory, namespaceContextInterceptorFactory, CurrentInvocationContextInterceptor.FACTORY, invokeMethodOnTarget(ejbPassivate));
  116. this.unsetEntityContext = Interceptors.getChainedInterceptorFactory(tcclInterceptorFactory, namespaceContextInterceptorFactory, CurrentInvocationContextInterceptor.FACTORY, invokeMethodOnTarget(unsetEntityContext));
  117. }
  118. private Class<?> load(ClassLoader classLoader, String ejbClass) {
  119. if (ejbClass != null) {
  120. try {
  121. return classLoader.loadClass(ejbClass);
  122. } catch (ClassNotFoundException e) {
  123. throw new RuntimeException("Failed to load component view class: " + ejbClass, e);
  124. }
  125. }
  126. return null;
  127. }
  128. private static InterceptorFactory invokeMethodOnTarget(final Method method) {
  129. method.setAccessible(true);
  130. return InvokeMethodOnTargetInterceptor.factory(method);
  131. }
  132. @Override
  133. protected BasicComponent createComponent() {
  134. return new EntityBeanComponent(this);
  135. }
  136. public static final ComponentCreateServiceFactory FACTORY = new EJBComponentCreateServiceFactory() {
  137. @Override
  138. public BasicComponentCreateService constructService(final ComponentConfiguration configuration) {
  139. return new EntityBeanComponentCreateService(configuration, this.ejbJarConfiguration);
  140. }
  141. };
  142. public Class<EJBHome> getHomeClass() {
  143. return homeClass;
  144. }
  145. public Class<EJBLocalHome> getLocalHomeClass() {
  146. return localHomeClass;
  147. }
  148. public Class<EJBObject> getRemoteClass() {
  149. return remoteClass;
  150. }
  151. public Class<EJBLocalObject> getLocalClass() {
  152. return localClass;
  153. }
  154. public Class<Object> getPrimaryKeyClass() {
  155. return primaryKeyClass;
  156. }
  157. public Method getEjbStoreMethod() {
  158. return ejbStoreMethod;
  159. }
  160. public Method getEjbLoadMethod() {
  161. return ejbLoadMethod;
  162. }
  163. public Method getEjbActivateMethod() {
  164. return ejbActivateMethod;
  165. }
  166. public InterceptorFactory getEjbStore() {
  167. return ejbStore;
  168. }
  169. public InterceptorFactory getEjbLoad() {
  170. return ejbLoad;
  171. }
  172. public InterceptorFactory getEjbActivate() {
  173. return ejbActivate;
  174. }
  175. public Method getEjbPassivateMethod() {
  176. return ejbPassivateMethod;
  177. }
  178. public InterceptorFactory getEjbPassivate() {
  179. return ejbPassivate;
  180. }
  181. public Method getUnsetEntityContextMethod() {
  182. return unsetEntityContextMethod;
  183. }
  184. public InterceptorFactory getUnsetEntityContext() {
  185. return unsetEntityContext;
  186. }
  187. public PoolConfig getPoolConfig() {
  188. return this.poolConfig.getOptionalValue();
  189. }
  190. public InjectedValue<PoolConfig> getPoolConfigInjector() {
  191. return this.poolConfig;
  192. }
  193. public Boolean getOptimisticLocking() {
  194. return defaultOptimisticLocking.getOptionalValue();
  195. }
  196. public InjectedValue<Boolean> getOptimisticLockingInjector() {
  197. return defaultOptimisticLocking;
  198. }
  199. }