KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > pm > Tx


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.pm;
23
24 import java.io.Externalizable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import javax.jms.JMSException JavaDoc;
28 import javax.transaction.xa.Xid JavaDoc;
29
30 /**
31  * A transaction
32  *
33  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
34  * @version $Revision: 45317 $
35  */

36 public class Tx implements Comparable JavaDoc, Externalizable JavaDoc
37 {
38    /** The serialVersionUID */
39    static final long serialVersionUID = -5428592493990463362L;
40
41    /** Transaction open */
42    private static final int OPEN = 0;
43    /** Transaction ended */
44    private static final int ENDED = 2;
45
46    /** Restore unknown */
47    public static final int UNKNOWN = 0;
48
49    /** Restore addition */
50    public static final int ADD = 1;
51
52    /** Restore remove */
53    public static final int REMOVE = -1;
54    
55    /** The transaction value */
56    long value = 0;
57    
58    /** Whether the transaction has been persisted */
59    boolean persisted = false;
60
61    transient Xid JavaDoc xid;
62    
63    /** List of Runnable tasks */
64    transient ArrayList JavaDoc postCommitTasks;
65    /** List of Runnable tasks */
66    transient ArrayList JavaDoc postRollbackTasks;
67
68    /** The status */
69    transient int status = OPEN;
70
71    /** The lock */
72    transient Object JavaDoc lock = new Object JavaDoc();
73    
74    /**
75     * Create a new Tx for externailzation
76     */

77    public Tx()
78    {
79    }
80    
81    /**
82     * Create a new Tx
83     *
84     * @param value the value
85     */

86    public Tx(long value)
87    {
88       this.value = value;
89    }
90    
91    /**
92     * Set the value
93     *
94     * @param tx the new value
95     */

96    public void setValue(long tx)
97    {
98       value = tx;
99    }
100
101    /**
102     * Get the long value
103     *
104     * @return the long value
105     */

106    public long longValue()
107    {
108       return value;
109    }
110
111    /**
112     * Get the xid.
113     *
114     * @return the xid.
115     */

116    public Xid JavaDoc getXid()
117    {
118       return xid;
119    }
120
121    /**
122     * Set the xid.
123     *
124     * @param xid the xid.
125     */

126    public void setXid(Xid JavaDoc xid)
127    {
128       this.xid = xid;
129    }
130
131    /**
132     * Get whether the transaction has been persisted
133     *
134     * @return true when persisted
135     */

136    public synchronized boolean checkPersisted()
137    {
138       boolean result = persisted;
139       persisted = true;
140       return result;
141    }
142
143    /**
144     * Get whether the transaction has been persisted
145     *
146     * @return true when persisted
147     */

148    public synchronized boolean wasPersisted()
149    {
150       return persisted;
151    }
152
153    /**
154     * Compare
155     *
156     * @param anotherLong the other value
157     * @return -1, 0, 1 if less than, equal or greater than respectively
158     */

159    public int compareTo(Tx anotherLong)
160    {
161       long thisVal = this.value;
162       long anotherVal = anotherLong.value;
163       return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
164    }
165    
166    public int compareTo(Object JavaDoc o)
167    {
168       return compareTo((Tx) o);
169    }
170    
171    public void readExternal(java.io.ObjectInput JavaDoc in) throws java.io.IOException JavaDoc
172    {
173       value = in.readLong();
174    }
175
176    public void writeExternal(java.io.ObjectOutput JavaDoc out) throws java.io.IOException JavaDoc
177    {
178       out.writeLong(value);
179    }
180    
181    public String JavaDoc toString()
182    {
183       return String.valueOf(value);
184    }
185    
186    public int hashCode()
187    {
188       return (int) (value ^ (value >> 32));
189    }
190
191    /**
192     * Commit the transaction
193     *
194     * @param pm the persistence manager
195     * @throws JMSExecption for any error
196     */

197    void commit(PersistenceManager pm) throws JMSException JavaDoc
198    {
199       synchronized (lock)
200       {
201          if (status == ENDED)
202             throw new JMSException JavaDoc("Transaction is not active for commit");
203          status = ENDED;
204          postRollbackTasks = null;
205       }
206       
207       pm.commitPersistentTx(this);
208       
209       synchronized (lock)
210       {
211          if (postCommitTasks == null)
212             return;
213          
214          for (int i = 0; i < postCommitTasks.size(); ++i)
215          {
216             Runnable JavaDoc task = (Runnable JavaDoc) postCommitTasks.get(i);
217             task.run();
218          }
219          
220          postCommitTasks = null;
221       }
222    }
223
224    /**
225     * Add post commit task
226     *
227     * @param task the task
228     * @throws JMSExecption for any error
229     */

230    void addPostCommitTask(Runnable JavaDoc task) throws JMSException JavaDoc
231    {
232       synchronized (lock)
233       {
234          if (status == ENDED)
235             throw new JMSException JavaDoc("Transacation is not active.");
236          if (postCommitTasks == null)
237             postCommitTasks = new ArrayList JavaDoc();
238          postCommitTasks.add(task);
239       }
240    }
241
242    /**
243     * Commit the transaction
244     *
245     * @param pm the persistence manager
246     * @throws JMSExecption for any error
247     */

248    public void rollback(PersistenceManager pm) throws JMSException JavaDoc
249    {
250       synchronized (lock)
251       {
252          if (status == ENDED)
253             throw new JMSException JavaDoc("Transaction is not active for rollback");
254          status = ENDED;
255          postCommitTasks = null;
256       }
257       
258       pm.rollbackPersistentTx(this);
259       
260       synchronized (lock)
261       {
262          if (postRollbackTasks == null)
263             return;
264
265          for (int i = 0; i < postRollbackTasks.size(); ++i)
266          {
267             Runnable JavaDoc task = (Runnable JavaDoc) postRollbackTasks.get(i);
268             task.run();
269          }
270          
271          postRollbackTasks = null;
272       }
273    }
274
275    /**
276     * Add post rollback task
277     *
278     * @param task the task
279     * @throws JMSExecption for any error
280     */

281    public void addPostRollbackTask(Runnable JavaDoc task) throws JMSException JavaDoc
282    {
283       synchronized (lock)
284       {
285          if (status == ENDED)
286             throw new JMSException JavaDoc("Transacation is not active.");
287          if (postRollbackTasks == null)
288                postRollbackTasks = new ArrayList JavaDoc();
289          postRollbackTasks.add(task);
290       }
291    }
292 }
Popular Tags