View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.client;
22  
23  import goldengate.common.database.exception.GoldenGateDatabaseException;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.common.logging.GgSlf4JLoggerFactory;
26  import openr66.commander.ClientRunner;
27  import openr66.context.ErrorCode;
28  import openr66.context.R66Result;
29  import openr66.context.task.exception.OpenR66RunnerErrorException;
30  import openr66.database.DbConstant;
31  import openr66.database.data.DbTaskRunner;
32  import openr66.protocol.configuration.Configuration;
33  import openr66.protocol.exception.OpenR66ProtocolNoConnectionException;
34  import openr66.protocol.exception.OpenR66ProtocolNotYetConnectionException;
35  import openr66.protocol.exception.OpenR66ProtocolPacketException;
36  import openr66.protocol.localhandler.LocalChannelReference;
37  import openr66.protocol.networkhandler.NetworkTransaction;
38  import openr66.protocol.utils.ChannelUtils;
39  import openr66.protocol.utils.R66Future;
40  
41  import org.jboss.netty.logging.InternalLoggerFactory;
42  
43  /**
44   * Direct Transfer from a client with or without database connection
45   *
46   * @author Frederic Bregier
47   *
48   */
49  public class DirectTransfer extends AbstractTransfer {
50      protected final NetworkTransaction networkTransaction;
51  
52      public DirectTransfer(R66Future future, String remoteHost,
53              String filename, String rulename, String fileinfo, boolean isMD5, int blocksize, long id,
54              NetworkTransaction networkTransaction) {
55          // no starttime since it is direct (blocking request, no delay)
56          super(DirectTransfer.class,
57                  future, filename, rulename, fileinfo, isMD5, remoteHost, blocksize, id, null);
58          this.networkTransaction = networkTransaction;
59      }
60  
61      /**
62       * Prior to call this method, the pipeline and NetworkTransaction must have been initialized.
63       * It is the responsibility of the caller to finish all network resources.
64       */
65      public void run() {
66          if (logger == null) {
67              logger = GgInternalLoggerFactory.getLogger(DirectTransfer.class);
68          }
69          DbTaskRunner taskRunner = this.initRequest();
70          ClientRunner runner = new ClientRunner(networkTransaction, taskRunner, future);
71          OpenR66ProtocolNotYetConnectionException exc = null;
72          for (int i = 0; i < Configuration.RETRYNB; i++) {
73              try {
74                  runner.runTransfer();
75                  exc = null;
76                  break;
77              } catch (OpenR66RunnerErrorException e) {
78                  logger.debug("Cannot Transfer", e);
79                  future.setResult(new R66Result(e, null, true,
80                          ErrorCode.Internal, taskRunner));
81                  future.setFailure(e);
82                  return;
83              } catch (OpenR66ProtocolNoConnectionException e) {
84                  logger.debug("Cannot Connect", e);
85                  future.setResult(new R66Result(e, null, true,
86                          ErrorCode.ConnectionImpossible, taskRunner));
87                  // since no connection : just forget it
88                  if (nolog) {
89                      try {
90                          taskRunner.delete();
91                      } catch (GoldenGateDatabaseException e1) {
92                      }
93                  }
94                  future.setFailure(e);
95                  return;
96              } catch (OpenR66ProtocolPacketException e) {
97                  logger.debug("Bad Protocol", e);
98                  future.setResult(new R66Result(e, null, true,
99                          ErrorCode.TransferError, taskRunner));
100                 future.setFailure(e);
101                 return;
102             } catch (OpenR66ProtocolNotYetConnectionException e) {
103                 logger.debug("Not Yet Connected", e);
104                 exc = e;
105                 continue;
106             }
107         }
108         if (exc!= null) {
109             taskRunner.setLocalChannelReference(new LocalChannelReference());
110             logger.debug("Cannot Connect", exc);
111             future.setResult(new R66Result(exc, null, true,
112                     ErrorCode.ConnectionImpossible, taskRunner));
113             // since no connection : just forget it
114             if (nolog) {
115                 try {
116                     taskRunner.delete();
117                 } catch (GoldenGateDatabaseException e1) {
118                 }
119             }
120             future.setFailure(exc);
121             return;
122         }
123     }
124 
125     public static void main(String[] args) {
126         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
127         if (logger == null) {
128             logger = GgInternalLoggerFactory.getLogger(DirectTransfer.class);
129         }
130         if (! getParams(args, false)) {
131             logger.error("Wrong initialization");
132             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
133                 DbConstant.admin.close();
134             }
135             ChannelUtils.stopLogger();
136             System.exit(2);
137         }
138         long time1 = System.currentTimeMillis();
139         R66Future future = new R66Future(true);
140 
141         Configuration.configuration.pipelineInit();
142         NetworkTransaction networkTransaction = new NetworkTransaction();
143         try {
144             DirectTransfer transaction = new DirectTransfer(future,
145                     rhost, localFilename, rule, fileInfo, ismd5, block, idt,
146                     networkTransaction);
147             transaction.run();
148             future.awaitUninterruptibly();
149             long time2 = System.currentTimeMillis();
150             logger.debug("finish transfer: "+future.isSuccess());
151             long delay = time2 - time1;
152             R66Result result = future.getResult();
153             if (future.isSuccess()) {
154                 if (result.runner.getErrorInfo() == ErrorCode.Warning) {
155                     logger.warn("Transfer in status:\nWARNED\n    "+result.runner.toShortString()+
156                             "\n    <REMOTE>"+rhost+"</REMOTE>"+
157                             "\n    <FILEFINAL>" +
158                             (result.file != null? result.file.toString()+"</FILEFINAL>" : "no file")
159                             +"\n    delay: "+delay);
160                 } else {
161                     logger.info("Transfer in status:\nSUCCESS\n    "+result.runner.toShortString()+
162                             "\n    <REMOTE>"+rhost+"</REMOTE>"+
163                             "\n    <FILEFINAL>" +
164                             (result.file != null? result.file.toString()+"</FILEFINAL>" : "no file")
165                             +"\n    delay: "+delay);
166                 }
167                 if (nolog) {
168                     // In case of success, delete the runner
169                     try {
170                         result.runner.delete();
171                     } catch (GoldenGateDatabaseException e) {
172                         logger.warn("Cannot apply nolog to\n    "+result.runner.toShortString(), e);
173                     }
174                 }
175             } else {
176                 if (result == null || result.runner == null) {
177                     logger.error("Transfer in\n    FAILURE with no Id", future.getCause());
178                     networkTransaction.closeAll();
179                     System.exit(ErrorCode.Unknown.ordinal());
180                 }
181                 if (result.runner.getErrorInfo() == ErrorCode.Warning) {
182                     logger.warn("Transfer is\n    WARNED\n    "+result.runner.toShortString()+
183                             "\n    <REMOTE>"+rhost+"</REMOTE>", future.getCause());
184                     networkTransaction.closeAll();
185                     System.exit(result.code.ordinal());
186                 } else {
187                     logger.error("Transfer in\n    FAILURE\n    "+result.runner.toShortString()+
188                             "\n    <REMOTE>"+rhost+"</REMOTE>", future.getCause());
189                     networkTransaction.closeAll();
190                     System.exit(result.code.ordinal());
191                 }
192             }
193         } catch (Exception e) {
194             logger.debug("exc",e);
195         } finally {
196             logger.debug("finish transfer: "+future.isDone()+":"+future.isSuccess());
197             networkTransaction.closeAll();
198             // In case something wrong append
199             if (future.isDone() && future.isSuccess()) {
200                 System.exit(0);
201             } else {
202                 System.exit(66);
203             }
204         }
205     }
206 
207 }