1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.ehcache.transaction.xa;
17
18 import net.sf.ehcache.transaction.manager.TransactionManagerLookup;
19
20 import java.util.Arrays;
21 import java.util.Properties;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 import javax.transaction.HeuristicMixedException;
25 import javax.transaction.HeuristicRollbackException;
26 import javax.transaction.InvalidTransactionException;
27 import javax.transaction.NotSupportedException;
28 import javax.transaction.RollbackException;
29 import javax.transaction.Synchronization;
30 import javax.transaction.SystemException;
31 import javax.transaction.Transaction;
32 import javax.transaction.TransactionManager;
33 import javax.transaction.xa.XAResource;
34 import javax.transaction.xa.Xid;
35
36 /***
37 * @author Ludovic Orban
38 */
39 public class DummyTransactionManagerLookup implements TransactionManagerLookup {
40
41 private static DummyTransactionManager transactionManager = new DummyTransactionManager();
42
43 public TransactionManager getTransactionManager() {
44 return transactionManager;
45 }
46
47 public synchronized void register(EhcacheXAResource resource) {
48
49 }
50
51 public synchronized void unregister(EhcacheXAResource resource) {
52 }
53
54 public void setProperties(Properties properties) {
55 }
56
57 static int arrayHashCode(byte[] uid) {
58 int hash = 0;
59 for (int i = uid.length - 1; i > 0; i--) {
60 hash <<= 1;
61
62 if (hash < 0) {
63 hash |= 1;
64 }
65
66 hash ^= uid[i];
67 }
68 return hash;
69 }
70
71 public static byte[] longToBytes(long aLong) {
72 byte[] array = new byte[8];
73
74 array[7] = (byte) (aLong & 0xff);
75 array[6] = (byte) ((aLong >> 8) & 0xff);
76 array[5] = (byte) ((aLong >> 16) & 0xff);
77 array[4] = (byte) ((aLong >> 24) & 0xff);
78 array[3] = (byte) ((aLong >> 32) & 0xff);
79 array[2] = (byte) ((aLong >> 40) & 0xff);
80 array[1] = (byte) ((aLong >> 48) & 0xff);
81 array[0] = (byte) ((aLong >> 56) & 0xff);
82
83 return array;
84 }
85
86 public static class DummyTransactionManager implements TransactionManager {
87
88 private final AtomicLong txIdGenerator = new AtomicLong();
89
90 private DummyTransaction testTransaction;
91
92 public DummyTransactionManager() {
93 }
94
95 public void begin() throws NotSupportedException, SystemException {
96 testTransaction = new DummyTransaction(txIdGenerator.incrementAndGet());
97 }
98
99 public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException {
100 }
101
102 public int getStatus() throws SystemException {
103 return 0;
104 }
105
106 public Transaction getTransaction() throws SystemException {
107 return testTransaction;
108 }
109
110 public void resume(Transaction transaction) throws IllegalStateException, InvalidTransactionException, SystemException {
111 testTransaction = (DummyTransaction) transaction;
112 }
113
114 public void rollback() throws IllegalStateException, SecurityException, SystemException {
115 }
116
117 public void setRollbackOnly() throws IllegalStateException, SystemException {
118 }
119
120 public void setTransactionTimeout(int i) throws SystemException {
121 }
122
123 public Transaction suspend() throws SystemException {
124 DummyTransaction suspendedTx = testTransaction;
125 testTransaction = null;
126 return suspendedTx;
127 }
128 }
129
130 public static class DummyTransaction implements Transaction {
131
132 private long id;
133
134 public DummyTransaction(long id) {
135 this.id = id;
136 }
137
138 @Override
139 public boolean equals(Object o) {
140 if (o instanceof DummyTransaction) {
141 DummyTransaction otherTx = (DummyTransaction) o;
142 return otherTx.id == id;
143 }
144 return false;
145 }
146
147 @Override
148 public int hashCode() {
149 return (int) id;
150 }
151
152 public void commit() throws HeuristicMixedException, HeuristicRollbackException, RollbackException, SecurityException, SystemException {
153 }
154
155 public boolean delistResource(XAResource xaResource, int i) throws IllegalStateException, SystemException {
156 return true;
157 }
158
159 public boolean enlistResource(XAResource xaResource) throws IllegalStateException, RollbackException, SystemException {
160 return true;
161 }
162
163 public int getStatus() throws SystemException {
164 return 0;
165 }
166
167 public void registerSynchronization(Synchronization synchronization) throws IllegalStateException, RollbackException, SystemException {
168 }
169
170 public void rollback() throws IllegalStateException, SystemException {
171 }
172
173 public void setRollbackOnly() throws IllegalStateException, SystemException {
174 }
175 }
176
177 public static class DummyXid implements Xid {
178
179 private int formatId = 123456;
180 private byte[] gtrid;
181 private byte[] bqual;
182
183 public DummyXid(long gtrid, long bqual) {
184 this.gtrid = longToBytes(gtrid);
185 this.bqual = longToBytes(bqual);
186 }
187
188 public DummyXid(Xid xid) {
189 this.formatId = xid.getFormatId();
190 this.gtrid = xid.getGlobalTransactionId();
191 this.bqual = xid.getBranchQualifier();
192 }
193
194 public int getFormatId() {
195 return formatId;
196 }
197
198 public byte[] getGlobalTransactionId() {
199 return gtrid;
200 }
201
202 public byte[] getBranchQualifier() {
203 return bqual;
204 }
205
206 @Override
207 public int hashCode() {
208 return formatId + arrayHashCode(gtrid) + arrayHashCode(bqual);
209 }
210
211 @Override
212 public boolean equals(Object o) {
213 if (o instanceof DummyXid) {
214 DummyXid otherXid = (DummyXid) o;
215 return formatId == otherXid.formatId &&
216 Arrays.equals(gtrid, otherXid.gtrid) &&
217 Arrays.equals(bqual, otherXid.bqual);
218 }
219 return false;
220 }
221
222 @Override
223 public String toString() {
224 return "DummyXid [" + hashCode() + "]";
225 }
226 }
227 }