1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.geronimo.connector.outbound;
19
20 import javax.resource.ResourceException;
21 import javax.resource.spi.LocalTransaction;
22 import javax.transaction.xa.XAException;
23 import javax.transaction.xa.XAResource;
24 import javax.transaction.xa.Xid;
25
26 import org.apache.geronimo.transaction.manager.NamedXAResource;
27
28
29
30
31
32
33
34
35
36 public class LocalXAResource implements NamedXAResource {
37
38
39 final LocalTransaction localTransaction;
40 private final String name;
41 private Xid xid;
42 private int transactionTimeout;
43
44 public LocalXAResource(LocalTransaction localTransaction, String name) {
45 this.localTransaction = localTransaction;
46 this.name = name;
47 }
48
49
50
51 public void commit(Xid xid, boolean flag) throws XAException {
52 if (this.xid == null || !this.xid.equals(xid)) {
53 throw new XAException("Invalid Xid");
54 }
55 try {
56 localTransaction.commit();
57 } catch (ResourceException e) {
58 throw (XAException)new XAException().initCause(e);
59 } finally {
60 this.xid = null;
61 }
62
63 }
64
65 public void forget(Xid xid) throws XAException {
66 this.xid = null;
67 }
68
69 public int getTransactionTimeout() throws XAException {
70 return transactionTimeout;
71 }
72
73 public boolean isSameRM(XAResource xares) throws XAException {
74 return this == xares;
75 }
76
77 public Xid[] recover(int n) throws XAException {
78 return new Xid[0];
79 }
80
81 public void rollback(Xid xid) throws XAException {
82 if (this.xid == null || !this.xid.equals(xid)) {
83 throw new XAException("Invalid Xid");
84 }
85 try {
86 localTransaction.rollback();
87 } catch (ResourceException e) {
88 throw (XAException)new XAException().initCause(e);
89 } finally {
90 this.xid = null;
91 }
92 }
93
94 public boolean setTransactionTimeout(int txTimeout) throws XAException {
95 this.transactionTimeout = txTimeout;
96 return true;
97 }
98
99 public void start(Xid xid, int flag) throws XAException {
100 if (flag == XAResource.TMNOFLAGS) {
101
102 if (this.xid != null) {
103 throw new XAException("already enlisted");
104 }
105 this.xid = xid;
106 try {
107 localTransaction.begin();
108 } catch (ResourceException e) {
109 throw (XAException) new XAException("could not start local tx").initCause(e);
110 }
111 } else if (flag == XAResource.TMRESUME) {
112 if (xid != this.xid) {
113 throw new XAException("attempting to resume in different transaction");
114 }
115 } else {
116 throw new XAException("unknown state");
117 }
118 }
119
120 public void end(Xid xid, int flag) throws XAException {
121 if (xid != this.xid) {
122 throw new XAException("Invalid Xid");
123 }
124
125 }
126
127 public int prepare(Xid xid) throws XAException {
128
129 return XAResource.XA_OK;
130 }
131
132 public String getName() {
133 return name;
134 }
135 }