KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > gs > JiniTransaction


1 /*
2  * $Id: JiniTransaction.java 3807 2006-11-06 13:13:36Z holger $
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.providers.gs;
12
13 import java.rmi.RemoteException JavaDoc;
14
15 import net.jini.core.transaction.Transaction;
16 import net.jini.core.transaction.TransactionFactory;
17 import net.jini.core.transaction.server.TransactionManager;
18
19 import org.mule.config.i18n.Message;
20 import org.mule.config.i18n.Messages;
21 import org.mule.providers.gs.space.GSSpace;
22 import org.mule.transaction.AbstractSingleResourceTransaction;
23 import org.mule.transaction.IllegalTransactionStateException;
24 import org.mule.transaction.TransactionRollbackException;
25 import org.mule.umo.TransactionException;
26
27 import com.j_spaces.core.IJSpace;
28 import com.j_spaces.core.client.LocalTransactionManager;
29
30 /**
31  * Provides a Jini Transaction wrapper so that Jini transactions can be used in Mule.
32  * As Jini does not use the standard JTA Transaction manager, the Jini
33  * TransactionManager Must be set on the JiniTransactionFactory before any
34  * transactions are begun.
35  *
36  * @see TransactionManager
37  * @see JiniTransactionFactory
38  */

39 public class JiniTransaction extends AbstractSingleResourceTransaction
40 {
41
42     protected TransactionManager txManager;
43     protected long timeout;
44     protected boolean unbound = true;
45
46     public JiniTransaction(long timeout)
47     {
48         this.timeout = timeout;
49     }
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
55      * java.lang.Object)
56      */

57     public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
58     {
59
60         // We can only start the transaction when its bound as we need the Space to
61
// obtain the TransactionManager
62
// Todo find a clean way of passing the Trsnasaction manager to the
63
// TransactionFactory
64
try
65         {
66             txManager = LocalTransactionManager.getInstance((IJSpace)((GSSpace)resource).getJavaSpace());
67         }
68         catch (RemoteException JavaDoc e)
69         {
70             throw new TransactionException(e);
71         }
72         try
73         {
74             Transaction.Created tCreated = TransactionFactory.create(txManager, timeout);
75             Transaction transaction = tCreated.transaction;
76             super.bindResource(resource, transaction);
77             unbound = false;
78         }
79         catch (Exception JavaDoc e)
80         {
81             throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_START_X_TRANSACTION,
82                 "Jini"), e);
83         }
84
85     }
86
87     /**
88      * Begin the transaction.
89      *
90      * @throws org.mule.umo.TransactionException
91      */

92     public void doBegin() throws TransactionException
93     {
94         // Do nothing here, the transacted cannot be created until it is bound
95
unbound = true;
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.mule.umo.UMOTransaction#commit()
102      */

103     protected void doCommit() throws TransactionException
104     {
105         if (unbound)
106         {
107             return;
108         }
109         try
110         {
111             ((Transaction)resource).commit();
112         }
113         catch (Exception JavaDoc e)
114         {
115             throw new IllegalTransactionStateException(new Message(Messages.TX_COMMIT_FAILED), e);
116         }
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see org.mule.umo.UMOTransaction#rollback()
123      */

124     protected void doRollback() throws TransactionException
125     {
126         try
127         {
128             if (unbound)
129             {
130                 return;
131             }
132             ((Transaction)resource).abort();
133         }
134         catch (Exception JavaDoc e)
135         {
136             throw new TransactionRollbackException(e);
137         }
138     }
139 }
140
Popular Tags