View Javadoc

1   package org.csc.phynixx.connection;
2   
3   /*
4    * #%L
5    * phynixx-connection
6    * %%
7    * Copyright (C) 2014 Christoph Schmidt-Casdorff
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
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   * Created by Christoph Schmidt-Casdorff on 26.02.14.
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              // get all recoverable transaction data
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                          // Falls Connection zugeordneten DataLogger hat, so wird dieser freigegeben
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 }