KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > manager > MockResource


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.manager;
19
20 import java.util.Set JavaDoc;
21 import java.util.HashSet JavaDoc;
22
23 import javax.transaction.xa.XAException JavaDoc;
24 import javax.transaction.xa.XAResource JavaDoc;
25 import javax.transaction.xa.Xid JavaDoc;
26
27 /**
28  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
29  */

30 public class MockResource implements NamedXAResource {
31     private String JavaDoc xaResourceName = "mockResource";
32     private Xid JavaDoc currentXid;
33     private MockResourceManager manager;
34     private int timeout = 0;
35     private boolean prepared;
36     private boolean committed;
37     private boolean rolledback;
38     private Set JavaDoc preparedXids = new HashSet JavaDoc();
39     private Set JavaDoc knownXids = new HashSet JavaDoc();
40     private Set JavaDoc finishedXids = new HashSet JavaDoc();//end was called with TMSUCCESS or TMFAIL
41

42     public MockResource(MockResourceManager manager, String JavaDoc xaResourceName) {
43         this.manager = manager;
44         this.xaResourceName = xaResourceName;
45     }
46
47     public int getTransactionTimeout() throws XAException JavaDoc {
48         return timeout;
49     }
50
51     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
52         return false;
53     }
54
55     public Xid JavaDoc getCurrentXid() {
56         return currentXid;
57     }
58
59     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
60         if (this.currentXid != null) {
61             throw new XAException JavaDoc(XAException.XAER_PROTO);
62         }
63         if (flags == XAResource.TMRESUME && !knownXids.contains(xid)) {
64             throw new XAException JavaDoc(XAException.XAER_PROTO);
65         }
66         if (finishedXids.contains(xid)) {
67             throw new XAException JavaDoc(XAException.XAER_PROTO);
68         }
69         if ((flags & XAResource.TMJOIN) != 0) {
70             manager.join(xid, this);
71         } else {
72             manager.newTx(xid, this);
73         }
74         this.currentXid = xid;
75         if (!knownXids.contains(xid)) {
76             knownXids.add(xid);
77         }
78     }
79
80     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
81         if (!knownXids.contains(xid)) {
82             throw new XAException JavaDoc(XAException.XAER_PROTO);
83         }
84         if (flags == XAResource.TMSUSPEND) {
85             if (currentXid == null) {
86                 throw new XAException JavaDoc(XAException.XAER_PROTO);
87             } else if (this.currentXid != xid) {
88                 throw new XAException JavaDoc(XAException.XAER_PROTO);
89             }
90         } else if (flags == XAResource.TMFAIL || flags == XAResource.TMSUCCESS) {
91             if (finishedXids.contains(xid)) {
92                 throw new XAException JavaDoc(XAException.XAER_PROTO);
93             }
94             finishedXids.add(xid);
95         }
96         this.currentXid = null;
97     }
98
99     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
100         if (!finishedXids.contains(xid)) {
101             throw new XAException JavaDoc(XAException.XAER_PROTO);
102         }
103         prepared = true;
104         preparedXids.add(xid);
105         return XAResource.XA_OK;
106     }
107
108     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
109         if (!finishedXids.contains(xid)) {
110             throw new XAException JavaDoc(XAException.XAER_PROTO);
111         }
112         preparedXids.remove(xid);
113         committed = true;
114     }
115
116     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
117         if (!finishedXids.contains(xid)) {
118             throw new XAException JavaDoc(XAException.XAER_PROTO);
119         }
120         rolledback = true;
121         preparedXids.remove(xid);
122         manager.forget(xid, this);
123     }
124
125     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
126         if (xaResource instanceof MockResource) {
127             return manager == ((MockResource) xaResource).manager;
128         }
129         return false;
130     }
131
132     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
133         throw new UnsupportedOperationException JavaDoc();
134     }
135
136     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
137         return (Xid JavaDoc[]) preparedXids.toArray(new Xid JavaDoc[preparedXids.size()]);
138     }
139
140     public boolean isPrepared() {
141         return prepared;
142     }
143
144     public boolean isCommitted() {
145         return committed;
146     }
147
148     public boolean isRolledback() {
149         return rolledback;
150     }
151
152     public String JavaDoc getName() {
153         return xaResourceName;
154     }
155
156 }
157
Popular Tags