View Javadoc

1   /*
2    * $Id: AbstractSingleResourceTransaction.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 CPAL v1.0
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 org.mule.config.i18n.CoreMessages;
14  import org.mule.umo.TransactionException;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
17  
18  /**
19   * This abstract class can be used as a base class for transactions that can enlist
20   * only one resource (such as a JMS session or JDBC connection).
21   */
22  public abstract class AbstractSingleResourceTransaction extends AbstractTransaction
23  {
24  
25      protected volatile Object key;
26      protected volatile Object resource;
27  
28      protected final AtomicBoolean started = new AtomicBoolean(false);
29      protected final AtomicBoolean committed = new AtomicBoolean(false);
30      protected final AtomicBoolean rolledBack = new AtomicBoolean(false);
31      protected final AtomicBoolean rollbackOnly = new AtomicBoolean(false);
32  
33      /*
34       * (non-Javadoc)
35       * 
36       * @see org.mule.umo.UMOTransaction#begin()
37       */
38      public void begin() throws TransactionException
39      {
40          super.begin();
41          started.compareAndSet(false, true);
42      }
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see org.mule.umo.UMOTransaction#commit()
48       */
49      public void commit() throws TransactionException
50      {
51          super.commit();
52          committed.compareAndSet(false, true);
53      }
54  
55      /*
56       * (non-Javadoc)
57       * 
58       * @see org.mule.umo.UMOTransaction#rollback()
59       */
60      public void rollback() throws TransactionException
61      {
62          super.rollback();
63          rolledBack.compareAndSet(false, true);
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see org.mule.umo.UMOTransaction#getStatus()
70       */
71      public int getStatus() throws TransactionStatusException
72      {
73          if (rolledBack.get())
74          {
75              return STATUS_ROLLEDBACK;
76          }
77          if (committed.get())
78          {
79              return STATUS_COMMITTED;
80          }
81          if (rollbackOnly.get())
82          {
83              return STATUS_MARKED_ROLLBACK;
84          }
85          if (started.get())
86          {
87              return STATUS_ACTIVE;
88          }
89          return STATUS_NO_TRANSACTION;
90      }
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see org.mule.umo.UMOTransaction#getResource(java.lang.Object)
96       */
97      public Object getResource(Object key)
98      {
99          return key != null && this.key == key ? this.resource : null;
100     }
101 
102     /*
103      * (non-Javadoc)
104      * 
105      * @see org.mule.umo.UMOTransaction#hasResource(java.lang.Object)
106      */
107     public boolean hasResource(Object key)
108     {
109         return key != null && this.key == key;
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
116      *      java.lang.Object)
117      */
118     public void bindResource(Object key, Object resource) throws TransactionException
119     {
120         if (key == null)
121         {
122             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindToNullKey());
123         }
124         if (resource == null)
125         {
126             throw new IllegalTransactionStateException(CoreMessages.transactionCannotBindNullResource());
127         }
128         if (this.key != null)
129         {
130             throw new IllegalTransactionStateException(CoreMessages.transactionSingleResourceOnly());
131         }
132         this.key = key;
133         this.resource = resource;
134     }
135 
136     /*
137      * (non-Javadoc)
138      * 
139      * @see org.mule.umo.UMOTransaction#setRollbackOnly()
140      */
141     public void setRollbackOnly()
142     {
143         rollbackOnly.set(true);
144     }
145 
146     public Object getId()
147     {
148         return key;
149     }
150 }