PageRenderTime 46ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 190 lines | 136 code | 24 blank | 30 comment | 7 complexity | 75f36f505b0bfe42c2ba45e6a6301ee5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright (c) 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.stateful;
  23. import java.io.ObjectStreamException;
  24. import java.lang.reflect.Method;
  25. import java.nio.ByteBuffer;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.UUID;
  29. import java.util.concurrent.atomic.AtomicReference;
  30. import javax.ejb.EJBException;
  31. import org.jboss.as.ee.component.Component;
  32. import org.jboss.as.ee.component.ComponentInstance;
  33. import org.jboss.as.ejb3.cache.Cacheable;
  34. import org.jboss.as.ejb3.component.InvokeMethodOnTargetInterceptor;
  35. import org.jboss.as.ejb3.component.session.SessionBeanComponentInstance;
  36. import org.jboss.as.naming.ManagedReference;
  37. import org.jboss.ejb.client.SessionID;
  38. import org.jboss.invocation.Interceptor;
  39. import org.jboss.invocation.InterceptorContext;
  40. import org.jboss.invocation.InterceptorFactoryContext;
  41. /**
  42. * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
  43. */
  44. public class StatefulSessionComponentInstance extends SessionBeanComponentInstance implements Cacheable<SessionID> {
  45. private static final long serialVersionUID = 3803978357389448971L;
  46. private final SessionID id;
  47. private final Interceptor afterBegin;
  48. private final Interceptor afterCompletion;
  49. private final Interceptor beforeCompletion;
  50. private final Interceptor prePassivate;
  51. private final Interceptor postActivate;
  52. private final Interceptor ejb2XRemoveInterceptor;
  53. private volatile Map<Object, Object> serializableInterceptors;
  54. /**
  55. * Construct a new instance.
  56. *
  57. * @param component the component
  58. */
  59. protected StatefulSessionComponentInstance(final StatefulSessionComponent component, final AtomicReference<ManagedReference> instanceReference, final Interceptor preDestroyInterceptor, final Map<Method, Interceptor> methodInterceptors, final InterceptorFactoryContext factoryContext) {
  60. super(component, instanceReference, preDestroyInterceptor, methodInterceptors);
  61. final SessionID existingSession = (SessionID) factoryContext.getContextData().get(SessionID.class);
  62. if (existingSession != null) {
  63. this.id = existingSession;
  64. } else {
  65. SessionID id = null;
  66. do {
  67. final UUID uuid = UUID.randomUUID();
  68. ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
  69. bb.putLong(uuid.getMostSignificantBits());
  70. bb.putLong(uuid.getLeastSignificantBits());
  71. id = SessionID.createSessionID(bb.array());
  72. } while (!component.getCache().hasAffinity(id));
  73. this.id = id;
  74. }
  75. this.afterBegin = component.createInterceptor(component.getAfterBegin(), factoryContext);
  76. this.afterCompletion = component.createInterceptor(component.getAfterCompletion(), factoryContext);
  77. this.beforeCompletion = component.createInterceptor(component.getBeforeCompletion(), factoryContext);
  78. this.prePassivate = component.createInterceptor(component.getPrePassivate(), factoryContext);
  79. this.postActivate = component.createInterceptor(component.getPostActivate(), factoryContext);
  80. this.ejb2XRemoveInterceptor = component.createInterceptor(component.getEjb2XRemoveMethod(), factoryContext);
  81. }
  82. protected void afterBegin() {
  83. CurrentSynchronizationCallback.set(CurrentSynchronizationCallback.CallbackType.AFTER_BEGIN);
  84. try {
  85. execute(afterBegin, getComponent().getAfterBeginMethod());
  86. } finally {
  87. CurrentSynchronizationCallback.clear();
  88. }
  89. }
  90. protected void afterCompletion(boolean committed) {
  91. CurrentSynchronizationCallback.set(CurrentSynchronizationCallback.CallbackType.AFTER_COMPLETION);
  92. try {
  93. execute(afterCompletion, getComponent().getAfterCompletionMethod(), committed);
  94. } finally {
  95. CurrentSynchronizationCallback.clear();
  96. }
  97. }
  98. protected void beforeCompletion() {
  99. CurrentSynchronizationCallback.set(CurrentSynchronizationCallback.CallbackType.BEFORE_COMPLETION);
  100. try {
  101. execute(beforeCompletion, getComponent().getBeforeCompletionMethod());
  102. } finally {
  103. CurrentSynchronizationCallback.clear();
  104. }
  105. }
  106. protected void prePassivate() {
  107. this.execute(prePassivate, null);
  108. }
  109. protected void postActivate() {
  110. this.execute(postActivate, null);
  111. }
  112. public void discard() {
  113. if (!isDiscarded()) {
  114. super.discard();
  115. getComponent().getCache().discard(id);
  116. }
  117. }
  118. private Object execute(final Interceptor interceptor, final Method method, final Object... parameters) {
  119. if (interceptor == null)
  120. return null;
  121. final InterceptorContext interceptorContext = new InterceptorContext();
  122. //we need the method so this does not count as a lifecycle invocation
  123. interceptorContext.setMethod(method);
  124. interceptorContext.putPrivateData(Component.class, getComponent());
  125. interceptorContext.putPrivateData(ComponentInstance.class, this);
  126. interceptorContext.putPrivateData(InvokeMethodOnTargetInterceptor.PARAMETERS_KEY, parameters);
  127. interceptorContext.setContextData(new HashMap<String, Object>());
  128. try {
  129. return interceptor.processInvocation(interceptorContext);
  130. } catch (Error e) {
  131. throw e;
  132. } catch (RuntimeException e) {
  133. throw e;
  134. } catch (Exception e) {
  135. throw new EJBException(e);
  136. }
  137. }
  138. public Map<Object, Object> getSerializableInterceptors() {
  139. return serializableInterceptors;
  140. }
  141. public void setSerializableInterceptors(final Map<Object, Object> serializableInterceptors) {
  142. this.serializableInterceptors = serializableInterceptors;
  143. }
  144. @Override
  145. public StatefulSessionComponent getComponent() {
  146. return (StatefulSessionComponent) super.getComponent();
  147. }
  148. @Override
  149. public boolean isModified() {
  150. return true;
  151. }
  152. @Override
  153. public SessionID getId() {
  154. return id;
  155. }
  156. public Interceptor getEjb2XRemoveInterceptor() {
  157. return ejb2XRemoveInterceptor;
  158. }
  159. @Override
  160. public String toString() {
  161. return " Instance of " + getComponent().getComponentName() + " {" + id + "}";
  162. }
  163. public Object writeReplace() throws ObjectStreamException {
  164. return new SerializedStatefulSessionComponent(getInstanceReference().get(), id, getComponent().getCreateServiceName().getCanonicalName(), serializableInterceptors);
  165. }
  166. }