KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > transaction > JtaTransaction


1 /*
2  * $Id: JtaTransaction.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.shark.transaction;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.entity.transaction.GenericTransactionException;
32 import org.ofbiz.entity.transaction.TransactionUtil;
33
34 import org.enhydra.shark.api.ApplicationMappingTransaction;
35 import org.enhydra.shark.api.ParticipantMappingTransaction;
36 import org.enhydra.shark.api.TransactionException;
37 import org.enhydra.shark.api.UserTransaction;
38 import org.enhydra.shark.api.RepositoryTransaction;
39 import org.enhydra.shark.api.RootException;
40 import org.enhydra.shark.api.internal.transaction.SharkInternalTransaction;
41 import org.enhydra.shark.api.internal.working.WfProcessInternal;
42 import org.enhydra.shark.api.internal.working.WfResourceInternal;
43
44 /**
45  * Shark JTA Transaction Implementation
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  */

51 public class JtaTransaction implements SharkInternalTransaction, UserTransaction,
52         ApplicationMappingTransaction, ParticipantMappingTransaction, RepositoryTransaction {
53
54     public static final String JavaDoc module = JtaTransaction.class.getName();
55     public static final int transactionTimeout = 120;
56
57     protected Map JavaDoc resourceMap = new HashMap JavaDoc();
58     protected Map JavaDoc processMap = new HashMap JavaDoc();
59
60     protected boolean beganTransaction = false;
61     protected boolean active = false;
62     protected boolean enabled = true;
63
64     public JtaTransaction() {
65         if (enabled) {
66             try {
67                 this.beganTransaction = TransactionUtil.begin(transactionTimeout);
68                 active = true;
69             } catch (GenericTransactionException e) {
70                 Debug.logError(e, module);
71             }
72         }
73     }
74
75     public void commit() throws TransactionException {
76         if (active) {
77             try {
78                 TransactionUtil.commit(beganTransaction);
79             } catch (GenericTransactionException e) {
80                 Debug.logError(e, module);
81                 throw new TransactionException(e);
82             }
83             active = false;
84         } else {
85             Debug.logError(new Exception JavaDoc(), "No active transaction; unable to commit", module);
86             //throw new TransactionException("No active transaction");
87
}
88     }
89
90     public void rollback() throws TransactionException {
91         if (active) {
92             try {
93                 TransactionUtil.rollback(beganTransaction, "Transaction rollback from Shark", null);
94             } catch (GenericTransactionException e) {
95                 Debug.logError(e, module);
96                 throw new TransactionException(e);
97             }
98             active = false;
99         } else {
100             Debug.logError(new Exception JavaDoc(), "No active transaction; unable to rollback", module);
101             //throw new TransactionException("No active transaction");
102
}
103     }
104
105     public void release() throws TransactionException {
106         if (active) {
107             this.commit();
108         }
109     }
110
111     public void addToTransaction(String JavaDoc procId, WfProcessInternal proc) throws RootException {
112         this.processMap.put(procId, proc);
113     }
114
115     public void addToTransaction(String JavaDoc resId, WfResourceInternal res) throws RootException {
116         this.resourceMap.put(resId, res);
117     }
118
119     public void removeProcess(String JavaDoc procId) throws RootException {
120         this.processMap.remove(procId);
121     }
122
123     public void removeResource(String JavaDoc resId) throws RootException {
124         this.resourceMap.remove(resId);
125     }
126
127     public WfProcessInternal getProcess(String JavaDoc procId) throws RootException {
128         return (WfProcessInternal) this.processMap.get(procId);
129     }
130
131     public WfResourceInternal getResource(String JavaDoc resId) throws RootException {
132         return (WfResourceInternal) this.resourceMap.get(resId);
133     }
134 }
135
Popular Tags