KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > resource > Resource


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.tm.resource;
23
24 import java.util.HashMap JavaDoc;
25
26 import javax.transaction.xa.XAException JavaDoc;
27 import javax.transaction.xa.XAResource JavaDoc;
28 import javax.transaction.xa.Xid JavaDoc;
29
30 /**
31  * A resource
32  *
33  * @author Adrian@jboss.org
34  * @version $Revision: 41193 $
35  */

36 public class Resource
37    implements XAResource JavaDoc
38 {
39    public static final int ERROR = -1;
40    public static final int INACTIVE = 0;
41    public static final int ACTIVE = 1;
42    public static final int SUSPENDED = 2;
43    public static final int PREPARED = 3;
44    public static final int COMMITTED = 4;
45    public static final int ROLLEDBACK = 5;
46    public static final int FORGOT = 6;
47    public static final int ENDED = 7;
48
49    Integer JavaDoc id;
50
51    boolean sameRM = false;
52
53    HashMap JavaDoc xids = new HashMap JavaDoc();
54    Xid JavaDoc last = null;
55
56    public Resource(Integer JavaDoc id)
57    {
58       this.id = id;
59    }
60
61    public int getStatus()
62       throws XAException JavaDoc
63    {
64       return getState(last, true, false).resState;
65    }
66
67    public void setStatus(int status)
68       throws XAException JavaDoc
69    {
70       getState(last).status = status;
71    }
72
73    public void newResourceManager()
74    {
75       sameRM = false;
76    }
77
78    public int prepare(Xid JavaDoc xid)
79       throws XAException JavaDoc
80    {
81       State state = getState(xid);
82       if (state.resState != ACTIVE && state.resState != SUSPENDED && state.resState != ENDED)
83       {
84          state.resState = ERROR;
85          throw new XAException JavaDoc(XAException.XAER_PROTO);
86       }
87       state.resState = PREPARED;
88       return state.status;
89    }
90
91    public void commit(Xid JavaDoc xid, boolean onePhase)
92       throws XAException JavaDoc
93    {
94       State state = getState(xid);
95       if (onePhase == false && state.resState != PREPARED)
96       {
97          state.resState = ERROR;
98          throw new XAException JavaDoc(XAException.XAER_PROTO);
99       }
100       if (onePhase && state.resState != ACTIVE && state.resState != SUSPENDED && state.resState != ENDED)
101       {
102          state.resState = ERROR;
103          throw new XAException JavaDoc(XAException.XAER_PROTO);
104       }
105       state.resState = COMMITTED;
106       state.removed = true;
107    }
108
109    public void rollback(Xid JavaDoc xid)
110       throws XAException JavaDoc
111    {
112       State state = getState(xid);
113       if (state.resState == INACTIVE)
114       {
115          state.resState = ERROR;
116          throw new XAException JavaDoc(XAException.XAER_PROTO);
117       }
118       state.resState = ROLLEDBACK;
119       state.removed = true;
120    }
121
122    public void forget(Xid JavaDoc xid)
123       throws XAException JavaDoc
124    {
125       State state = getState(xid, false, false);
126       state.resState = FORGOT;
127       state.removed = true;
128    }
129
130    public void start(Xid JavaDoc xid, int flags)
131       throws XAException JavaDoc
132    {
133       if (xid == null)
134          throw new XAException JavaDoc(XAException.XAER_NOTA);
135       if (flags == TMJOIN)
136       {
137          getState(xid); // Just checks the state
138
return;
139       }
140       if (flags == TMRESUME)
141       {
142          State state = getState(xid);
143          if (state.resState != SUSPENDED)
144          {
145             state.resState = ERROR;
146             throw new XAException JavaDoc("Xid not suspended " + xid);
147          }
148          return;
149       }
150       if (xids.containsKey(xid))
151       {
152          throw new XAException JavaDoc(XAException.XAER_DUPID);
153       }
154       xids.put(xid, new State());
155       last = xid;
156       
157    }
158
159    public void end(Xid JavaDoc xid, int flags)
160       throws XAException JavaDoc
161    {
162       State state = getState(xid);
163       if (flags == TMSUSPEND)
164       {
165          state.resState = SUSPENDED;
166          return;
167       }
168       state.resState = ENDED;
169    }
170
171    public Xid JavaDoc[] recover(int flag)
172    {
173       return new Xid JavaDoc[0];
174    }
175
176    public boolean isSameRM(XAResource JavaDoc res)
177    {
178       return sameRM;
179    }
180
181    public int getTransactionTimeout()
182    {
183       return 0;
184    }
185
186    public boolean setTransactionTimeout(int timeout)
187    {
188       return false;
189    }
190
191    public State getState(Xid JavaDoc xid)
192       throws XAException JavaDoc
193    {
194       return getState(xid, false);
195    }
196
197    public State getState(Xid JavaDoc xid, boolean includeRemoved)
198       throws XAException JavaDoc
199    {
200       return getState(xid, includeRemoved, true);
201    }
202
203    public State getState(Xid JavaDoc xid, boolean includeRemoved, boolean checkStatus)
204       throws XAException JavaDoc
205    {
206       if (xid == null)
207          throw new XAException JavaDoc(XAException.XAER_NOTA);
208       State state = (State) xids.get(xid);
209       if (state.resState == ERROR)
210          throw new XAException JavaDoc(XAException.XAER_PROTO);
211       if (state == null || (state.removed == true && includeRemoved == false))
212          throw new XAException JavaDoc(XAException.XAER_PROTO);
213       if (checkStatus && (state.status >= 5 || state.status < 0))
214          throw new XAException JavaDoc(state.status);
215       return state;
216    }
217
218    public class State
219    {
220       int status = XAResource.XA_OK;
221       int resState = ACTIVE;
222       boolean removed = false;
223    }
224 }
225
Popular Tags