/impl/src/main/java/org/jboss/seam/transaction/DefaultSeamTransaction.java

https://github.com/LightGuard/seam-transaction · Java · 172 lines · 121 code · 28 blank · 23 comment · 0 complexity · 3264f8906cc0348ded2894a2794e05de MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
  4. * contributors by the @authors tag. See the copyright.txt in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jboss.seam.transaction;
  18. import javax.ejb.EJBContext;
  19. import javax.inject.Inject;
  20. import javax.naming.InitialContext;
  21. import javax.naming.NameNotFoundException;
  22. import javax.naming.NamingException;
  23. import javax.persistence.EntityManager;
  24. import javax.transaction.HeuristicMixedException;
  25. import javax.transaction.HeuristicRollbackException;
  26. import javax.transaction.NotSupportedException;
  27. import javax.transaction.RollbackException;
  28. import javax.transaction.Synchronization;
  29. import javax.transaction.SystemException;
  30. import org.jboss.solder.bean.defaultbean.DefaultBean;
  31. /**
  32. * Supports injection of a Seam UserTransaction object that wraps the current
  33. * JTA transaction or EJB container managed transaction.
  34. *
  35. * @author Stuart Douglas
  36. */
  37. @DefaultBean(SeamTransaction.class)
  38. @DefaultTransaction
  39. public class DefaultSeamTransaction implements SeamTransaction {
  40. @Inject
  41. private Synchronizations synchronizations;
  42. public void enlist(EntityManager entityManager) throws SystemException {
  43. getSeamTransaction().enlist(entityManager);
  44. }
  45. public boolean isActive() throws SystemException {
  46. return getSeamTransaction().isActive();
  47. }
  48. public boolean isActiveOrMarkedRollback() throws SystemException {
  49. return getSeamTransaction().isActiveOrMarkedRollback();
  50. }
  51. public boolean isCommitted() throws SystemException {
  52. return getSeamTransaction().isCommitted();
  53. }
  54. public boolean isConversationContextRequired() {
  55. return getSeamTransaction().isConversationContextRequired();
  56. }
  57. public boolean isMarkedRollback() throws SystemException {
  58. return getSeamTransaction().isMarkedRollback();
  59. }
  60. public boolean isNoTransaction() throws SystemException {
  61. return getSeamTransaction().isNoTransaction();
  62. }
  63. public boolean isRolledBack() throws SystemException {
  64. return getSeamTransaction().isRolledBack();
  65. }
  66. public boolean isRolledBackOrMarkedRollback() throws SystemException {
  67. return getSeamTransaction().isRolledBackOrMarkedRollback();
  68. }
  69. public void registerSynchronization(Synchronization sync) {
  70. getSeamTransaction().registerSynchronization(sync);
  71. }
  72. public void begin() throws NotSupportedException, SystemException {
  73. getSeamTransaction().begin();
  74. }
  75. public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
  76. getSeamTransaction().commit();
  77. }
  78. public int getStatus() throws SystemException {
  79. return getSeamTransaction().getStatus();
  80. }
  81. public void rollback() throws IllegalStateException, SecurityException, SystemException {
  82. getSeamTransaction().rollback();
  83. }
  84. public void setRollbackOnly() throws IllegalStateException, SystemException {
  85. getSeamTransaction().setRollbackOnly();
  86. }
  87. public void setTransactionTimeout(int seconds) throws SystemException {
  88. getSeamTransaction().setTransactionTimeout(seconds);
  89. }
  90. protected SeamTransaction getSeamTransaction() {
  91. try {
  92. return createUTTransaction();
  93. } catch (NameNotFoundException nnfe) {
  94. try {
  95. return createCMTTransaction();
  96. } catch (NameNotFoundException nnfe2) {
  97. return createNoTransaction();
  98. } catch (NamingException e) {
  99. throw new RuntimeException(e);
  100. }
  101. } catch (NamingException e) {
  102. throw new RuntimeException(e);
  103. }
  104. }
  105. protected SeamTransaction createNoTransaction() {
  106. return new NoTransaction();
  107. }
  108. protected SeamTransaction createCMTTransaction() throws NamingException {
  109. return new CMTTransaction(getEJBContext(), synchronizations);
  110. }
  111. protected SeamTransaction createUTTransaction() throws NamingException {
  112. return new UTTransaction(getUserTransaction(), synchronizations);
  113. }
  114. protected javax.transaction.UserTransaction getUserTransaction() throws NamingException {
  115. InitialContext context = new InitialContext();
  116. try {
  117. return (javax.transaction.UserTransaction) context.lookup("java:comp/UserTransaction");
  118. } catch (NameNotFoundException nnfe) {
  119. try {
  120. // Embedded JBoss has no java:comp/UserTransaction
  121. javax.transaction.UserTransaction ut = (javax.transaction.UserTransaction) context.lookup("UserTransaction");
  122. ut.getStatus(); // for glassfish, which can return an unusable UT
  123. return ut;
  124. } catch (Exception e) {
  125. throw nnfe;
  126. }
  127. }
  128. }
  129. public static String ejbContextName = "java:comp.ejb3/EJBContext";
  130. public static final String STANDARD_EJB_CONTEXT_NAME = "java:comp/EJBContext";
  131. public static EJBContext getEJBContext() throws NamingException {
  132. try {
  133. return (EJBContext) new InitialContext().lookup(ejbContextName);
  134. } catch (NameNotFoundException nnfe) {
  135. return (EJBContext) new InitialContext().lookup(STANDARD_EJB_CONTEXT_NAME);
  136. }
  137. }
  138. protected static String getEjbContextName() {
  139. return ejbContextName;
  140. }
  141. protected static void setEjbContextName(String ejbContextName) {
  142. DefaultSeamTransaction.ejbContextName = ejbContextName;
  143. }
  144. }