01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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 javax.transaction.xa.XAException;
25: import javax.transaction.xa.XAResource;
26: import javax.transaction.xa.Xid;
27:
28: import org.jboss.tm.LastResource;
29:
30: /**
31: * A LocalResource.
32: *
33: * @author <a href="adrian@jboss.com">Adrian Brock</a>
34: * @version $Revision: 57211 $
35: */
36: public class LocalResource extends Resource implements LastResource {
37: /** Whether to fail the commit */
38: private boolean failLocal = false;
39:
40: /**
41: * Create a new LocalResource.
42: *
43: * @param id the id
44: */
45: public LocalResource(Integer id) {
46: super (id);
47: }
48:
49: public void failLocal() {
50: failLocal = true;
51: }
52:
53: public int prepare(Xid xid) throws XAException {
54: XAException e = new XAException(
55: "Prepare called on local resource");
56: e.errorCode = XAException.XAER_PROTO;
57: throw e;
58: }
59:
60: public void commit(Xid xid, boolean onePhase) throws XAException {
61: State state = getState(xid);
62: if (state.resState != ACTIVE && state.resState != ENDED) {
63: state.resState = ERROR;
64: throw new XAException(XAException.XAER_PROTO);
65: }
66: if (failLocal) {
67: state.resState = ROLLEDBACK;
68: state.removed = true;
69: throw new XAException(XAException.XA_RBROLLBACK);
70: } else {
71: state.resState = COMMITTED;
72: state.removed = true;
73: }
74: }
75:
76: public boolean isSameRM(XAResource res) {
77: return res == this;
78: }
79: }
|