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.context.task;
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.logging.GgInternalLogger;
29  import goldengate.common.logging.GgInternalLoggerFactory;
30  import openr66.client.SubmitTransfer;
31  import openr66.context.R66Session;
32  import openr66.context.task.exception.OpenR66RunnerErrorException;
33  import openr66.database.DbConstant;
34  import openr66.database.data.DbTaskRunner;
35  import openr66.protocol.configuration.Configuration;
36  import openr66.protocol.utils.R66Future;
37  
38  /**
39   * Transfer task:<br>
40   *
41   * Result of arguments will be as r66send command.<br>
42   * Format is like r66send command in any order except "-info" which should be
43   * the last item:<br>
44   * "-file filepath -to requestedHost -rule rule [-md5] [-start yyyyMMddHHmmss or -delay (delay or +delay)] [-info information]"<br>
45   * <br>
46   * INFO is the only one field that can contains blank character.<br>
47   *
48   * @author Frederic Bregier
49   *
50   */
51  public class TransferTask extends AbstractTask {
52      /**
53       * Internal Logger
54       */
55      private static final GgInternalLogger logger = GgInternalLoggerFactory
56              .getLogger(TransferTask.class);
57  
58      /**
59       * @param argRule
60       * @param delay
61       * @param argTransfer
62       * @param session
63       */
64      public TransferTask(String argRule, int delay, String argTransfer,
65              R66Session session) {
66          super(TaskType.TRANSFER, delay, argRule, argTransfer, session);
67      }
68  
69      /*
70       * (non-Javadoc)
71       *
72       * @see openr66.context.task.AbstractTask#run()
73       */
74      @Override
75      public void run() {
76          logger.info("Transfer with " + argRule + ":" + argTransfer + " and {}",
77                  session);
78          String finalname = argRule;
79          finalname = getReplacedValue(finalname, argTransfer.split(" "));
80          String[] args = finalname.split(" ");
81          if (args.length < 6) {
82              futureCompletion.setFailure(
83                      new OpenR66RunnerErrorException("Not enough argument in Transfer"));
84              return;
85          }
86          String filepath = null;
87          String requested = null;
88          String rule = null;
89          String information = null;
90          boolean isMD5 = false;
91          int blocksize = Configuration.configuration.BLOCKSIZE;
92          Timestamp timestart = null;
93          for (int i = 0; i < args.length; i ++) {
94              if (args[i].equalsIgnoreCase("-to")) {
95                  i ++;
96                  requested = args[i];
97              } else if (args[i].equalsIgnoreCase("-file")) {
98                  i ++;
99                  filepath = args[i];
100             } else if (args[i].equalsIgnoreCase("-rule")) {
101                 i ++;
102                 rule = args[i];
103             } else if (args[i].equalsIgnoreCase("-info")) {
104                 i ++;
105                 information = args[i];
106                 i ++;
107                 while (i < args.length) {
108                     information += " " + args[i];
109                     i ++;
110                 }
111             } else if (args[i].equalsIgnoreCase("-md5")) {
112                 isMD5 = true;
113             } else if (args[i].equalsIgnoreCase("-block")) {
114                 i ++;
115                 blocksize = Integer.parseInt(args[i]);
116                 if (blocksize < 100) {
117                     logger.warn("Block size is too small: " + blocksize);
118                     blocksize = Configuration.configuration.BLOCKSIZE;
119                 }
120             } else if (args[i].equalsIgnoreCase("-start")) {
121                 i++;
122                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
123                 Date date;
124                 try {
125                     date = dateFormat.parse(args[i]);
126                     timestart = new Timestamp(date.getTime());
127                 } catch (ParseException e) {
128                 }
129             } else if (args[i].equalsIgnoreCase("-delay")) {
130                 i++;
131                 if (args[i].charAt(0) == '+') {
132                     timestart = new Timestamp(System.currentTimeMillis()+
133                             Long.parseLong(args[i].substring(1)));
134                 } else {
135                     timestart = new Timestamp(Long.parseLong(args[i]));
136                 }
137             }
138         }
139         if (information == null) {
140             information = "noinfo";
141         }
142         R66Future future = new R66Future(true);
143         SubmitTransfer transaction = new SubmitTransfer(future,
144                 requested, filepath, rule, information, isMD5, blocksize, DbConstant.ILLEGALVALUE,
145                 timestart);
146         transaction.run();
147         future.awaitUninterruptibly();
148         futureCompletion.setResult(future.getResult());
149         DbTaskRunner runner = future.getResult().runner;
150         if (future.isSuccess()) {
151             logger.info("Prepare transfer in\n    SUCCESS\n    "+runner.toShortString()+
152                             "\n    <REMOTE>"+requested+"</REMOTE>");
153             futureCompletion.setSuccess();
154         } else {
155             if (runner != null) {
156                 if (future.getCause() == null) {
157                     futureCompletion.cancel();
158                 } else {
159                     futureCompletion.setFailure(future.getCause());
160                 }
161                 logger.error("Prepare transfer in\n    FAILURE\n     "+runner.toShortString()+
162                             "\n    <REMOTE>"+requested+"</REMOTE>", future.getCause());
163             } else {
164                 if (future.getCause() == null) {
165                     futureCompletion.cancel();
166                 } else {
167                     futureCompletion.setFailure(future.getCause());
168                 }
169                 logger.error("Prepare transfer in\n    FAILURE without any runner back", future.getCause());
170             }
171         }
172     }
173 
174 }