1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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 **/
18 package org.apache.geronimo.transaction.context;
19
20 import javax.transaction.HeuristicMixedException;
21 import javax.transaction.HeuristicRollbackException;
22 import javax.transaction.InvalidTransactionException;
23 import javax.transaction.NotSupportedException;
24 import javax.transaction.RollbackException;
25 import javax.transaction.SystemException;
26 import javax.transaction.Transaction;
27 import javax.transaction.TransactionManager;
28 import javax.transaction.UserTransaction;
29
30 import org.apache.geronimo.transaction.ExtendedTransactionManager;
31
32 /***
33 * The GeronimoTransactionManager is a wrapper around the TransactionContextManager
34 * to offer both UserTransaction and TransactionManager interfaces.
35 *
36 * @version $Revision: 1.4 $
37 * @author <a href="mailto:gnt@codehaus.org">Guillaume Nodet</a>
38 */
39 public class GeronimoTransactionManager implements UserTransaction, TransactionManager {
40
41 TransactionContextManager transactionContextManager;
42
43 public GeronimoTransactionManager(TransactionContextManager transactionContextManager) {
44 this.transactionContextManager = transactionContextManager;
45 }
46
47 public void begin() throws NotSupportedException, SystemException {
48
49 if (transactionContextManager.getContext() == null) {
50 transactionContextManager.newUnspecifiedTransactionContext();
51 }
52 TransactionContext ctx = transactionContextManager.getContext();
53 if (ctx instanceof UnspecifiedTransactionContext == false) {
54 throw new NotSupportedException("Previous Transaction has not been committed");
55 }
56 UnspecifiedTransactionContext oldContext = (UnspecifiedTransactionContext) ctx;
57 GeronimoTransactionContext transactionContext = new GeronimoTransactionContext(
58 this,
59 (ExtendedTransactionManager) transactionContextManager.getTransactionManager(),
60 oldContext);
61 oldContext.suspend();
62 try {
63 transactionContext.begin(0);
64 } catch (SystemException e) {
65 oldContext.resume();
66 throw e;
67 } catch (NotSupportedException e) {
68 oldContext.resume();
69 throw e;
70 }
71 transactionContextManager.setContext(transactionContext);
72 }
73
74 public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
75 TransactionContext ctx = transactionContextManager.getContext();
76 if (ctx instanceof GeronimoTransactionContext == false) {
77 throw new IllegalStateException("Transaction has not been started");
78 }
79 GeronimoTransactionContext beanContext = (GeronimoTransactionContext) ctx;
80 try {
81 if (!beanContext.commit()) {
82 throw new RollbackException();
83 }
84 } finally {
85 transactionContextManager.setContext(null);
86 }
87 }
88
89 public int getStatus() throws SystemException {
90 return transactionContextManager.getStatus();
91 }
92
93 public void rollback() throws IllegalStateException, SecurityException, SystemException {
94 TransactionContext ctx = transactionContextManager.getContext();
95 if (ctx instanceof GeronimoTransactionContext == false) {
96 throw new IllegalStateException("Transaction has not been started");
97 }
98 GeronimoTransactionContext beanContext = (GeronimoTransactionContext) ctx;
99 try {
100 beanContext.rollback();
101 } finally {
102 transactionContextManager.setContext(null);
103 }
104 }
105
106 public void setRollbackOnly() throws IllegalStateException, SystemException {
107 transactionContextManager.setRollbackOnly();
108 }
109
110 public void setTransactionTimeout(int seconds) throws SystemException {
111 if (seconds < 0) {
112 throw new SystemException("transaction timeout must be positive or 0, not " + seconds);
113 }
114 transactionContextManager.setTransactionTimeout(seconds);
115 }
116
117 public Transaction getTransaction() throws SystemException {
118 Object context = transactionContextManager.getContext();
119 if (context == null || context instanceof UnspecifiedTransactionContext) {
120 return null;
121 } else if (context instanceof GeronimoTransactionContext){
122 return ((GeronimoTransactionContext) context).getTransactionDelegate();
123 } else {
124 throw new IllegalStateException("unrecognized transaction context");
125 }
126 }
127
128 public void resume(Transaction tx) throws IllegalStateException, InvalidTransactionException, SystemException {
129
130 if (transactionContextManager.getContext() == null) {
131 transactionContextManager.newUnspecifiedTransactionContext();
132 }
133 if (tx instanceof GeronimoTransactionDelegate == false) {
134 throw new InvalidTransactionException("invalid transaction specified");
135 }
136 TransactionContext ctx = ((GeronimoTransactionDelegate) tx).getContext();
137 transactionContextManager.resumeBeanTransactionContext(ctx);
138 }
139
140 public Transaction suspend() throws SystemException {
141 return ((GeronimoTransactionContext) transactionContextManager.suspendBeanTransactionContext()).getTransactionDelegate();
142 }
143
144 }