1 22 package org.jboss.test.tm.resource; 23 24 import java.util.HashMap ; 25 26 import javax.transaction.xa.XAException ; 27 import javax.transaction.xa.XAResource ; 28 import javax.transaction.xa.Xid ; 29 30 36 public class Resource 37 implements XAResource 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 id; 50 51 boolean sameRM = false; 52 53 HashMap xids = new HashMap (); 54 Xid last = null; 55 56 public Resource(Integer id) 57 { 58 this.id = id; 59 } 60 61 public int getStatus() 62 throws XAException 63 { 64 return getState(last, true, false).resState; 65 } 66 67 public void setStatus(int status) 68 throws XAException 69 { 70 getState(last).status = status; 71 } 72 73 public void newResourceManager() 74 { 75 sameRM = false; 76 } 77 78 public int prepare(Xid xid) 79 throws XAException 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 (XAException.XAER_PROTO); 86 } 87 state.resState = PREPARED; 88 return state.status; 89 } 90 91 public void commit(Xid xid, boolean onePhase) 92 throws XAException 93 { 94 State state = getState(xid); 95 if (onePhase == false && state.resState != PREPARED) 96 { 97 state.resState = ERROR; 98 throw new XAException (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 (XAException.XAER_PROTO); 104 } 105 state.resState = COMMITTED; 106 state.removed = true; 107 } 108 109 public void rollback(Xid xid) 110 throws XAException 111 { 112 State state = getState(xid); 113 if (state.resState == INACTIVE) 114 { 115 state.resState = ERROR; 116 throw new XAException (XAException.XAER_PROTO); 117 } 118 state.resState = ROLLEDBACK; 119 state.removed = true; 120 } 121 122 public void forget(Xid xid) 123 throws XAException 124 { 125 State state = getState(xid, false, false); 126 state.resState = FORGOT; 127 state.removed = true; 128 } 129 130 public void start(Xid xid, int flags) 131 throws XAException 132 { 133 if (xid == null) 134 throw new XAException (XAException.XAER_NOTA); 135 if (flags == TMJOIN) 136 { 137 getState(xid); 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 ("Xid not suspended " + xid); 147 } 148 return; 149 } 150 if (xids.containsKey(xid)) 151 { 152 throw new XAException (XAException.XAER_DUPID); 153 } 154 xids.put(xid, new State()); 155 last = xid; 156 157 } 158 159 public void end(Xid xid, int flags) 160 throws XAException 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 [] recover(int flag) 172 { 173 return new Xid [0]; 174 } 175 176 public boolean isSameRM(XAResource 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 xid) 192 throws XAException 193 { 194 return getState(xid, false); 195 } 196 197 public State getState(Xid xid, boolean includeRemoved) 198 throws XAException 199 { 200 return getState(xid, includeRemoved, true); 201 } 202 203 public State getState(Xid xid, boolean includeRemoved, boolean checkStatus) 204 throws XAException 205 { 206 if (xid == null) 207 throw new XAException (XAException.XAER_NOTA); 208 State state = (State) xids.get(xid); 209 if (state.resState == ERROR) 210 throw new XAException (XAException.XAER_PROTO); 211 if (state == null || (state.removed == true && includeRemoved == false)) 212 throw new XAException (XAException.XAER_PROTO); 213 if (checkStatus && (state.status >= 5 || state.status < 0)) 214 throw new XAException (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 |