PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 156 lines | 95 code | 26 blank | 35 comment | 8 complexity | f09e301e488e577241d1ce9f4a61d163 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.interceptors;
  23. import java.lang.reflect.InvocationTargetException;
  24. import java.lang.reflect.Method;
  25. import java.util.concurrent.atomic.AtomicReference;
  26. import javax.transaction.Status;
  27. import javax.transaction.Synchronization;
  28. import javax.transaction.TransactionSynchronizationRegistry;
  29. import org.jboss.as.ee.component.Component;
  30. import org.jboss.as.ee.component.interceptors.InvocationType;
  31. import org.jboss.as.ejb3.component.entity.EntityBeanComponent;
  32. import org.jboss.as.ejb3.component.entity.EntityBeanComponentInstance;
  33. import org.jboss.invocation.Interceptor;
  34. import org.jboss.invocation.InterceptorContext;
  35. import org.jboss.invocation.InterceptorFactory;
  36. import org.jboss.invocation.InterceptorFactoryContext;
  37. import org.jboss.invocation.Interceptors;
  38. import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
  39. /**
  40. * Interceptor factory for entity beans that class the corresponding ejbCreate method.
  41. * <p/>
  42. * This is a post construct interceptor for the Ejb(Local)Object view
  43. *
  44. * @author Stuart Douglas
  45. */
  46. public class EntityBeanEjbCreateMethodInterceptorFactory implements InterceptorFactory {
  47. public static final EntityBeanEjbCreateMethodInterceptorFactory INSTANCE = new EntityBeanEjbCreateMethodInterceptorFactory();
  48. protected EntityBeanEjbCreateMethodInterceptorFactory() {
  49. }
  50. @Override
  51. public Interceptor create(InterceptorFactoryContext context) {
  52. final Object existing = context.getContextData().get(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY);
  53. final AtomicReference<Object> primaryKeyReference = new AtomicReference<Object>();
  54. context.getContextData().put(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY, primaryKeyReference);
  55. final Method ejbCreate = (Method) context.getContextData().get(EntityBeanHomeCreateInterceptorFactory.EJB_CREATE_METHOD_KEY);
  56. final Method ejbPostCreate = (Method) context.getContextData().get(EntityBeanHomeCreateInterceptorFactory.EJB_POST_CREATE_METHOD_KEY);
  57. final Object[] params = (Object[]) context.getContextData().get(EntityBeanHomeCreateInterceptorFactory.PARAMETERS_KEY);
  58. return new Interceptor() {
  59. @Override
  60. public Object processInvocation(final InterceptorContext context) throws Exception {
  61. if (existing != null) {
  62. primaryKeyReference.set(existing);
  63. return existing;
  64. }
  65. final Component component = context.getPrivateData(Component.class);
  66. if (!(component instanceof EntityBeanComponent)) {
  67. throw MESSAGES.unexpectedComponent(component, EntityBeanComponent.class);
  68. }
  69. final EntityBeanComponent entityBeanComponent = (EntityBeanComponent) component;
  70. //grab an unasociated entity bean from the pool
  71. final EntityBeanComponentInstance instance = entityBeanComponent.acquireUnAssociatedInstance();
  72. //call the ejbCreate method
  73. final Object primaryKey = invokeEjbCreate(context, ejbCreate, instance, params);
  74. instance.associate(primaryKey);
  75. primaryKeyReference.set(primaryKey);
  76. //now add the instance to the cache, so it is usable
  77. //note that we do not release it back to the pool
  78. //the cache will do that when it is expired or removed
  79. boolean synchronizationRegistered = false;
  80. boolean exception = false;
  81. entityBeanComponent.getCache().create(instance);
  82. try {
  83. invokeEjbPostCreate(context, ejbPostCreate, instance, params);
  84. //if a transaction is active we register a sync
  85. //and if the transaction is rolled back we release the instance back into the pool
  86. final TransactionSynchronizationRegistry transactionSynchronizationRegistry = entityBeanComponent.getTransactionSynchronizationRegistry();
  87. if (transactionSynchronizationRegistry.getTransactionKey() != null) {
  88. transactionSynchronizationRegistry.registerInterposedSynchronization(new Synchronization() {
  89. @Override
  90. public void beforeCompletion() {
  91. }
  92. @Override
  93. public void afterCompletion(final int status) {
  94. entityBeanComponent.getCache().release(instance, status == Status.STATUS_COMMITTED);
  95. }
  96. });
  97. synchronizationRegistered = true;
  98. }
  99. return context.proceed();
  100. } catch (Exception e) {
  101. entityBeanComponent.getCache().release(instance, false);
  102. exception = true;
  103. throw e;
  104. } finally {
  105. if (!synchronizationRegistered && !exception) {
  106. entityBeanComponent.getCache().release(instance, true);
  107. }
  108. }
  109. }
  110. };
  111. }
  112. protected void invokeEjbPostCreate(final InterceptorContext context, final Method ejbPostCreate, final EntityBeanComponentInstance instance, final Object[] params) throws Exception {
  113. try {
  114. ejbPostCreate.invoke(instance.getInstance(), params);
  115. } catch (InvocationTargetException e) {
  116. throw Interceptors.rethrow(e.getCause());
  117. }
  118. }
  119. protected Object invokeEjbCreate(final InterceptorContext context, final Method ejbCreate, final EntityBeanComponentInstance instance, final Object[] params) throws Exception {
  120. final InvocationType invocationType = context.getPrivateData(InvocationType.class);
  121. try {
  122. context.putPrivateData(InvocationType.class, InvocationType.ENTITY_EJB_CREATE);
  123. return ejbCreate.invoke(instance.getInstance(), params);
  124. } catch (InvocationTargetException e) {
  125. throw Interceptors.rethrow(e.getCause());
  126. } finally {
  127. context.putPrivateData(InvocationType.class, invocationType);
  128. }
  129. }
  130. }