KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ManagedTransactionContext


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import javax.jms.JMSException JavaDoc;
21 import javax.transaction.xa.XAException JavaDoc;
22 import javax.transaction.xa.XAResource JavaDoc;
23 import javax.transaction.xa.Xid JavaDoc;
24
25 import org.apache.activemq.TransactionContext;
26 import org.apache.activemq.command.TransactionId;
27 import org.apache.activemq.transaction.Synchronization;
28
29 /**
30  * Allows us to switch between using a shared transaction context,
31  * or using a local transaction context.
32  *
33  * @version $Revision$
34  */

35 public class ManagedTransactionContext extends TransactionContext {
36
37     private final TransactionContext sharedContext;
38     boolean useSharedTxContext=false;
39
40     public ManagedTransactionContext(TransactionContext sharedContext) {
41         super(sharedContext.getConnection());
42         this.sharedContext = sharedContext;
43         setLocalTransactionEventListener(sharedContext.getLocalTransactionEventListener());
44     }
45     
46     public void setUseSharedTxContext(boolean enable) throws JMSException JavaDoc {
47         if( isInLocalTransaction() || isInXATransaction() )
48             throw new JMSException JavaDoc("The resource is allready being used in transaction context.");
49         useSharedTxContext = enable;
50     }
51     
52     public void begin() throws JMSException JavaDoc {
53         if( useSharedTxContext )
54             sharedContext.begin();
55         else
56             super.begin();
57     }
58     public void commit() throws JMSException JavaDoc {
59         if( useSharedTxContext )
60             sharedContext.commit();
61         else
62             super.commit();
63     }
64
65     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
66         if( useSharedTxContext )
67             sharedContext.commit(xid, onePhase);
68         else
69             super.commit(xid, onePhase);
70     }
71
72     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
73         if( useSharedTxContext )
74             sharedContext.end(xid, flags);
75         else
76             super.end(xid, flags);
77     }
78
79     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
80         if( useSharedTxContext )
81             sharedContext.forget(xid);
82         else
83             super.forget(xid);
84     }
85
86     public TransactionId getTransactionId() {
87         if( useSharedTxContext )
88             return sharedContext.getTransactionId();
89         else
90             return super.getTransactionId();
91     }
92
93     public int getTransactionTimeout() throws XAException JavaDoc {
94         if( useSharedTxContext )
95             return sharedContext.getTransactionTimeout();
96         else
97             return super.getTransactionTimeout();
98     }
99
100     public boolean isInLocalTransaction() {
101         if( useSharedTxContext )
102             return sharedContext.isInLocalTransaction();
103         else
104             return super.isInLocalTransaction();
105     }
106
107     public boolean isInXATransaction() {
108         if( useSharedTxContext )
109             return sharedContext.isInXATransaction();
110         else
111             return super.isInXATransaction();
112     }
113
114     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
115         if( useSharedTxContext )
116             return sharedContext.isSameRM(xaResource);
117         else
118             return super.isSameRM(xaResource);
119     }
120
121     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
122         if( useSharedTxContext )
123             return sharedContext.prepare(xid);
124         else
125             return super.prepare(xid);
126     }
127
128     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
129         if( useSharedTxContext )
130             return sharedContext.recover(flag);
131         else
132             return super.recover(flag);
133     }
134
135     public void rollback() throws JMSException JavaDoc {
136         if( useSharedTxContext )
137             sharedContext.rollback();
138         else
139             super.rollback();
140     }
141
142     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
143         if( useSharedTxContext )
144             sharedContext.rollback(xid);
145         else
146             super.rollback(xid);
147     }
148
149     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
150         if( useSharedTxContext )
151             return sharedContext.setTransactionTimeout(seconds);
152         else
153             return super.setTransactionTimeout(seconds);
154     }
155
156     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
157         if( useSharedTxContext )
158             sharedContext.start(xid, flags);
159         else
160             super.start(xid, flags);
161     }
162     
163     public void addSynchronization(Synchronization s) {
164         if( useSharedTxContext )
165             sharedContext.addSynchronization(s);
166         else
167             super.addSynchronization(s);
168     }
169 }
170
Popular Tags