KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > SpyXAResource


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq;
23
24 import javax.transaction.xa.XAException JavaDoc;
25 import javax.transaction.xa.XAResource JavaDoc;
26 import javax.transaction.xa.Xid JavaDoc;
27
28 import org.jboss.logging.Logger;
29
30 /**
31  * This class implements the XAResource interface for used with an XASession.
32  *
33  * @author Hiram Chirino (Cojonudo14@hotmail.com)
34  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
35  * @version $Revision: 45399 $
36  */

37 public class SpyXAResource implements XAResource JavaDoc
38 {
39    /** The log */
40    private static final Logger log = Logger.getLogger(SpyXAResource.class);
41    
42    /** Whether trace is enabled */
43    private static boolean trace = log.isTraceEnabled();
44
45    /** The session */
46    SpySession session;
47
48    /**
49     * Create a new SpyXAResource
50     *
51     * @param session the session
52     */

53    SpyXAResource(SpySession session)
54    {
55       trace = log.isTraceEnabled();
56       
57       this.session = session;
58       
59       if (trace)
60          log.trace("Created " + this);
61    }
62
63    public boolean setTransactionTimeout(int arg1) throws XAException JavaDoc
64    {
65       return false;
66    }
67
68    public int getTransactionTimeout() throws XAException JavaDoc
69    {
70       return 0;
71    }
72
73    public boolean isSameRM(XAResource JavaDoc arg1) throws XAException JavaDoc
74    {
75       if (!(arg1 instanceof SpyXAResource))
76          return false;
77       return ((SpyXAResource) arg1).session.connection.spyXAResourceManager == session.connection.spyXAResourceManager;
78    }
79
80    public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc
81    {
82       if (trace)
83          log.trace("Commit xid=" + xid + ", onePhase=" + onePhase + " " + this);
84
85       xid = convertXid(xid);
86       try
87       {
88          session.connection.spyXAResourceManager.commit(xid, onePhase);
89       }
90       catch (Throwable JavaDoc t)
91       {
92          throw SpyXAException.getAsXAException("Resource manager error during commit", t);
93       }
94    }
95
96    public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc
97    {
98       if (trace)
99          log.trace("End xid=" + xid + ", flags=" + flags + " " +this);
100
101       xid = convertXid(xid);
102       synchronized (session.runLock)
103       {
104
105          switch (flags)
106          {
107             case TMSUSPEND :
108                session.unsetCurrentTransactionId(xid);
109                session.connection.spyXAResourceManager.suspendTx(xid);
110                break;
111             case TMFAIL :
112                session.unsetCurrentTransactionId(xid);
113                session.connection.spyXAResourceManager.endTx(xid, false);
114                break;
115             case TMSUCCESS :
116                session.unsetCurrentTransactionId(xid);
117                session.connection.spyXAResourceManager.endTx(xid, true);
118                break;
119          }
120       }
121    }
122
123    public void forget(Xid JavaDoc xid) throws XAException JavaDoc
124    {
125       if (trace)
126          log.trace("Forget xid=" + xid + " " + this);
127       
128       xid = convertXid(xid);
129       try
130       {
131          session.connection.spyXAResourceManager.forget(xid);
132       }
133       catch (Throwable JavaDoc t)
134       {
135          throw SpyXAException.getAsXAException("Resource manager error during forget", t);
136       }
137    }
138
139    public int prepare(Xid JavaDoc xid) throws XAException JavaDoc
140    {
141       if (trace)
142          log.trace("Prepare xid=" + xid + " " + this);
143
144       xid = convertXid(xid);
145       try
146       {
147          return session.connection.spyXAResourceManager.prepare(xid);
148       }
149       catch (Throwable JavaDoc t)
150       {
151          throw SpyXAException.getAsXAException("Resource manager error during prepare", t);
152       }
153    }
154
155    public Xid JavaDoc[] recover(int arg1) throws XAException JavaDoc
156    {
157       if (trace)
158          log.trace("Recover arg1=" + arg1 + " " + this);
159
160       try
161       {
162          return session.connection.spyXAResourceManager.recover(arg1);
163       }
164       catch (Throwable JavaDoc t)
165       {
166          throw SpyXAException.getAsXAException("Resource manager error during recover", t);
167       }
168    }
169
170    public void rollback(Xid JavaDoc xid) throws XAException JavaDoc
171    {
172       if (trace)
173          log.trace("Rollback xid=" + xid + " " + this);
174
175       xid = convertXid(xid);
176       try
177       {
178          session.connection.spyXAResourceManager.rollback(xid);
179       }
180       catch (Throwable JavaDoc t)
181       {
182          throw SpyXAException.getAsXAException("Resource manager error during rollback", t);
183       }
184    }
185
186    public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc
187    {
188       if (trace)
189          log.trace("Start xid=" + xid + ", flags=" + flags + " " + this);
190
191       xid = convertXid(xid);
192
193       boolean convertTx = false;
194       if (session.getCurrentTransactionId() != null)
195       {
196          if (flags == TMNOFLAGS && session.getCurrentTransactionId() instanceof Long JavaDoc)
197          {
198             convertTx = true;
199          }
200          else
201          {
202             XAException JavaDoc e = new XAException JavaDoc("Attempt to enlist in " + xid + " with TMNOFLAGS when already enlisted " + session.getCurrentTransactionId());
203             e.errorCode = XAException.XAER_OUTSIDE; // REVIEW: Is this the correct error code?
204
throw e;
205          }
206       }
207
208       synchronized (session.runLock)
209       {
210
211          switch (flags)
212          {
213             case TMNOFLAGS :
214                if (convertTx)
215                {
216                   // it was an anonymous TX, TM is now taking control over it.
217
// convert it over to a normal XID tansaction.
218
session.setCurrentTransactionId(session.connection.spyXAResourceManager
219                         .convertTx((Long JavaDoc) session.getCurrentTransactionId(), xid));
220                }
221                else
222                {
223                   session.setCurrentTransactionId(session.connection.spyXAResourceManager.startTx(xid));
224                }
225                break;
226             case TMJOIN :
227                session.setCurrentTransactionId(session.connection.spyXAResourceManager.joinTx(xid));
228                break;
229             case TMRESUME :
230                session.setCurrentTransactionId(session.connection.spyXAResourceManager.resumeTx(xid));
231                break;
232          }
233          session.runLock.notify();
234       }
235    }
236    
237    public String JavaDoc toString()
238    {
239       return "SpyXAResource[session=" + session + ']';
240    }
241    
242    /**
243     * Convert the external xid to our internal one
244     *
245     * @param xid the xid
246     * @return our internal xid
247     * @throws XAException for a null xid
248     */

249    protected Xid JavaDoc convertXid(Xid JavaDoc xid) throws XAException JavaDoc
250    {
251       if (xid == null)
252       {
253          XAException JavaDoc e = new XAException JavaDoc("Null xid");
254          e.errorCode = XAException.XAER_NOTA;
255          throw e;
256       }
257       
258       if (xid instanceof JBossMQXid)
259          return xid;
260       
261       return new JBossMQXid(xid);
262    }
263 }
Popular Tags