001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.tm;
023:
024: import javax.transaction.Status;
025: import javax.transaction.SystemException;
026: import javax.transaction.Transaction;
027: import javax.transaction.TransactionManager;
028: import javax.transaction.UserTransaction;
029: import javax.transaction.xa.XAResource;
030:
031: import org.jboss.util.NestedRuntimeException;
032:
033: /**
034: * TxUtils.java has utility methods for determining transaction status
035: * in various useful ways.
036: *
037: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
038: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
039: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
040: * @version $Revision: 63567 $
041: */
042: public class TxUtils {
043: /** Transaction Status Strings */
044: private static final String[] TxStatusStrings = { "STATUS_ACTIVE",
045: "STATUS_MARKED_ROLLBACK", "STATUS_PREPARED",
046: "STATUS_COMMITTED", "STATUS_ROLLEDBACK", "STATUS_UNKNOWN",
047: "STATUS_NO_TRANSACTION", "STATUS_PREPARING",
048: "STATUS_COMMITTING", "STATUS_ROLLING_BACK" };
049:
050: /**
051: * Do now allow instances of this class
052: */
053: private TxUtils() {
054:
055: }
056:
057: public static boolean isActive(Transaction tx) {
058: if (tx == null)
059: return false;
060:
061: try {
062: int status = tx.getStatus();
063: return isActive(status);
064: } catch (SystemException error) {
065: throw new NestedRuntimeException(error);
066: }
067: }
068:
069: public static boolean isActive(TransactionManager tm) {
070: try {
071: return isActive(tm.getTransaction());
072: } catch (SystemException error) {
073: throw new NestedRuntimeException(error);
074: }
075: }
076:
077: public static boolean isActive(UserTransaction ut) {
078: try {
079: int status = ut.getStatus();
080: return isActive(status);
081: } catch (SystemException error) {
082: throw new NestedRuntimeException(error);
083: }
084: }
085:
086: public static boolean isActive(int status) {
087: return status == Status.STATUS_ACTIVE;
088: }
089:
090: public static boolean isUncommitted(Transaction tx) {
091: if (tx == null)
092: return false;
093:
094: try {
095: int status = tx.getStatus();
096: return isUncommitted(status);
097: } catch (SystemException error) {
098: throw new NestedRuntimeException(error);
099: }
100: }
101:
102: public static boolean isUncommitted(TransactionManager tm) {
103: try {
104: return isUncommitted(tm.getTransaction());
105: } catch (SystemException error) {
106: throw new NestedRuntimeException(error);
107: }
108: }
109:
110: public static boolean isUncommitted(UserTransaction ut) {
111: try {
112: int status = ut.getStatus();
113: return isUncommitted(status);
114:
115: } catch (SystemException error) {
116: throw new NestedRuntimeException(error);
117: }
118: }
119:
120: public static boolean isUncommitted(int status) {
121: return status == Status.STATUS_ACTIVE
122: || status == Status.STATUS_MARKED_ROLLBACK;
123: }
124:
125: public static boolean isCompleted(Transaction tx) {
126: if (tx == null)
127: return true;
128:
129: try {
130: int status = tx.getStatus();
131: return isCompleted(status);
132: } catch (SystemException error) {
133: throw new NestedRuntimeException(error);
134: }
135: }
136:
137: public static boolean isCompleted(TransactionManager tm) {
138: try {
139: return isCompleted(tm.getTransaction());
140: } catch (SystemException error) {
141: throw new NestedRuntimeException(error);
142: }
143: }
144:
145: public static boolean isCompleted(UserTransaction ut) {
146: try {
147: int status = ut.getStatus();
148: return isCompleted(status);
149:
150: } catch (SystemException error) {
151: throw new NestedRuntimeException(error);
152: }
153: }
154:
155: public static boolean isCompleted(int status) {
156: return status == Status.STATUS_COMMITTED
157: || status == Status.STATUS_ROLLEDBACK
158: || status == Status.STATUS_NO_TRANSACTION;
159: }
160:
161: public static boolean isRollback(Transaction tx) {
162: if (tx == null)
163: return false;
164:
165: try {
166: int status = tx.getStatus();
167: return isRollback(status);
168: } catch (SystemException error) {
169: throw new NestedRuntimeException(error);
170: }
171: }
172:
173: public static boolean isRollback(TransactionManager tm) {
174: try {
175: return isRollback(tm.getTransaction());
176: } catch (SystemException error) {
177: throw new NestedRuntimeException(error);
178: }
179: }
180:
181: public static boolean isRollback(UserTransaction ut) {
182: try {
183: int status = ut.getStatus();
184: return isRollback(status);
185: } catch (SystemException error) {
186: throw new NestedRuntimeException(error);
187: }
188: }
189:
190: public static boolean isRollback(int status) {
191: return status == Status.STATUS_MARKED_ROLLBACK
192: || status == Status.STATUS_ROLLING_BACK
193: || status == Status.STATUS_ROLLEDBACK;
194: }
195:
196: /**
197: * Converts a tx Status index to a String
198: *
199: * @see javax.transaction.Status
200: *
201: * @param status the Status index
202: * @return status as String or "STATUS_INVALID"
203: */
204: public static String getStatusAsString(int status) {
205: if (status >= Status.STATUS_ACTIVE
206: && status <= Status.STATUS_ROLLING_BACK) {
207: return TxStatusStrings[status];
208: } else {
209: return "STATUS_INVALID";
210: }
211: }
212:
213: /**
214: * Converts a XAResource flag to a String
215: *
216: * @see javax.transaction.xa.XAResource
217: *
218: * @param flags the flags passed in to start(), end(), recover()
219: * @return the flags in String form
220: */
221: public static String getXAResourceFlagsAsString(int flags) {
222: if (flags == XAResource.TMNOFLAGS) {
223: return "|TMNOFLAGS";
224: } else {
225: StringBuffer sbuf = new StringBuffer(64);
226:
227: if ((flags & XAResource.TMONEPHASE) != 0) {
228: sbuf.append("|TMONEPHASE");
229: }
230: if ((flags & XAResource.TMJOIN) != 0) {
231: sbuf.append("|TMJOIN");
232: }
233: if ((flags & XAResource.TMRESUME) != 0) {
234: sbuf.append("|TMRESUME");
235: }
236: if ((flags & XAResource.TMSUCCESS) != 0) {
237: sbuf.append("|TMSUCCESS");
238: }
239: if ((flags & XAResource.TMFAIL) != 0) {
240: sbuf.append("|TMFAIL");
241: }
242: if ((flags & XAResource.TMSUSPEND) != 0) {
243: sbuf.append("|TMSUSPEND");
244: }
245: if ((flags & XAResource.TMSTARTRSCAN) != 0) {
246: sbuf.append("|TMSTARTRSCAN");
247: }
248: if ((flags & XAResource.TMENDRSCAN) != 0) {
249: sbuf.append("|TMENDRSCAN");
250: }
251: return sbuf.toString();
252: }
253: }
254: }
|