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.protocol.test;
22  
23  import goldengate.common.database.exception.GoldenGateDatabaseException;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.common.logging.GgSlf4JLoggerFactory;
26  
27  import org.jboss.netty.logging.InternalLoggerFactory;
28  
29  import openr66.client.ProgressBarTransfer;
30  import openr66.context.ErrorCode;
31  import openr66.context.R66Result;
32  import openr66.database.DbConstant;
33  import openr66.protocol.configuration.Configuration;
34  import openr66.protocol.networkhandler.NetworkTransaction;
35  import openr66.protocol.utils.ChannelUtils;
36  import openr66.protocol.utils.R66Future;
37  
38  /**
39   * @author Frederic Bregier
40   *
41   */
42  public class TestProgressBarTransfer extends ProgressBarTransfer {
43  
44      /**
45       * @param future
46       * @param remoteHost
47       * @param filename
48       * @param rulename
49       * @param fileinfo
50       * @param isMD5
51       * @param blocksize
52       * @param id
53       * @param networkTransaction
54       * @param callbackdelay
55       */
56      public TestProgressBarTransfer(R66Future future, String remoteHost,
57              String filename, String rulename, String fileinfo, boolean isMD5,
58              int blocksize, long id, NetworkTransaction networkTransaction,
59              long callbackdelay) {
60          super(future, remoteHost, filename, rulename, fileinfo, isMD5,
61                  blocksize, id, networkTransaction, callbackdelay);
62      }
63  
64      /* (non-Javadoc)
65       * @see openr66.client.ProgressBarTransfer#callBack(int, int)
66       */
67      @Override
68      public void callBack(int currentBlock, int blocksize) {
69          if (filesize == 0) {
70              System.err.println("Block: "+currentBlock+" BSize: "+blocksize);
71          } else {
72              System.err.println("Block: "+currentBlock+" BSize: "+blocksize+" on "+
73                      (int) (Math.ceil(((double)filesize/(double)blocksize))));
74          }
75      }
76  
77      /* (non-Javadoc)
78       * @see openr66.client.ProgressBarTransfer#lastCallBack(boolean, int, int)
79       */
80      @Override
81      public void lastCallBack(boolean success, int currentBlock, int blocksize) {
82          if (filesize == 0) {
83              System.err.println("Status: "+success+" Block: "+currentBlock+" BSize: "+blocksize);
84          } else {
85              System.err.println("Status: "+success+" Block: "+currentBlock+" BSize: "+blocksize+
86                      " Size="+filesize);
87          }
88      }
89  
90      public static void main(String[] args) {
91          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
92                  null));
93          if (logger == null) {
94              logger = GgInternalLoggerFactory.getLogger(ProgressBarTransfer.class);
95          }
96          if (! getParams(args, false)) {
97              logger.error("Wrong initialization");
98              if (DbConstant.admin != null && DbConstant.admin.isConnected) {
99                  DbConstant.admin.close();
100             }
101             ChannelUtils.stopLogger();
102             System.exit(2);
103         }
104         long time1 = System.currentTimeMillis();
105         R66Future future = new R66Future(true);
106 
107         Configuration.configuration.pipelineInit();
108         NetworkTransaction networkTransaction = new NetworkTransaction();
109         try {
110             TestProgressBarTransfer transaction = new TestProgressBarTransfer(future,
111                     rhost, localFilename, rule, fileInfo, ismd5, block, idt,
112                     networkTransaction, 100);
113             transaction.run();
114             future.awaitUninterruptibly();
115             long time2 = System.currentTimeMillis();
116             long delay = time2 - time1;
117             R66Result result = future.getResult();
118             if (future.isSuccess()) {
119                 if (result.runner.getErrorInfo() == ErrorCode.Warning) {
120                     logger.warn("Transfer in status:\nWARNED\n    "+result.runner.toShortString()+
121                             "\n    <REMOTE>"+rhost+"</REMOTE>"+
122                             "\n    <FILEFINAL>" +
123                             (result.file != null? result.file.toString()+"</FILEFINAL>" : "no file")
124                             +"\n    delay: "+delay);
125                 } else {
126                     logger.info("Transfer in status:\nSUCCESS\n    "+result.runner.toShortString()+
127                             "\n    <REMOTE>"+rhost+"</REMOTE>"+
128                             "\n    <FILEFINAL>" +
129                             (result.file != null? result.file.toString()+"</FILEFINAL>" : "no file")
130                             +"\n    delay: "+delay);
131                 }
132                 if (nolog) {
133                     // In case of success, delete the runner
134                     try {
135                         result.runner.delete();
136                     } catch (GoldenGateDatabaseException e) {
137                         logger.warn("Cannot apply nolog to\n    "+result.runner.toShortString(), e);
138                     }
139                 }
140             } else {
141                 if (result == null || result.runner == null) {
142                     logger.error("Transfer in\n    FAILURE with no Id", future.getCause());
143                     networkTransaction.closeAll();
144                     System.exit(ErrorCode.Unknown.ordinal());
145                 }
146                 if (result.runner.getErrorInfo() == ErrorCode.Warning) {
147                     logger.warn("Transfer is\n    WARNED\n    "+result.runner.toShortString()+
148                             "\n    <REMOTE>"+rhost+"</REMOTE>", future.getCause());
149                     networkTransaction.closeAll();
150                     System.exit(result.code.ordinal());
151                 } else {
152                     logger.error("Transfer in\n    FAILURE\n    "+result.runner.toShortString()+
153                             "\n    <REMOTE>"+rhost+"</REMOTE>", future.getCause());
154                     networkTransaction.closeAll();
155                     System.exit(result.code.ordinal());
156                 }
157             }
158         } finally {
159             networkTransaction.closeAll();
160             // In case something wrong append
161             if (future.isDone() && future.isSuccess()) {
162                 System.exit(0);
163             } else {
164                 System.exit(66);
165             }
166         }
167     }
168 
169 
170 }