KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transaction > AbstractSingleResourceTransaction


1 /*
2  * $Id: AbstractSingleResourceTransaction.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.transaction;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
14
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.umo.TransactionException;
18
19 /**
20  * This abstract class can be used as a base class for transactions that can enlist
21  * only one resource (such as a JMS session or JDBC connection).
22  */

23 public abstract class AbstractSingleResourceTransaction extends AbstractTransaction
24 {
25
26     protected volatile Object JavaDoc key;
27     protected volatile Object JavaDoc resource;
28
29     protected final AtomicBoolean started = new AtomicBoolean(false);
30     protected final AtomicBoolean committed = new AtomicBoolean(false);
31     protected final AtomicBoolean rolledBack = new AtomicBoolean(false);
32     protected final AtomicBoolean rollbackOnly = new AtomicBoolean(false);
33
34     /*
35      * (non-Javadoc)
36      *
37      * @see org.mule.umo.UMOTransaction#begin()
38      */

39     public void begin() throws TransactionException
40     {
41         super.begin();
42         started.compareAndSet(false, true);
43     }
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see org.mule.umo.UMOTransaction#commit()
49      */

50     public void commit() throws TransactionException
51     {
52         super.commit();
53         committed.compareAndSet(false, true);
54     }
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see org.mule.umo.UMOTransaction#rollback()
60      */

61     public void rollback() throws TransactionException
62     {
63         super.rollback();
64         rolledBack.compareAndSet(false, true);
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.mule.umo.UMOTransaction#getStatus()
71      */

72     public int getStatus() throws TransactionStatusException
73     {
74         if (rolledBack.get())
75         {
76             return STATUS_ROLLEDBACK;
77         }
78         if (committed.get())
79         {
80             return STATUS_COMMITTED;
81         }
82         if (rollbackOnly.get())
83         {
84             return STATUS_MARKED_ROLLBACK;
85         }
86         if (started.get())
87         {
88             return STATUS_ACTIVE;
89         }
90         return STATUS_NO_TRANSACTION;
91     }
92
93     /*
94      * (non-Javadoc)
95      *
96      * @see org.mule.umo.UMOTransaction#getResource(java.lang.Object)
97      */

98     public Object JavaDoc getResource(Object JavaDoc key)
99     {
100         return key != null && this.key == key ? this.resource : null;
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.mule.umo.UMOTransaction#hasResource(java.lang.Object)
107      */

108     public boolean hasResource(Object JavaDoc key)
109     {
110         return key != null && this.key == key;
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
117      * java.lang.Object)
118      */

119     public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
120     {
121         if (key == null)
122         {
123             throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_BIND_TO_NULL_KEY));
124         }
125         if (resource == null)
126         {
127             throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_BIND_NULL_RESOURCE));
128         }
129         if (this.key != null)
130         {
131             throw new IllegalTransactionStateException(new Message(Messages.TX_SINGLE_RESOURCE_ONLY));
132         }
133         this.key = key;
134         this.resource = resource;
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.mule.umo.UMOTransaction#setRollbackOnly()
141      */

142     public void setRollbackOnly()
143     {
144         rollbackOnly.set(true);
145     }
146
147     public Object JavaDoc getId()
148     {
149         return key;
150     }
151
152     /**
153      * Really begin the transaction. Note that resources are enlisted yet.
154      *
155      * @throws TransactionException
156      */

157     protected abstract void doBegin() throws TransactionException;
158
159     /**
160      * Commit the transaction on the underlying resource
161      *
162      * @throws TransactionException
163      */

164     protected abstract void doCommit() throws TransactionException;
165
166     /**
167      * Rollback the transaction on the underlying resource
168      *
169      * @throws TransactionException
170      */

171     protected abstract void doRollback() throws TransactionException;
172
173 }
174
Popular Tags