KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > mock > MockXAResource


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.connector.mock;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22 import javax.transaction.xa.XAException JavaDoc;
23 import javax.transaction.xa.XAResource JavaDoc;
24 import javax.transaction.xa.Xid JavaDoc;
25
26 /**
27  *
28  *
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  *
31  * */

32 public class MockXAResource implements XAResource JavaDoc {
33
34     private final MockManagedConnection mockManagedConnection;
35     private int prepareResult = XAResource.XA_OK;
36     private Xid JavaDoc currentXid;
37     private int transactionTimeoutSeconds;
38     private final Set JavaDoc knownXids = new HashSet JavaDoc();
39     private final Set JavaDoc successfulXids = new HashSet JavaDoc();
40     private Xid JavaDoc prepared;
41     private Xid JavaDoc committed;
42     private Xid JavaDoc rolledback;
43
44     public MockXAResource(MockManagedConnection mockManagedConnection) {
45         this.mockManagedConnection = mockManagedConnection;
46     }
47
48     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
49         assert xid != null;
50         assert onePhase || prepared == xid;
51         committed = xid;
52     }
53
54     //TODO TMFAIL? TMENDRSCAN?
55
public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
56         assert xid != null;
57         assert knownXids.contains(xid);
58         assert flags == XAResource.TMSUSPEND || flags == XAResource.TMSUCCESS;
59         if (flags == XAResource.TMSUSPEND) {
60             assert currentXid == xid;
61             currentXid = null;
62         }
63         if (flags == XAResource.TMSUCCESS) {
64             successfulXids.add(xid);
65             if (xid.equals(currentXid)) {
66                 currentXid = null;
67             }
68         }
69     }
70
71     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
72         //todo
73
}
74
75     public int getTransactionTimeout() throws XAException JavaDoc {
76         return transactionTimeoutSeconds;
77     }
78
79     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
80         if (!(xaResource instanceof MockXAResource)) {
81             return false;
82         }
83         MockXAResource other = (MockXAResource) xaResource;
84         return other.mockManagedConnection.getManagedConnectionFactory() == mockManagedConnection.getManagedConnectionFactory();
85     }
86
87     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
88         assert xid != null;
89         prepared = xid;
90         return prepareResult;
91     }
92
93     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
94         //todo
95
return new Xid JavaDoc[0];
96     }
97
98     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
99         assert xid != null;
100         rolledback = xid;
101     }
102
103     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
104         transactionTimeoutSeconds = seconds;
105         return true;
106     }
107
108     //TODO TMSTARTRSCAN?
109
public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
110         assert currentXid == null :"Expected no xid when start called";
111         assert xid != null: "Expected xid supplied to start";
112         assert flags == XAResource.TMNOFLAGS || flags == XAResource.TMJOIN || flags == XAResource.TMRESUME;
113         if (flags == XAResource.TMNOFLAGS || flags == XAResource.TMJOIN) {
114             assert !knownXids.contains(xid);
115             knownXids.add(xid);
116         }
117         if (flags == XAResource.TMRESUME) {
118             assert knownXids.contains(xid);
119         }
120         currentXid = xid;
121     }
122
123     public void setPrepareResult(int prepareResult) {
124         this.prepareResult = prepareResult;
125     }
126
127     public Xid JavaDoc getCurrentXid() {
128         return currentXid;
129     }
130
131     public Set JavaDoc getKnownXids() {
132         return knownXids;
133     }
134
135     public Set JavaDoc getSuccessfulXids() {
136         return successfulXids;
137     }
138
139     public Xid JavaDoc getPrepared() {
140         return prepared;
141     }
142
143     public Xid JavaDoc getCommitted() {
144         return committed;
145     }
146
147     public Xid JavaDoc getRolledback() {
148         return rolledback;
149     }
150
151     public void clear() {
152         currentXid = null;
153         prepared = null;
154         rolledback = null;
155         committed = null;
156         knownXids.clear();
157         successfulXids.clear();
158         prepareResult = XAResource.XA_OK;
159     }
160 }
161
Popular Tags