PageRenderTime 173ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/component/stateful/StatefulComponentDescription.java

#
Java | 374 lines | 275 code | 55 blank | 44 comment | 49 complexity | b76dc7f355f17b31cf9178963a71caba 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
  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.component.stateful;
  23. import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
  24. import java.lang.reflect.Method;
  25. import java.util.Collection;
  26. import java.util.Collections;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import javax.ejb.EJBLocalObject;
  30. import javax.ejb.EJBObject;
  31. import javax.ejb.TransactionManagementType;
  32. import org.jboss.as.ee.component.Component;
  33. import org.jboss.as.ee.component.ComponentConfiguration;
  34. import org.jboss.as.ee.component.ComponentConfigurator;
  35. import org.jboss.as.ee.component.ComponentDescription;
  36. import org.jboss.as.ee.component.ComponentInstanceInterceptorFactory;
  37. import org.jboss.as.ee.component.ViewConfiguration;
  38. import org.jboss.as.ee.component.ViewConfigurator;
  39. import org.jboss.as.ee.component.ViewDescription;
  40. import org.jboss.as.ee.component.interceptors.InterceptorOrder;
  41. import org.jboss.as.ee.component.serialization.WriteReplaceInterface;
  42. import org.jboss.as.ejb3.cache.CacheInfo;
  43. import org.jboss.as.ejb3.component.EJBViewDescription;
  44. import org.jboss.as.ejb3.component.MethodIntf;
  45. import org.jboss.as.ejb3.component.interceptors.ComponentTypeIdentityInterceptorFactory;
  46. import org.jboss.as.ejb3.component.session.SessionBeanComponentDescription;
  47. import org.jboss.as.ejb3.deployment.EjbJarDescription;
  48. import org.jboss.as.ejb3.tx.StatefulBMTInterceptor;
  49. import org.jboss.as.server.deployment.DeploymentPhaseContext;
  50. import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
  51. import org.jboss.as.server.deployment.reflect.ClassIndex;
  52. import org.jboss.as.server.deployment.reflect.ClassReflectionIndex;
  53. import org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex;
  54. import org.jboss.invocation.ImmediateInterceptorFactory;
  55. import org.jboss.invocation.Interceptor;
  56. import org.jboss.invocation.InterceptorFactory;
  57. import org.jboss.invocation.InterceptorFactoryContext;
  58. import org.jboss.invocation.proxy.MethodIdentifier;
  59. import org.jboss.metadata.ejb.spec.SessionBeanMetaData;
  60. import org.jboss.modules.ModuleLoader;
  61. import org.jboss.msc.service.ServiceName;
  62. /**
  63. * User: jpai
  64. */
  65. public class StatefulComponentDescription extends SessionBeanComponentDescription {
  66. private Method afterBegin;
  67. private Method afterCompletion;
  68. private Method beforeCompletion;
  69. private final Map<MethodIdentifier, StatefulRemoveMethod> removeMethods = new HashMap<MethodIdentifier, StatefulRemoveMethod>();
  70. private StatefulTimeoutInfo statefulTimeout;
  71. private CacheInfo cache;
  72. /**
  73. * Map of init method, to the corresponding home create method on the home interface
  74. */
  75. private Map<Method, String> initMethods = new HashMap<Method, String>(0);
  76. public class StatefulRemoveMethod {
  77. private final MethodIdentifier methodIdentifier;
  78. private final boolean retainIfException;
  79. StatefulRemoveMethod(final MethodIdentifier method, final boolean retainIfException) {
  80. if (method == null) {
  81. throw MESSAGES.removeMethodIsNull();
  82. }
  83. this.methodIdentifier = method;
  84. this.retainIfException = retainIfException;
  85. }
  86. public MethodIdentifier getMethodIdentifier() {
  87. return methodIdentifier;
  88. }
  89. public boolean isRetainIfException() {
  90. return retainIfException;
  91. }
  92. @Override
  93. public boolean equals(Object o) {
  94. if (this == o) return true;
  95. if (o == null || getClass() != o.getClass()) return false;
  96. StatefulRemoveMethod that = (StatefulRemoveMethod) o;
  97. if (!methodIdentifier.equals(that.methodIdentifier)) return false;
  98. return true;
  99. }
  100. @Override
  101. public int hashCode() {
  102. return methodIdentifier.hashCode();
  103. }
  104. }
  105. /**
  106. * Construct a new instance.
  107. *
  108. * @param componentName the component name
  109. * @param componentClassName the component instance class name
  110. * @param ejbJarDescription the module description
  111. */
  112. public StatefulComponentDescription(final String componentName, final String componentClassName, final EjbJarDescription ejbJarDescription,
  113. final ServiceName deploymentUnitServiceName, final SessionBeanMetaData descriptorData) {
  114. super(componentName, componentClassName, ejbJarDescription, deploymentUnitServiceName, descriptorData);
  115. addInitMethodInvokingInterceptor();
  116. }
  117. private void addInitMethodInvokingInterceptor() {
  118. getConfigurators().addFirst(new ComponentConfigurator() {
  119. @Override
  120. public void configure(DeploymentPhaseContext context, ComponentDescription description, ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
  121. configuration.addPostConstructInterceptor(StatefulInitMethodInterceptorFactory.INSTANCE, InterceptorOrder.ComponentPostConstruct.SFSB_INIT_METHOD);
  122. }
  123. });
  124. }
  125. private void addStatefulSessionSynchronizationInterceptor() {
  126. // we must run before the DefaultFirstConfigurator
  127. getConfigurators().addFirst(new ComponentConfigurator() {
  128. @Override
  129. public void configure(DeploymentPhaseContext context, ComponentDescription description, ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
  130. final InterceptorFactory interceptorFactory = StatefulSessionSynchronizationInterceptor.factory(getTransactionManagementType());
  131. configuration.addComponentInterceptor(interceptorFactory, InterceptorOrder.Component.SYNCHRONIZATION_INTERCEPTOR, false);
  132. }
  133. });
  134. }
  135. @Override
  136. public ComponentConfiguration createConfiguration(final ClassIndex classIndex, final ClassLoader moduleClassLoader, final ModuleLoader moduleLoader) {
  137. final ComponentConfiguration statefulComponentConfiguration = new ComponentConfiguration(this, classIndex, moduleClassLoader, moduleLoader);
  138. // setup the component create service
  139. statefulComponentConfiguration.setComponentCreateServiceFactory(new StatefulComponentCreateServiceFactory());
  140. if (getTransactionManagementType() == TransactionManagementType.BEAN) {
  141. getConfigurators().add(new ComponentConfigurator() {
  142. @Override
  143. public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
  144. final ComponentInstanceInterceptorFactory bmtComponentInterceptorFactory = new ComponentInstanceInterceptorFactory() {
  145. @Override
  146. protected Interceptor create(Component component, InterceptorFactoryContext context) {
  147. if (!(component instanceof StatefulSessionComponent)) {
  148. throw MESSAGES.componentNotInstanceOfSessionComponent(component, component.getComponentClass(), "stateful");
  149. }
  150. return new StatefulBMTInterceptor((StatefulSessionComponent) component);
  151. }
  152. };
  153. configuration.addComponentInterceptor(bmtComponentInterceptorFactory, InterceptorOrder.Component.BMT_TRANSACTION_INTERCEPTOR, false);
  154. }
  155. });
  156. }
  157. addStatefulSessionSynchronizationInterceptor();
  158. return statefulComponentConfiguration;
  159. }
  160. @Override
  161. public boolean allowsConcurrentAccess() {
  162. return true;
  163. }
  164. public Method getAfterBegin() {
  165. return afterBegin;
  166. }
  167. public Method getAfterCompletion() {
  168. return afterCompletion;
  169. }
  170. public Method getBeforeCompletion() {
  171. return beforeCompletion;
  172. }
  173. @Override
  174. public SessionBeanType getSessionBeanType() {
  175. return SessionBeanComponentDescription.SessionBeanType.STATEFUL;
  176. }
  177. public void setAfterBegin(final Method afterBegin) {
  178. this.afterBegin = afterBegin;
  179. }
  180. public void setAfterCompletion(final Method afterCompletion) {
  181. this.afterCompletion = afterCompletion;
  182. }
  183. public void setBeforeCompletion(final Method afterCompletion) {
  184. this.beforeCompletion = afterCompletion;
  185. }
  186. @Override
  187. protected void setupViewInterceptors(EJBViewDescription view) {
  188. // let super do its job
  189. super.setupViewInterceptors(view);
  190. // add the @Remove method interceptor
  191. this.addRemoveMethodInterceptor(view);
  192. // setup the instance associating interceptors
  193. this.addStatefulInstanceAssociatingInterceptor(view);
  194. this.addViewSerializationInterceptor(view);
  195. if (view.getMethodIntf() == MethodIntf.REMOTE) {
  196. view.getConfigurators().add(new ViewConfigurator() {
  197. @Override
  198. public void configure(final DeploymentPhaseContext context, final ComponentConfiguration componentConfiguration, final ViewDescription description, final ViewConfiguration configuration) throws DeploymentUnitProcessingException {
  199. final String earApplicationName = componentConfiguration.getComponentDescription().getModuleDescription().getEarApplicationName();
  200. configuration.setViewInstanceFactory(new StatefulRemoteViewInstanceFactory(earApplicationName, componentConfiguration.getModuleName(), componentConfiguration.getComponentDescription().getModuleDescription().getDistinctName(), componentConfiguration.getComponentName()));
  201. }
  202. });
  203. }
  204. }
  205. @Override
  206. protected ViewConfigurator getSessionBeanObjectViewConfigurator() {
  207. return StatefulSessionBeanObjectViewConfigurator.INSTANCE;
  208. }
  209. private void addViewSerializationInterceptor(final ViewDescription view) {
  210. view.setSerializable(true);
  211. view.setUseWriteReplace(true);
  212. view.getConfigurators().add(new ViewConfigurator() {
  213. @Override
  214. public void configure(final DeploymentPhaseContext context, final ComponentConfiguration componentConfiguration, final ViewDescription description, final ViewConfiguration configuration) throws DeploymentUnitProcessingException {
  215. final DeploymentReflectionIndex index = context.getDeploymentUnit().getAttachment(org.jboss.as.server.deployment.Attachments.REFLECTION_INDEX);
  216. ClassReflectionIndex<WriteReplaceInterface> classIndex = index.getClassIndex(WriteReplaceInterface.class);
  217. for (Method method : classIndex.getMethods()) {
  218. configuration.addClientInterceptor(method, new StatefulWriteReplaceInterceptor.Factory(configuration.getViewServiceName().getCanonicalName()), InterceptorOrder.Client.WRITE_REPLACE);
  219. }
  220. }
  221. });
  222. }
  223. public void addRemoveMethod(final MethodIdentifier removeMethod, final boolean retainIfException) {
  224. if (removeMethod == null) {
  225. throw MESSAGES.removeMethodIsNull();
  226. }
  227. this.removeMethods.put(removeMethod, new StatefulRemoveMethod(removeMethod, retainIfException));
  228. }
  229. public Collection<StatefulRemoveMethod> getRemoveMethods() {
  230. return this.removeMethods.values();
  231. }
  232. public StatefulTimeoutInfo getStatefulTimeout() {
  233. return statefulTimeout;
  234. }
  235. public void setStatefulTimeout(final StatefulTimeoutInfo statefulTimeout) {
  236. this.statefulTimeout = statefulTimeout;
  237. }
  238. private void addStatefulInstanceAssociatingInterceptor(final EJBViewDescription view) {
  239. view.getConfigurators().add(new ViewConfigurator() {
  240. @Override
  241. public void configure(final DeploymentPhaseContext context, final ComponentConfiguration componentConfiguration, ViewDescription description, ViewConfiguration viewConfiguration) throws DeploymentUnitProcessingException {
  242. EJBViewDescription ejbViewDescription = (EJBViewDescription) view;
  243. //if this is a home interface we add a different interceptor
  244. if (ejbViewDescription.getMethodIntf() == MethodIntf.HOME || ejbViewDescription.getMethodIntf() == MethodIntf.LOCAL_HOME) {
  245. for (Method method : viewConfiguration.getProxyFactory().getCachedMethods()) {
  246. if ((method.getName().equals("hashCode") && method.getParameterTypes().length == 0) ||
  247. method.getName().equals("equals") && method.getParameterTypes().length == 1 &&
  248. method.getParameterTypes()[0] == Object.class) {
  249. viewConfiguration.addClientInterceptor(method, ComponentTypeIdentityInterceptorFactory.INSTANCE, InterceptorOrder.Client.EJB_EQUALS_HASHCODE);
  250. }
  251. }
  252. } else {
  253. // interceptor factory return an interceptor which sets up the session id on component view instance creation
  254. final InterceptorFactory sessionIdGeneratingInterceptorFactory = StatefulComponentSessionIdGeneratingInterceptorFactory.INSTANCE;
  255. // add the session id generating interceptor to the start of the *post-construct interceptor chain of the ComponentViewInstance*
  256. viewConfiguration.addClientPostConstructInterceptor(sessionIdGeneratingInterceptorFactory, InterceptorOrder.ClientPostConstruct.INSTANCE_CREATE);
  257. for (Method method : viewConfiguration.getProxyFactory().getCachedMethods()) {
  258. if ((method.getName().equals("hashCode") && method.getParameterTypes().length == 0) ||
  259. method.getName().equals("equals") && method.getParameterTypes().length == 1 &&
  260. method.getParameterTypes()[0] == Object.class) {
  261. viewConfiguration.addClientInterceptor(method, StatefulIdentityInterceptorFactory.INSTANCE, InterceptorOrder.Client.EJB_EQUALS_HASHCODE);
  262. }
  263. }
  264. }
  265. }
  266. });
  267. if (view.getMethodIntf() != MethodIntf.LOCAL_HOME && view.getMethodIntf() != MethodIntf.HOME) {
  268. view.getConfigurators().add(new ViewConfigurator() {
  269. @Override
  270. public void configure(DeploymentPhaseContext context, ComponentConfiguration componentConfiguration, ViewDescription description, ViewConfiguration configuration) throws DeploymentUnitProcessingException {
  271. // add the instance associating interceptor to the *start of the invocation interceptor chain*
  272. configuration.addClientInterceptor(StatefulComponentIdInterceptor.Factory.INSTANCE, InterceptorOrder.Client.ASSOCIATING_INTERCEPTOR);
  273. configuration.addViewInterceptor(StatefulComponentInstanceInterceptor.FACTORY, InterceptorOrder.View.ASSOCIATING_INTERCEPTOR);
  274. }
  275. });
  276. }
  277. }
  278. private void addRemoveMethodInterceptor(final ViewDescription view) {
  279. view.getConfigurators().add(new ViewConfigurator() {
  280. @Override
  281. public void configure(DeploymentPhaseContext context, ComponentConfiguration componentConfiguration, ViewDescription description, ViewConfiguration configuration) throws DeploymentUnitProcessingException {
  282. final StatefulComponentDescription statefulComponentDescription = (StatefulComponentDescription) componentConfiguration.getComponentDescription();
  283. final Collection<StatefulRemoveMethod> removeMethods = statefulComponentDescription.getRemoveMethods();
  284. if (removeMethods.isEmpty()) {
  285. return;
  286. }
  287. for (final Method viewMethod : configuration.getProxyFactory().getCachedMethods()) {
  288. final MethodIdentifier viewMethodIdentifier = MethodIdentifier.getIdentifierForMethod(viewMethod);
  289. for (final StatefulRemoveMethod removeMethod : removeMethods) {
  290. if (removeMethod.methodIdentifier.equals(viewMethodIdentifier)) {
  291. //we do not want to add this if it is the Ejb(Local)Object.remove() method, as that is handed elsewhere
  292. final boolean object = EJBObject.class.isAssignableFrom(configuration.getViewClass()) || EJBLocalObject.class.isAssignableFrom(configuration.getViewClass());
  293. if (!object || !viewMethodIdentifier.getName().equals("remove") || viewMethodIdentifier.getParameterTypes().length != 0) {
  294. configuration.addViewInterceptor(viewMethod, new ImmediateInterceptorFactory(new StatefulRemoveInterceptor(removeMethod.retainIfException)), InterceptorOrder.View.SESSION_REMOVE_INTERCEPTOR);
  295. }
  296. break;
  297. }
  298. }
  299. }
  300. }
  301. });
  302. }
  303. public void addInitMethod(final Method method, final String createMethod) {
  304. initMethods.put(method, createMethod);
  305. }
  306. public Map<Method, String> getInitMethods() {
  307. return Collections.unmodifiableMap(initMethods);
  308. }
  309. public CacheInfo getCache() {
  310. return this.cache;
  311. }
  312. public void setCache(CacheInfo cache) {
  313. this.cache = cache;
  314. }
  315. @Override
  316. public boolean isPassivationApplicable() {
  317. return true;
  318. }
  319. }