/ejb3/src/main/java/org/jboss/as/ejb3/component/entity/EntityBeanComponentInstance.java

https://github.com/dobozysaurus/jboss-as · Java · 218 lines · 152 code · 25 blank · 41 comment · 7 complexity · 349cacc0c86103588850ab42c2ee4502 MD5 · raw file

  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.component.entity;
  23. import java.lang.reflect.Method;
  24. import java.rmi.RemoteException;
  25. import java.util.Map;
  26. import java.util.concurrent.atomic.AtomicReference;
  27. import javax.ejb.EJBLocalObject;
  28. import javax.ejb.EJBObject;
  29. import javax.ejb.EntityBean;
  30. import org.jboss.as.ee.component.BasicComponent;
  31. import org.jboss.as.ejb3.component.EjbComponentInstance;
  32. import org.jboss.as.ejb3.context.EntityContextImpl;
  33. import org.jboss.as.naming.ManagedReference;
  34. import org.jboss.invocation.Interceptor;
  35. import org.jboss.invocation.InterceptorContext;
  36. /**
  37. * @author Stuart Douglas
  38. */
  39. public class EntityBeanComponentInstance extends EjbComponentInstance {
  40. /**
  41. * The primary key of this instance, is it is associated with an object identity
  42. */
  43. private volatile Object primaryKey;
  44. private volatile boolean isDiscarded;
  45. private volatile EntityContextImpl entityContext;
  46. private volatile boolean removed = false;
  47. private volatile boolean synchronizeRegistered;
  48. private final Interceptor ejbStore;
  49. private final Interceptor ejbActivate;
  50. private final Interceptor ejbLoad;
  51. private final Interceptor ejbPassivate;
  52. protected EntityBeanComponentInstance(final BasicComponent component, final AtomicReference<ManagedReference> instanceReference, final Interceptor preDestroyInterceptor, final Map<Method, Interceptor> methodInterceptors, final Map<Method, Interceptor> timeoutInterceptors) {
  53. super(component, instanceReference, preDestroyInterceptor, methodInterceptors, timeoutInterceptors);
  54. final EntityBeanComponent ejbComponent = (EntityBeanComponent)component;
  55. this.ejbStore = ejbComponent.createInterceptor(ejbComponent.getEjbStore());
  56. this.ejbActivate = ejbComponent.createInterceptor(ejbComponent.getEjbActivate());
  57. this.ejbLoad = ejbComponent.createInterceptor(ejbComponent.getEjbLoad());
  58. this.ejbPassivate = ejbComponent.createInterceptor(ejbComponent.getEjbPassivate());
  59. }
  60. @Override
  61. public EntityBeanComponent getComponent() {
  62. return (EntityBeanComponent) super.getComponent();
  63. }
  64. @Override
  65. public EntityBean getInstance() {
  66. return (EntityBean) super.getInstance();
  67. }
  68. public Object getPrimaryKey() {
  69. return primaryKey;
  70. }
  71. public void discard() {
  72. if (!isDiscarded) {
  73. isDiscarded = true;
  74. getComponent().getCache().discard(this);
  75. this.primaryKey = null;
  76. }
  77. }
  78. @Override
  79. public void destroy() {
  80. try {
  81. getInstance().unsetEntityContext();
  82. } catch (RemoteException e) {
  83. throw new RuntimeException(e);
  84. }
  85. super.destroy();
  86. }
  87. /**
  88. * Associates this entity with a primary key. This method is called when an entity bean is moved from the
  89. * pool to the entity cache
  90. *
  91. * @param primaryKey The primary key to associate the entity with
  92. */
  93. public synchronized void associate(Object primaryKey) {
  94. this.primaryKey = primaryKey;
  95. try {
  96. final InterceptorContext context = prepareInterceptorContext();
  97. final EntityBeanComponent component = getComponent();
  98. final Method ejbActivateMethod = component.getEjbActivateMethod();
  99. context.setMethod(ejbActivateMethod);
  100. ejbActivate.processInvocation(context);
  101. final InterceptorContext loadContext = prepareInterceptorContext();
  102. loadContext.setMethod(component.getEjbLoadMethod());
  103. ejbLoad.processInvocation(loadContext);
  104. } catch (RemoteException e) {
  105. throw new WrappedRemoteException(e);
  106. } catch (RuntimeException e) {
  107. throw e;
  108. } catch (Exception e) {
  109. throw new RuntimeException(e);
  110. }
  111. }
  112. /**
  113. * Invokes the ejbStore method
  114. */
  115. public synchronized void store() {
  116. try {
  117. if (!removed) {
  118. final InterceptorContext context = prepareInterceptorContext();
  119. final EntityBeanComponent component = getComponent();
  120. context.setMethod(component.getEjbStoreMethod());
  121. ejbStore.processInvocation(context);
  122. }
  123. } catch (RemoteException e) {
  124. throw new WrappedRemoteException(e);
  125. } catch (RuntimeException e) {
  126. throw e;
  127. } catch (Exception e) {
  128. throw new RuntimeException(e);
  129. }
  130. }
  131. /**
  132. * Prepares the instance for release by calling the ejbPassivate method.
  133. * <p/>
  134. * This method does not actually release this instance into the pool
  135. */
  136. public synchronized void passivate() {
  137. try {
  138. if (!removed) {
  139. final InterceptorContext context = prepareInterceptorContext();
  140. final EntityBeanComponent component = getComponent();
  141. context.setMethod(component.getEjbPassivateMethod());
  142. ejbPassivate.processInvocation(context);
  143. }
  144. } catch (RemoteException e) {
  145. throw new WrappedRemoteException(e);
  146. } catch (RuntimeException e) {
  147. throw e;
  148. } catch (Exception e) {
  149. throw new RuntimeException(e);
  150. }
  151. }
  152. public void setupContext() {
  153. try {
  154. final EntityContextImpl entityContext = new EntityContextImpl(this);
  155. setEjbContext(entityContext);
  156. getInstance().setEntityContext(entityContext);
  157. } catch (RemoteException e) {
  158. throw new WrappedRemoteException(e);
  159. }
  160. }
  161. public EntityContextImpl getEjbContext() {
  162. return entityContext;
  163. }
  164. protected void setEjbContext(EntityContextImpl entityContext) {
  165. this.entityContext = entityContext;
  166. }
  167. public EJBObject getEjbObject() {
  168. final Object pk = getPrimaryKey();
  169. if (pk == null) {
  170. throw new IllegalStateException("Cannot call getEjbObject before the object is associated with a primary key");
  171. }
  172. return getComponent().getEJBObject(pk);
  173. }
  174. public EJBLocalObject getEjbLocalObject() {
  175. final Object pk = getPrimaryKey();
  176. if (pk == null) {
  177. throw new IllegalStateException("Cannot call getEjbLocalObject before the object is associated with a primary key");
  178. }
  179. return getComponent().getEjbLocalObject(pk);
  180. }
  181. public boolean isRemoved() {
  182. return removed;
  183. }
  184. public void setRemoved(final boolean removed) {
  185. this.removed = removed;
  186. }
  187. public synchronized void setSynchronizationRegistered(final boolean synchronizeRegistered) {
  188. this.synchronizeRegistered = synchronizeRegistered;
  189. }
  190. public synchronized boolean isSynchronizeRegistered() {
  191. return synchronizeRegistered;
  192. }
  193. }