001 /**
002 *
003 * Copyright 2004 Hiram Chirino
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.ra;
019
020 import javax.jms.JMSException;
021 import javax.transaction.xa.XAException;
022 import javax.transaction.xa.XAResource;
023 import javax.transaction.xa.Xid;
024
025 import org.activemq.ActiveMQSession;
026 import org.activemq.TransactionContext;
027
028 /**
029 * Allows us to switch between using a shared transaction context,
030 * or using a local transaction context.
031 *
032 * @version $Revision: 1.1.1.1 $
033 */
034 public class RATransactionContext extends TransactionContext {
035
036 private final TransactionContext sharedContext;
037 boolean useSharedTxContext=false;
038
039 public RATransactionContext(TransactionContext sharedContext) {
040 super(sharedContext.getConnection());
041 this.sharedContext = sharedContext;
042 setLocalTransactionEventListener(sharedContext.getLocalTransactionEventListener());
043 }
044
045 public void setUseSharedTxContext(boolean enable) throws JMSException {
046 if( isInLocalTransaction() || isInXATransaction() )
047 throw new JMSException("The resource is allready being used in transaction context.");
048 useSharedTxContext = enable;
049 }
050
051 public void addSession(ActiveMQSession session) {
052 super.addSession(session);
053 sharedContext.addSession(session);
054 }
055 public void removeSession(ActiveMQSession session) {
056 super.removeSession(session);
057 sharedContext.removeSession(session);
058 }
059
060 public void begin() throws JMSException {
061 if( useSharedTxContext )
062 sharedContext.begin();
063 else
064 super.begin();
065 }
066 public void commit() throws JMSException {
067 if( useSharedTxContext )
068 sharedContext.commit();
069 else
070 super.commit();
071 }
072
073 public void commit(Xid xid, boolean onePhase) throws XAException {
074 if( useSharedTxContext )
075 sharedContext.commit(xid, onePhase);
076 else
077 super.commit(xid, onePhase);
078 }
079
080 public void end(Xid xid, int flags) throws XAException {
081 if( useSharedTxContext )
082 sharedContext.end(xid, flags);
083 else
084 super.end(xid, flags);
085 }
086
087 public void forget(Xid xid) throws XAException {
088 if( useSharedTxContext )
089 sharedContext.forget(xid);
090 else
091 super.forget(xid);
092 }
093
094 public Object getTransactionId() {
095 if( useSharedTxContext )
096 return sharedContext.getTransactionId();
097 else
098 return super.getTransactionId();
099 }
100
101 public int getTransactionTimeout() throws XAException {
102 if( useSharedTxContext )
103 return sharedContext.getTransactionTimeout();
104 else
105 return super.getTransactionTimeout();
106 }
107
108 public boolean isInLocalTransaction() {
109 if( useSharedTxContext )
110 return sharedContext.isInLocalTransaction();
111 else
112 return super.isInLocalTransaction();
113 }
114
115 public boolean isInXATransaction() {
116 if( useSharedTxContext )
117 return sharedContext.isInXATransaction();
118 else
119 return super.isInXATransaction();
120 }
121
122 public boolean isSameRM(XAResource xaResource) throws XAException {
123 if( useSharedTxContext )
124 return sharedContext.isSameRM(xaResource);
125 else
126 return super.isSameRM(xaResource);
127 }
128
129 public int prepare(Xid xid) throws XAException {
130 if( useSharedTxContext )
131 return sharedContext.prepare(xid);
132 else
133 return super.prepare(xid);
134 }
135
136 public Xid[] recover(int flag) throws XAException {
137 if( useSharedTxContext )
138 return sharedContext.recover(flag);
139 else
140 return super.recover(flag);
141 }
142
143 public void rollback() throws JMSException {
144 if( useSharedTxContext )
145 sharedContext.rollback();
146 else
147 super.rollback();
148 }
149
150 public void rollback(Xid xid) throws XAException {
151 if( useSharedTxContext )
152 sharedContext.rollback(xid);
153 else
154 super.rollback(xid);
155 }
156
157 public boolean setTransactionTimeout(int seconds) throws XAException {
158 if( useSharedTxContext )
159 return sharedContext.setTransactionTimeout(seconds);
160 else
161 return super.setTransactionTimeout(seconds);
162 }
163
164 public void start(Xid xid, int flags) throws XAException {
165 if( useSharedTxContext )
166 sharedContext.start(xid, flags);
167 else
168 super.start(xid, flags);
169 }
170 }