1 package org.csc.phynixx.connection;
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.common.cast.ImplementorUtils;
25 import org.csc.phynixx.connection.loggersystem.IPhynixxLoggerSystemStrategy;
26 import org.csc.phynixx.loggersystem.logrecord.IXADataRecorder;
27
28 import java.util.List;
29
30
31
32
33 public class PhynixxRecovery<C extends IPhynixxConnection> implements IPhynixxRecovery<C> {
34
35 private final PhynixxManagedConnectionFactory managedConnectionFactory;
36 IPhynixxLoggerSystemStrategy<C> loggerSystemStrategy = null;
37
38 public PhynixxRecovery(IPhynixxConnectionFactory<C> connectionFactory) {
39 managedConnectionFactory = new PhynixxManagedConnectionFactory(connectionFactory);
40 }
41
42 public IPhynixxLoggerSystemStrategy<C> getLoggerSystemStrategy() {
43 return loggerSystemStrategy;
44 }
45
46 public void setLoggerSystemStrategy(IPhynixxLoggerSystemStrategy<C> loggerSystemStrategy) {
47
48 this.loggerSystemStrategy = loggerSystemStrategy;
49 }
50
51 private IPhynixxManagedConnection<C> getManagedConnection() {
52 return this.managedConnectionFactory.getManagedConnection();
53 }
54
55 public List<IPhynixxConnectionProxyDecorator<C>> getConnectionProxyDecorators() {
56 return this.managedConnectionFactory.getManagedConnectionDecorators();
57 }
58
59 public void addConnectionProxyDecorator(
60 IPhynixxConnectionProxyDecorator<C> connectionProxyDecorator) {
61 this.managedConnectionFactory.addManagedConnectionDecorator(connectionProxyDecorator);
62 }
63
64
65 @Override
66 public void recover(IPhynixxRecovery.IRecoveredManagedConnection<C> recoveredManagedConnectionCallback) {
67
68 if (this.loggerSystemStrategy == null) {
69 throw new IllegalStateException("LoggerSystem must be reset to recover from this System");
70 }
71 this.loggerSystemStrategy.close();
72
73
74 try {
75
76 List<IXADataRecorder> messageLoggers = this.loggerSystemStrategy.readIncompleteTransactions();
77 IPhynixxManagedConnection<C> con = null;
78 for (int i = 0; i < messageLoggers.size(); i++) {
79 try {
80 IXADataRecorder msgLogger = messageLoggers.get(i);
81 con = this.getManagedConnection();
82 if (!ImplementorUtils.isImplementationOf(con, IXADataRecorderAware.class)) {
83 throw new IllegalStateException("Connection does not support " + IXADataRecorderAware.class + " and can't be recovered");
84 } else {
85
86
87
88 IXADataRecorderAware xaDataRecorderAware = ImplementorUtils.cast(con, IXADataRecorderAware.class);
89 IXADataRecorder dataRecorder = xaDataRecorderAware.getXADataRecorder();
90 if (dataRecorder != null) {
91 dataRecorder.destroy();
92 }
93 xaDataRecorderAware.setXADataRecorder(msgLogger);
94 }
95
96 con.recover();
97
98 if (recoveredManagedConnectionCallback != null) {
99 recoveredManagedConnectionCallback.managedConnectionRecovered(con.toConnection());
100 }
101 } finally {
102 if (con != null) {
103 con.close();
104 }
105 }
106 }
107
108 } finally {
109 if (this.loggerSystemStrategy != null) {
110 this.loggerSystemStrategy.close();
111 }
112 }
113 }
114 }