1 package org.csc.phynixx.xa;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.csc.phynixx.connection.IPhynixxConnection;
25 import org.csc.phynixx.connection.IPhynixxConnectionFactory;
26 import org.csc.phynixx.connection.IPhynixxManagedConnectionFactory;
27 import org.csc.phynixx.watchdog.IWatchdog;
28 import org.csc.phynixx.watchdog.IWatchedCondition;
29 import org.csc.phynixx.watchdog.WatchdogRegistry;
30
31 import javax.transaction.TransactionManager;
32 import javax.transaction.xa.Xid;
33
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.Set;
38
39
40
41
42
43 public class PhynixxXAResourceFactory<T extends IPhynixxConnection> implements IPhynixxXAResourceFactory<T>, IPhynixxXAResourceListener<T> {
44
45 private static final long CHECK_INTERVAL = 100;
46
47 private static IResourceIDGenerator ID_GENERATOR = new IDGenerator();
48
49
50
51
52
53 PhynixxXAResource<T> xaResource;
54
55
56 private final IXATransactionalBranchRepository<T> xaTransactionalBranchRepository;
57
58
59
60
61
62
63
64
65 private Set<PhynixxXAResource<T>> xaresources = Collections.synchronizedSet(new HashSet());
66
67 private Object resourceFactoryId = null;
68
69
70
71
72 private IPhynixxManagedConnectionFactory<T> managedConnectionFactory;
73
74 private TransactionManager transactionManager = null;
75
76 private boolean supportsTimeOut= false;
77
78 public boolean isSupportsTimeOut() {
79 return supportsTimeOut;
80 }
81
82 public void setSupportsTimeOut(boolean supportsTimeOut) {
83 this.supportsTimeOut = supportsTimeOut;
84 }
85
86
87
88
89 private IWatchdog xaresourrceWatchdog = null;
90
91 public PhynixxXAResourceFactory(
92 IPhynixxManagedConnectionFactory<T> connectionFactory,
93 TransactionManager transactionManager) {
94 this("RF", connectionFactory, transactionManager);
95 }
96
97 public PhynixxXAResourceFactory(
98 Object id,
99 IPhynixxManagedConnectionFactory<T> connectionFactory,
100 TransactionManager transactionManager) {
101 this.resourceFactoryId = id;
102
103
104
105 this.managedConnectionFactory = connectionFactory;
106
107 xaTransactionalBranchRepository = new XATransactionalBranchRepository();
108
109 this.transactionManager = transactionManager;
110
111
112
113 this.xaResource= instanciateXAResource();
114 }
115
116
117 public String getId() {
118 return resourceFactoryId.toString();
119 }
120
121 public IPhynixxManagedConnectionFactory<T> getManagedConnectionFactory() {
122
123 return managedConnectionFactory;
124 }
125
126 IXATransactionalBranchRepository<T> getXATransactionalBranchRepository() {
127 return xaTransactionalBranchRepository;
128 }
129
130 public TransactionManager getTransactionManager() {
131 return transactionManager;
132 }
133
134
135 protected IResourceIDGenerator getIdGenerator() {
136 return ID_GENERATOR;
137 }
138
139 protected static void setIdGenerator(IResourceIDGenerator idGenerator) {
140 ID_GENERATOR = idGenerator;
141 }
142
143
144 private PhynixxXAResource<T> instanciateXAResource() {
145 PhynixxXAResource<T> xares = new PhynixxXAResource<T>(createXAResourceId(), this.transactionManager, this);
146 xares.addXAResourceListener(this);
147 this.xaresources.add(xares);
148 return xares;
149 }
150
151 public IPhynixxXAConnection<T> getXAConnection() {
152 return this.xaResource.getXAConnection();
153 }
154
155
156
157
158
159
160
161
162 private String createXAResourceId() {
163 return this.resourceFactoryId + "_" + this.ID_GENERATOR.generate();
164 }
165
166
167
168
169
170
171 @Override
172 public IPhynixxXAResource<T> getXAResource() {
173 return this.xaResource;
174 }
175
176
177 public void closed(IPhynixxXAResourceEvent event) {
178 this.xaresources.remove(event.getXAResource());
179 }
180
181
182
183
184
185
186
187
188
189
190 @Override
191 public synchronized Xid[] recover() {
192 return new Xid[]{};
193 }
194
195
196
197
198 @Override
199 public synchronized void close() {
200 if (this.xaresources.size() > 0) {
201
202 Set<PhynixxXAResource<T>> tmpXAResources = new HashSet(this.xaresources);
203
204 for (Iterator<PhynixxXAResource<T>> iterator = tmpXAResources.iterator(); iterator.hasNext(); ) {
205 PhynixxXAResource<T> xaresource = iterator.next();
206 xaresource.close();
207 }
208 }
209
210 this.getXATransactionalBranchRepository().close();
211 }
212
213
214 synchronized void registerWatchCondition(IWatchedCondition cond) {
215 this.xaresourrceWatchdog.registerCondition(cond);
216 }
217
218 synchronized void unregisterWatchCondition(IWatchedCondition cond) {
219 this.xaresourrceWatchdog.unregisterCondition(cond);
220 }
221
222 @Override
223 public T getConnection() {
224 return this.getXAResource().getXAConnection().getConnection();
225 }
226
227 @Override
228 public Class<T> getConnectionInterface() {
229 return this.managedConnectionFactory.getConnectionInterface();
230 }
231
232
233 }