KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > TxUtils


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.tm;
23
24 import javax.transaction.Status JavaDoc;
25 import javax.transaction.SystemException JavaDoc;
26 import javax.transaction.Transaction JavaDoc;
27 import javax.transaction.TransactionManager JavaDoc;
28 import javax.transaction.UserTransaction JavaDoc;
29 import javax.transaction.xa.XAException JavaDoc;
30 import javax.transaction.xa.XAResource JavaDoc;
31
32 import org.jboss.util.NestedRuntimeException;
33
34 /**
35  * TxUtils.java has utility methods for determining transaction status
36  * in various useful ways.
37  *
38  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
41  * @version $1.0$
42  */

43 public class TxUtils
44 {
45    /** Transaction Status Strings */
46    private static final String JavaDoc[] TxStatusStrings =
47    {
48       "STATUS_ACTIVE",
49       "STATUS_MARKED_ROLLBACK",
50       "STATUS_PREPARED",
51       "STATUS_COMMITTED",
52       "STATUS_ROLLEDBACK",
53       "STATUS_UNKNOWN",
54       "STATUS_NO_TRANSACTION",
55       "STATUS_PREPARING",
56       "STATUS_COMMITTING",
57       "STATUS_ROLLING_BACK"
58    };
59
60    /**
61     * Do now allow instances of this class
62     */

63    private TxUtils()
64    {
65
66    }
67
68    public static boolean isActive(Transaction JavaDoc tx)
69    {
70       try
71       {
72          return tx != null && (tx.getStatus() == Status.STATUS_ACTIVE);
73       }
74       catch (SystemException JavaDoc error)
75       {
76          throw new NestedRuntimeException(error);
77       }
78    }
79
80    public static boolean isActive(TransactionManager JavaDoc tm)
81    {
82       try
83       {
84          return isActive(tm.getTransaction());
85       }
86       catch (SystemException JavaDoc error)
87       {
88          throw new NestedRuntimeException(error);
89       }
90    }
91
92    public static boolean isActive(UserTransaction JavaDoc ut)
93    {
94       try
95       {
96          return ut.getStatus() == Status.STATUS_ACTIVE;
97       }
98       catch (SystemException JavaDoc error)
99       {
100          throw new NestedRuntimeException(error);
101       }
102    }
103
104    public static boolean isUncommitted(Transaction JavaDoc tx)
105    {
106       try
107       {
108          if (tx == null)
109             return false;
110          int status = tx.getStatus();
111          return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
112       }
113       catch (SystemException JavaDoc error)
114       {
115          throw new NestedRuntimeException(error);
116       }
117    }
118
119    public static boolean isUncommitted(TransactionManager JavaDoc tm)
120    {
121       try
122       {
123          return isUncommitted(tm.getTransaction());
124       }
125       catch (SystemException JavaDoc error)
126       {
127          throw new NestedRuntimeException(error);
128       }
129    }
130
131    public static boolean isCompleted(Transaction JavaDoc tx)
132    {
133       try
134       {
135          if (tx == null)
136             return true;
137          int status = tx.getStatus();
138          return status == Status.STATUS_COMMITTED
139              || status == Status.STATUS_ROLLEDBACK
140              || status == Status.STATUS_NO_TRANSACTION;
141       }
142       catch (SystemException JavaDoc error)
143       {
144          throw new NestedRuntimeException(error);
145       }
146    }
147
148    public static boolean isCompleted(TransactionManager JavaDoc tm)
149    {
150       try
151       {
152          return isCompleted(tm.getTransaction());
153       }
154       catch (SystemException JavaDoc error)
155       {
156          throw new NestedRuntimeException(error);
157       }
158    }
159
160    public static boolean isCompleted(UserTransaction JavaDoc ut)
161    {
162       try
163       {
164          int status = ut.getStatus();
165          return status == Status.STATUS_COMMITTED
166              || status == Status.STATUS_ROLLEDBACK
167              || status == Status.STATUS_NO_TRANSACTION;
168       }
169       catch (SystemException JavaDoc error)
170       {
171          throw new NestedRuntimeException(error);
172       }
173    }
174    
175    /**
176     * Converts a tx Status index to a String
177     *
178     * @see javax.transaction.Status
179     *
180     * @param status the Status index
181     * @return status as String or "STATUS_INVALID(value)"
182     */

183    public static String JavaDoc getStatusAsString(int status)
184    {
185       if (status >= Status.STATUS_ACTIVE && status <= Status.STATUS_ROLLING_BACK)
186       {
187          return TxStatusStrings[status];
188       }
189       else
190       {
191          return "STATUS_INVALID(" + status + ")";
192       }
193    }
194    
195    /**
196     * Converts a XAResource flag to a String
197     *
198     * @see javax.transaction.xa.XAResource
199     *
200     * @param flags the flags passed in to start(), end(), recover()
201     * @return the flags in String form
202     */

203    public static String JavaDoc getXAResourceFlagsAsString(int flags)
204    {
205       if (flags == XAResource.TMNOFLAGS)
206       {
207          return "|TMNOFLAGS";
208       }
209       else
210       {
211          StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(64);
212          
213          if ((flags & XAResource.TMONEPHASE) != 0)
214          {
215             sbuf.append("|TMONEPHASE");
216          }
217          if ((flags & XAResource.TMJOIN) != 0)
218          {
219             sbuf.append("|TMJOIN");
220          }
221          if ((flags & XAResource.TMRESUME) != 0)
222          {
223             sbuf.append("|TMRESUME");
224          }
225          if ((flags & XAResource.TMSUCCESS) != 0)
226          {
227             sbuf.append("|TMSUCCESS");
228          }
229          if ((flags & XAResource.TMFAIL) != 0)
230          {
231             sbuf.append("|TMFAIL");
232          }
233          if ((flags & XAResource.TMSUSPEND) != 0)
234          {
235             sbuf.append("|TMSUSPEND");
236          }
237          if ((flags & XAResource.TMSTARTRSCAN) != 0)
238          {
239             sbuf.append("|TMSTARTRSCAN");
240          }
241          if ((flags & XAResource.TMENDRSCAN) != 0)
242          {
243             sbuf.append("|TMENDRSCAN");
244          }
245          return sbuf.toString();
246       }
247    }
248    
249    /**
250     * Converts a XAException error code to a string.
251     *
252     * @see javax.transaction.xa.XAException
253     *
254     * @param errorCode an XAException error code
255     * @return the error code in String form.
256     *
257     */

258    public static String JavaDoc getXAErrorCodeAsString(int errorCode)
259    {
260       switch (errorCode)
261       {
262       case XAException.XA_HEURCOM:
263          return "XA_HEURCOM";
264       case XAException.XA_HEURHAZ:
265          return "XA_HEURHAZ";
266       case XAException.XA_HEURMIX:
267          return "XA_HEURMIX";
268       case XAException.XA_HEURRB:
269          return "XA_HEURRB";
270       case XAException.XA_NOMIGRATE:
271          return "XA_NOMIGRATE";
272       case XAException.XA_RBCOMMFAIL:
273          return "XA_RBCOMMFAIL";
274       case XAException.XA_RBDEADLOCK:
275          return "XA_RBDEADLOCK";
276       case XAException.XA_RBINTEGRITY:
277          return "XA_RBINTEGRITY";
278       case XAException.XA_RBOTHER:
279          return "XA_RBOTHER";
280       case XAException.XA_RBPROTO:
281          return "XA_RBPROTO";
282       case XAException.XA_RBROLLBACK:
283          return "XA_RBROLLBACK";
284       case XAException.XA_RBTIMEOUT:
285          return "XA_RBTIMEOUT";
286       case XAException.XA_RBTRANSIENT:
287          return "XA_RBTRANSIENT";
288       case XAException.XA_RDONLY:
289          return "XA_RDONLY";
290       case XAException.XA_RETRY:
291          return "XA_RETRY";
292
293       case XAException.XAER_ASYNC:
294          return "XAER_ASYNC";
295       case XAException.XAER_DUPID:
296          return "XAER_DUPID";
297       case XAException.XAER_INVAL:
298          return "XAER_INVAL";
299       case XAException.XAER_NOTA:
300          return "XAER_NOTA";
301       case XAException.XAER_OUTSIDE:
302          return "XAER_OUTSIDE";
303       case XAException.XAER_PROTO:
304          return "XAER_PROTO";
305       case XAException.XAER_RMERR:
306          return "XAER_RMERR";
307       case XAException.XAER_RMFAIL:
308          return "XAER_RMFAIL";
309       default:
310          return "XA_UNKNOWN(" + errorCode + ")";
311       }
312    }
313
314 }
315
Popular Tags