KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > ra > RATransactionContext


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.ra;
48
49 import java.util.Collection JavaDoc;
50
51 import javax.jms.JMSException JavaDoc;
52 import javax.transaction.xa.XAException JavaDoc;
53 import javax.transaction.xa.XAResource JavaDoc;
54 import javax.transaction.xa.Xid JavaDoc;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.mr.api.jms.MantaSession;
59 import org.mr.api.jms.MantaXADescriptor;
60 import org.mr.api.jms.TransactionContext;
61
62 /**
63  * Allows us to switch between using a shared transaction context,
64  * or using a local transaction context.
65  */

66 public class RATransactionContext extends TransactionContext {
67
68     private static final Log log = LogFactory.getLog(RATransactionContext.class);
69     private final TransactionContext sharedContext;
70     boolean useSharedTxContext=false;
71
72     //public RATransactionContext(TransactionContext sharedContext, MantaSession session) {
73
public RATransactionContext(TransactionContext sharedContext) {
74         super();
75         this.sharedContext = sharedContext;
76         setLocalTransactionEventListener(sharedContext.getLocalTransactionEventListener());
77     }
78     
79     public void setUseSharedTxContext(boolean enable) throws JMSException JavaDoc {
80         if (enable && (isInLocalTransaction() || isInXATransaction()))
81             throw new JMSException JavaDoc("The resource is already being used in transaction context.");
82         useSharedTxContext = enable;
83     }
84     
85     public void addSession(MantaSession session) {
86         super.addSession(session);
87         sharedContext.addSession(session);
88     }
89     public void removeSession(MantaSession session) {
90         super.removeSession(session);
91         sharedContext.removeSession(session);
92     }
93
94     public void begin() throws JMSException JavaDoc {
95         if (useSharedTxContext)
96             sharedContext.begin();
97         else
98             super.begin();
99     }
100     public void commit() throws JMSException JavaDoc {
101         if (useSharedTxContext)
102             sharedContext.commit();
103         else
104             super.commit();
105     }
106     
107     public void rollback() throws JMSException JavaDoc {
108         if (useSharedTxContext)
109             sharedContext.rollback();
110         else
111             super.rollback();
112     }
113
114     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
115         if (useSharedTxContext)
116             sharedContext.commit(xid, onePhase);
117         else
118             super.commit(xid, onePhase);
119     }
120
121     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
122         if (useSharedTxContext)
123             sharedContext.end(xid, flags);
124         else
125             super.end(xid, flags);
126     }
127
128     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
129         if (useSharedTxContext)
130             sharedContext.forget(xid);
131         else
132             super.forget(xid);
133     }
134
135     public Object JavaDoc getTransactionId() {
136         if (useSharedTxContext)
137             return sharedContext.getTransactionId();
138         else
139             return super.getTransactionId();
140     }
141
142     public int getTransactionTimeout() throws XAException JavaDoc {
143         if (useSharedTxContext)
144             return sharedContext.getTransactionTimeout();
145         else
146             return super.getTransactionTimeout();
147     }
148
149     public boolean isInLocalTransaction() {
150         if (useSharedTxContext)
151             return sharedContext.isInLocalTransaction();
152         else
153             return super.isInLocalTransaction();
154     }
155
156     public boolean isInXATransaction() {
157         if (useSharedTxContext)
158             return sharedContext.isInXATransaction();
159         else
160             return super.isInXATransaction();
161     }
162
163     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
164         if (useSharedTxContext)
165             return sharedContext.isSameRM(xaResource);
166         else
167             return super.isSameRM(xaResource);
168     }
169
170     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
171         if (useSharedTxContext)
172             return sharedContext.prepare(xid);
173         else
174             return super.prepare(xid);
175     }
176
177     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
178         if (useSharedTxContext)
179             return sharedContext.recover(flag);
180         else
181             return super.recover(flag);
182     }
183
184     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
185         if (useSharedTxContext)
186             sharedContext.rollback(xid);
187         else
188             super.rollback(xid);
189     }
190
191     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
192         if (useSharedTxContext)
193             return sharedContext.setTransactionTimeout(seconds);
194         else
195             return super.setTransactionTimeout(seconds);
196     }
197
198     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
199         if (useSharedTxContext)
200             sharedContext.start(xid, flags);
201         else
202             super.start(xid, flags);
203     }
204 }
205
Popular Tags