KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > XAResource


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 Bull SA
5  * Copyright (C) 1996 - 2000 Dyade
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA.
21  *
22  * Initial developer(s): Frederic Maistre (INRIA)
23  * Contributor(s): ScalAgent Distributed Technologies
24  */

25 package org.objectweb.joram.client.jms;
26
27 import javax.jms.JMSException JavaDoc;
28 import javax.transaction.xa.XAException JavaDoc;
29 import javax.transaction.xa.Xid JavaDoc;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.joram.shared.JoramTracing;
33
34 /**
35  * A <code>XAResource</code> instance is used by a <code>XASession</code>
36  * instance as a delegate to a Transaction Manager.
37  */

38 public class XAResource implements javax.transaction.xa.XAResource JavaDoc {
39   /** <code>true</code> if the resource is enlisted in a transaction. */
40   private boolean enlisted = false;
41   /** The current transaction identifier. */
42   private Xid JavaDoc currentXid = null;
43
44   /** The XA connection acting as resource manager. */
45   XAResourceMngr rm;
46
47   /** The session producing and consuming messages. */
48   Session sess;
49  
50
51   /**
52    * Constructs an XA resource representing a given session.
53    */

54   public XAResource(XAResourceMngr rm, Session sess) {
55     this.rm = rm;
56     this.sess = sess;
57
58     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
59       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
60                                  " XAResource rm = " + rm +
61                                  ", sess = " + sess);
62   }
63
64
65   /**
66    * Enlists this resource in a given transaction.
67    *
68    * @exception XAException If the resource is already enlisted in a
69    * transaction, or if the RM fails to enlist the
70    * resource.
71    */

72   public void start(Xid JavaDoc xid, int flag)
73     throws XAException JavaDoc {
74     if (enlisted)
75       throw new XAException JavaDoc("Resource already enlisted.");
76
77     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
78       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
79                                  this + ": start(" + xid +
80                                  ", " + flag + ")");
81
82     rm.start(xid, flag, sess);
83
84     enlisted = true;
85     currentXid = xid;
86   }
87
88   /**
89    * Delists this resource.
90    *
91    * @exception XAException If the resource is not enlisted in the specified
92    * transaction, or if the RM fails to delist the
93    * resource.
94    */

95   public void end(Xid JavaDoc xid, int flag)
96     throws XAException JavaDoc {
97     if (! enlisted || ! xid.equals(currentXid))
98       throw new XAException JavaDoc("Resource is not enlisted in specified"
99                             + " transaction.");
100
101     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
102       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
103                                  this + ": end(" + xid +
104                                  ", " + flag + ")");
105
106     rm.end(xid, flag, sess);
107
108     enlisted = false;
109     currentXid = null;
110   }
111
112   /**
113    * Prepares the resource.
114    *
115    * @exception XAException If the RM fails to prepare the resource.
116    */

117   public int prepare(Xid JavaDoc xid)
118     throws XAException JavaDoc {
119
120     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
121       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
122                                  this + ": prepare(" + xid + ")");
123     rm.prepare(xid);
124     return XA_OK;
125   }
126
127   /**
128    * Commits the resource.
129    *
130    * @exception XAException If the RM fails to commit the resource.
131    */

132   public void commit(Xid JavaDoc xid, boolean onePhase)
133     throws XAException JavaDoc {
134
135     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
136       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
137                                  this + ": commit(" + xid +
138                                  ", " + onePhase + ")");
139
140     if (onePhase)
141       rm.prepare(xid);
142
143     rm.commit(xid);
144   }
145
146   /**
147    * Rolls the resource back.
148    *
149    * @exception XAException If the RM fails to roll the resource back.
150    */

151   public void rollback(Xid JavaDoc xid)
152     throws XAException JavaDoc {
153
154     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
155       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
156                                  this + ": rollback(" + xid + ")");
157
158     if (enlisted && currentXid.equals(xid)) {
159       rm.end(xid, javax.transaction.xa.XAResource.TMFAIL, sess);
160       enlisted = false;
161       currentXid = null;
162     }
163
164     rm.rollback(xid);
165   }
166
167   /**
168    * Recovers the prepared transactions identifiers.
169    *
170    * @exception XAException If the RM fails to recover.
171    */

172   public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc
173   {
174     return rm.recover(flag);
175   }
176
177   /**
178    * Not implemented as transactions are not heuristically completed.
179    *
180    * @exception XAException Always thrown.
181    */

182   public void forget(Xid JavaDoc xid) throws XAException JavaDoc
183   {
184     throw new XAException JavaDoc("Non implemented method.");
185   }
186
187   /**
188    * Returns <code>false</code> as timeout feaure is not supported.
189    *
190    * @exception XAException Never thrown.
191    */

192   public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc
193   {
194     return false;
195   }
196
197   /**
198    * Returns 0 as timeout feaure is not supported.
199    *
200    * @exception XAException Never thrown.
201    */

202   public int getTransactionTimeout() throws XAException JavaDoc
203   {
204     return 0;
205   }
206
207   /**
208    * Checks wether this resource shares the same resource manager
209    * (XAConnection) with an other resource.
210    *
211    * @exception XAException Never thrown.
212    */

213   public boolean isSameRM(javax.transaction.xa.XAResource JavaDoc o)
214     throws XAException JavaDoc {
215
216     if (! (o instanceof org.objectweb.joram.client.jms.XAResource))
217       return false;
218
219     XAResource other = (org.objectweb.joram.client.jms.XAResource) o;
220
221    if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
222       JoramTracing.dbgClient.log(BasicLevel.DEBUG,
223                                  this + ": isSameRM other.rm = " + other.rm +
224                                  ", this.rm = " + this.rm +
225                                  ", equals = " + this.rm.equals(other.rm));
226
227     return this.rm.equals(other.rm);
228   }
229 }
230
Popular Tags