KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > transaction > GenericXaResource


1 /*
2  * $Id: GenericXaResource.java 6778 2006-02-20 05:13:55Z jonesde $
3  *
4  * Copyright 2001-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18 package org.ofbiz.entity.transaction;
19
20 import org.ofbiz.base.util.Debug;
21
22 import javax.transaction.xa.XAResource JavaDoc;
23 import javax.transaction.xa.Xid JavaDoc;
24 import javax.transaction.xa.XAException JavaDoc;
25 import javax.transaction.*;
26
27 /**
28  * GenericXaResource - Abstract XA Resource implementation supporting a single transaction
29  *
30  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
31  * @version $Rev: 6778 $
32  * @since 3.0
33  */

34 public abstract class GenericXaResource implements XAResource JavaDoc {
35
36     public static final String JavaDoc module = GenericXaResource.class.getName();
37
38     protected boolean active = false;
39     protected int timeout = 0;
40     protected Xid JavaDoc xid = null;
41
42     /**
43      * Enlists this resource in the current transaction
44      * @throws XAException
45      */

46     public void enlist() throws XAException JavaDoc {
47         TransactionManager tm = TransactionFactory.getTransactionManager();
48         try {
49             if (tm != null && tm.getStatus() == Status.STATUS_ACTIVE) {
50                 Transaction tx = tm.getTransaction();
51                 if (tx != null) {
52                     tx.enlistResource(this);
53                 } else {
54                     throw new XAException JavaDoc(XAException.XAER_NOTA);
55                 }
56             } else {
57                 throw new XAException JavaDoc("No transaction manager or invalid status");
58             }
59         } catch (SystemException e) {
60             throw new XAException JavaDoc("Unable to get transaction status");
61         } catch (RollbackException e) {
62             throw new XAException JavaDoc("Unable to enlist resource with transaction");
63         }
64     }
65
66     /**
67      * @see javax.transaction.xa.XAResource#start(javax.transaction.xa.Xid xid, int flag)
68      */

69     public void start(Xid JavaDoc xid, int flag) throws XAException JavaDoc {
70         if (this.active) {
71             if (this.xid != null && this.xid.equals(xid)) {
72                 throw new XAException JavaDoc(XAException.XAER_DUPID);
73             } else {
74                 throw new XAException JavaDoc(XAException.XAER_PROTO);
75             }
76         }
77         if (this.xid != null && !this.xid.equals(xid)) {
78             throw new XAException JavaDoc(XAException.XAER_NOTA);
79         }
80
81         this.xid = xid;
82         this.active = true;
83     }
84
85     /**
86      * @see javax.transaction.xa.XAResource#end(javax.transaction.xa.Xid xid, int flag)
87      */

88     public void end(Xid JavaDoc xid, int flag) throws XAException JavaDoc {
89         if (!this.active) {
90             throw new XAException JavaDoc(XAException.XAER_PROTO);
91         }
92
93         if (this.xid == null || !this.xid.equals(xid)) {
94             throw new XAException JavaDoc(XAException.XAER_NOTA);
95         }
96         this.active = false;
97     }
98
99     /**
100      * @see javax.transaction.xa.XAResource#forget(javax.transaction.xa.Xid xid)
101      */

102     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
103         if (this.xid == null || !this.xid.equals(xid)) {
104             throw new XAException JavaDoc(XAException.XAER_NOTA);
105         }
106         this.xid = null;
107         if (active) {
108             // non-fatal
109
Debug.logWarning("forget() called without end()", module);
110         }
111     }
112
113     /**
114      * @see javax.transaction.xa.XAResource#prepare(javax.transaction.xa.Xid xid)
115      */

116     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
117         if (this.xid == null || !this.xid.equals(xid)) {
118             throw new XAException JavaDoc(XAException.XAER_NOTA);
119         }
120         return XA_OK;
121     }
122
123     /**
124      * @see javax.transaction.xa.XAResource#recover(int flag)
125      */

126     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
127         if (this.xid == null) {
128             return new Xid JavaDoc[0];
129         } else {
130             return new Xid JavaDoc[] {this.xid};
131         }
132     }
133
134     /**
135      * @see javax.transaction.xa.XAResource#isSameRM(javax.transaction.xa.XAResource xaResource)
136      */

137     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
138         return xaResource == this;
139     }
140
141     /**
142      * @see javax.transaction.xa.XAResource#getTransactionTimeout()
143      */

144     public int getTransactionTimeout() throws XAException JavaDoc {
145         return this.timeout;
146     }
147
148     /**
149      * @see javax.transaction.xa.XAResource#setTransactionTimeout(int seconds)
150      * Note: the valus is saved but in the current implementation this is not used.
151      */

152     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
153         this.timeout = seconds;
154         return true;
155     }
156
157     public Xid JavaDoc getXid() {
158         return this.xid;
159     }
160
161     /**
162      * @see javax.transaction.xa.XAResource#commit(javax.transaction.xa.Xid xid, boolean onePhase)
163      */

164     public abstract void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc;
165
166     /**
167      * @see javax.transaction.xa.XAResource#rollback(javax.transaction.xa.Xid xid)
168      */

169     public abstract void rollback(Xid JavaDoc xid) throws XAException JavaDoc;
170 }
171
Popular Tags