KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XaTransaction.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 java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.transaction.HeuristicRollbackException JavaDoc;
17 import javax.transaction.RollbackException JavaDoc;
18 import javax.transaction.SystemException JavaDoc;
19 import javax.transaction.Transaction JavaDoc;
20 import javax.transaction.TransactionManager JavaDoc;
21
22 import org.mule.MuleManager;
23 import org.mule.config.i18n.Message;
24 import org.mule.config.i18n.Messages;
25 import org.mule.umo.TransactionException;
26
27 /**
28  * <code>XaTransaction</code> represents an XA transaction in Mule.
29  */

30 public class XaTransaction extends AbstractTransaction
31 {
32     /**
33      * The inner JTA transaction
34      */

35     private Transaction JavaDoc transaction = null;
36
37     /**
38      * Map of enlisted resources
39      */

40     private Map JavaDoc resources = null;
41
42     /**
43      * Default constructor
44      */

45     public XaTransaction()
46     {
47         super();
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see org.mule.umo.UMOTransaction#begin()
54      */

55     protected void doBegin() throws TransactionException
56     {
57         TransactionManager JavaDoc txManager = MuleManager.getInstance().getTransactionManager();
58
59         if (txManager == null)
60         {
61             throw new IllegalStateException JavaDoc(new Message(Messages.X_NOT_REGISTERED_WITH_MANAGER,
62                 "Transaction Manager").getMessage());
63         }
64
65         try
66         {
67             txManager.begin();
68             synchronized (this)
69             {
70                 transaction = txManager.getTransaction();
71             }
72         }
73         catch (Exception JavaDoc e)
74         {
75             throw new TransactionException(new Message(Messages.TX_CANT_START_X_TRANSACTION, "XA"), e);
76         }
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.mule.umo.UMOTransaction#commit()
83      */

84     protected void doCommit() throws TransactionException
85     {
86         try
87         {
88             synchronized (this)
89             {
90                 transaction.commit();
91             }
92         }
93         catch (RollbackException JavaDoc e)
94         {
95             throw new TransactionRollbackException(new Message(Messages.TX_MARKED_FOR_ROLLBACK), e);
96         }
97         catch (HeuristicRollbackException JavaDoc e)
98         {
99             throw new TransactionRollbackException(new Message(Messages.TX_MARKED_FOR_ROLLBACK), e);
100         }
101         catch (Exception JavaDoc e)
102         {
103             throw new IllegalTransactionStateException(new Message(Messages.TX_COMMIT_FAILED), e);
104         }
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.mule.umo.UMOTransaction#rollback()
111      */

112     protected void doRollback() throws TransactionRollbackException
113     {
114         try
115         {
116             synchronized (this)
117             {
118                 transaction.rollback();
119             }
120         }
121         catch (SystemException JavaDoc e)
122         {
123             throw new TransactionRollbackException(e);
124         }
125
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.mule.umo.UMOTransaction#getStatus()
132      */

133     public int getStatus() throws TransactionStatusException
134     {
135         synchronized (this)
136         {
137             if (transaction == null)
138             {
139                 return STATUS_NO_TRANSACTION;
140             }
141
142             try
143             {
144                 return transaction.getStatus();
145             }
146             catch (SystemException JavaDoc e)
147             {
148                 throw new TransactionStatusException(e);
149             }
150         }
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.mule.umo.UMOTransaction#setRollbackOnly()
157      */

158     public void setRollbackOnly()
159     {
160         try
161         {
162             synchronized (this)
163             {
164                 transaction.setRollbackOnly();
165             }
166         }
167         catch (SystemException JavaDoc e)
168         {
169             throw new IllegalStateException JavaDoc("Failed to set transaction to rollback only: " + e.getMessage());
170         }
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.mule.umo.UMOTransaction#getResource(java.lang.Object)
177      */

178     public Object JavaDoc getResource(Object JavaDoc key)
179     {
180         synchronized (this)
181         {
182             return resources == null ? null : resources.get(key);
183         }
184     }
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see org.mule.umo.UMOTransaction#hasResource(java.lang.Object)
190      */

191     public boolean hasResource(Object JavaDoc key)
192     {
193         synchronized (this)
194         {
195             return resources != null && resources.containsKey(key);
196         }
197     }
198
199     /*
200      * (non-Javadoc)
201      *
202      * @see org.mule.umo.UMOTransaction#enlistResource(java.lang.Object,
203      * java.lang.Object)
204      */

205     public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
206     {
207         synchronized (this)
208         {
209             if (resources == null)
210             {
211                 resources = new HashMap JavaDoc();
212             }
213
214             if (resources.containsKey(key))
215             {
216                 throw new IllegalTransactionStateException(new Message(
217                     Messages.TX_RESOURCE_ALREADY_LISTED_FOR_KEY_X, key));
218             }
219
220             resources.put(key, resource);
221         }
222     }
223 }
224
Popular Tags