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 java.sql.Timestamp;
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Date;
27  
28  import goldengate.common.database.exception.GoldenGateDatabaseException;
29  import goldengate.common.logging.GgInternalLogger;
30  import goldengate.common.logging.GgInternalLoggerFactory;
31  import openr66.configuration.FileBasedConfiguration;
32  import openr66.context.ErrorCode;
33  import openr66.context.R66Result;
34  import openr66.database.DbConstant;
35  import openr66.database.data.DbRule;
36  import openr66.database.data.DbTaskRunner;
37  import openr66.protocol.configuration.Configuration;
38  import openr66.protocol.exception.OpenR66DatabaseGlobalException;
39  import openr66.protocol.localhandler.packet.RequestPacket;
40  import openr66.protocol.utils.R66Future;
41  
42  /**
43   * Abstract class for Transfer operation
44   *
45   * @author Frederic Bregier
46   *
47   */
48  public abstract class AbstractTransfer implements Runnable {
49      /**
50       * Internal Logger
51       */
52      static protected volatile GgInternalLogger logger;
53  
54      protected final R66Future future;
55  
56      protected final String filename;
57  
58      protected final String rulename;
59  
60      protected final String fileinfo;
61  
62      protected final boolean isMD5;
63  
64      protected final String remoteHost;
65  
66      protected final int blocksize;
67      
68      protected final long id;
69  
70      protected final Timestamp startTime;
71  
72      /**
73       * @param clasz Class of Client Transfer
74       * @param future
75       * @param filename
76       * @param rulename
77       * @param fileinfo
78       * @param isMD5
79       * @param remoteHost
80       * @param blocksize
81       * @param id
82       */
83      public AbstractTransfer(Class<?> clasz, R66Future future, String filename,
84              String rulename, String fileinfo,
85              boolean isMD5, String remoteHost, int blocksize, long id, Timestamp timestart) {
86          if (logger == null) {
87              logger = GgInternalLoggerFactory.getLogger(clasz);
88          }
89          this.future = future;
90          this.filename = filename;
91          this.rulename = rulename;
92          this.fileinfo = fileinfo;
93          this.isMD5 = isMD5;
94          this.remoteHost = remoteHost;
95          this.blocksize = blocksize;
96          this.id = id;
97          this.startTime = timestart;
98      }
99      /**
100      * Initiate the Request and return a potential DbTaskRunner
101      * @return null if an error occurs or a DbTaskRunner
102      */
103     protected DbTaskRunner initRequest() {
104         DbRule rule;
105         try {
106             rule = new DbRule(DbConstant.admin.session, rulename);
107         } catch (GoldenGateDatabaseException e) {
108             logger.error("Cannot get Rule: "+rulename, e);
109             future.setResult(new R66Result(new OpenR66DatabaseGlobalException(e), null, true,
110                     ErrorCode.Internal, null));
111             future.setFailure(e);
112             return null;
113         }
114         int mode = rule.mode;
115         if (isMD5) {
116             mode = RequestPacket.getModeMD5(mode);
117         }
118         RequestPacket request;
119         DbTaskRunner taskRunner = null;
120         if (id != DbConstant.ILLEGALVALUE) {
121             try {
122                 taskRunner = new DbTaskRunner(DbConstant.admin.session, id, 
123                         remoteHost);
124                 request = new RequestPacket(rulename,
125                         mode, filename, taskRunner.getBlocksize(), taskRunner.getRank(),
126                         id, fileinfo);
127             } catch (GoldenGateDatabaseException e) {
128                 request = new RequestPacket(rulename,
129                         mode, filename, blocksize, 0,
130                         id, fileinfo);
131             }
132         } else {
133             request = new RequestPacket(rulename,
134                 mode, filename, blocksize, 0,
135                 id, fileinfo);
136             // Not isRecv since it is the requester, so send => isRetrieve is true
137             boolean isRetrieve = ! RequestPacket.isRecvMode(request.getMode());
138             try {
139                 taskRunner =
140                     new DbTaskRunner(DbConstant.admin.session,rule,isRetrieve,request,remoteHost, startTime);
141             } catch (GoldenGateDatabaseException e) {
142                 logger.error("Cannot get task", e);
143                 future.setResult(new R66Result(new OpenR66DatabaseGlobalException(e), null, true,
144                         ErrorCode.Internal, null));
145                 future.setFailure(e);
146                 return null;
147             }
148         }
149         return taskRunner;
150     }
151     static protected String rhost = null;
152     static protected String localFilename = null;
153     static protected String rule = null;
154     static protected String fileInfo = null;
155     static protected boolean ismd5 = false;
156     static protected int block = 0x10000; // 64K as default
157     static protected boolean nolog = false;
158     static protected long idt = DbConstant.ILLEGALVALUE;
159     static protected Timestamp ttimestart = null;
160     static protected SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
161 
162     /**
163      * Parse the parameter and set current values
164      * @param args
165      * @param submitOnly True if the client is only a submitter (through database)
166      * @return True if all parameters were found and correct
167      */
168     protected static boolean getParams(String []args, boolean submitOnly) {
169         if (args.length < 2) {
170             logger
171                     .error("Needs at least 3 or 4 arguments:\n" +
172                     		"  the XML client configuration file,\n" +
173                                 "  '-to' the remoteHost Id,\n" +
174                                 "  '-file' the file to transfer,\n" +
175                                 "  '-rule' the rule\n"+
176                                 "Or\n"+
177                                 "  '-to' the remoteHost Id,\n" +
178                                 "  '-id' \"Id of a previous transfer\",\n"+
179                                 "Other options:\n" +
180                                 "  '-info' \"information to send\",\n" +
181                                 "  '-md5' to force MD5 by packet control,\n" +
182                                 "  '-block' size of packet > 1K (prefered is 64K),\n" +
183                                 "  '-nolog' to not log locally this action\n" +
184                                 "  '-start' \"time start\" as yyyyMMddHHmmss (override previous -delay options)\n" +
185                                 "  '-delay' \"+delay in ms\" as delay in ms from current time(override previous -start options)\n"+
186                                 "  '-delay' \"delay in ms\" as time in ms (override previous -start options)");
187             return false;
188         }
189         if (submitOnly) {
190             if (! FileBasedConfiguration
191                     .setSubmitClientConfigurationFromXml(Configuration.configuration, args[0])) {
192                 logger
193                         .error("Needs a correct configuration file as first argument");
194                 return false;
195             }
196         } else if (! FileBasedConfiguration
197                 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
198             logger
199                     .error("Needs a correct configuration file as first argument");
200             return false;
201         }
202         // Now set default values from configuration
203         block = Configuration.configuration.BLOCKSIZE;
204         for (int i = 1; i < args.length; i++) {
205             if (args[i].equalsIgnoreCase("-to")) {
206                 i++;
207                 rhost = args[i];
208             } else if (args[i].equalsIgnoreCase("-file")) {
209                 i++;
210                 localFilename = args[i];
211             } else if (args[i].equalsIgnoreCase("-rule")) {
212                 i++;
213                 rule = args[i];
214             } else if (args[i].equalsIgnoreCase("-info")) {
215                 i++;
216                 fileInfo = args[i];
217             } else if (args[i].equalsIgnoreCase("-md5")) {
218                 ismd5 = true;
219             } else if (args[i].equalsIgnoreCase("-block")) {
220                 i++;
221                 block = Integer.parseInt(args[i]);
222                 if (block < 100) {
223                     logger.error("Block size is too small: "+block);
224                     return false;
225                 }
226             } else if (args[i].equalsIgnoreCase("-nolog")) {
227                 nolog = true;
228                 i++;
229             } else if (args[i].equalsIgnoreCase("-id")) {
230                 i++;
231                 idt = Long.parseLong(args[i]);
232             } else if (args[i].equalsIgnoreCase("-start")) {
233                 i++;
234                 Date date;
235                 try {
236                     date = dateFormat.parse(args[i]);
237                     ttimestart = new Timestamp(date.getTime());
238                 } catch (ParseException e) {
239                 }
240             } else if (args[i].equalsIgnoreCase("-delay")) {
241                 i++;
242                 if (args[i].charAt(0) == '+') {
243                     ttimestart = new Timestamp(System.currentTimeMillis()+
244                             Long.parseLong(args[i].substring(1)));
245                 } else {
246                     ttimestart = new Timestamp(Long.parseLong(args[i]));
247                 }
248             }
249         }
250         if (fileInfo == null) {
251             fileInfo = "noinfo";
252         }
253         if (rhost != null && rule != null && localFilename != null) {
254             return true;
255         } else if (idt != DbConstant.ILLEGALVALUE && rhost != null) {
256             try {
257                 DbTaskRunner runner = new DbTaskRunner(DbConstant.admin.session, idt, 
258                         rhost);
259                 rule = runner.getRuleId();
260                 localFilename = runner.getOriginalFilename();
261             } catch (GoldenGateDatabaseException e) {
262                 logger.error("All params are not correctly set! Need at least (-to -rule and -file)" +
263                 		" or (-to and -id) params", e);
264                 return false;
265             }
266 
267         }
268         logger.error("All params are not set! Need at least (-to -rule and -file)" +
269                                 " or (-to and -id) params");
270         return false;
271     }
272 }