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 goldengate.common.command.exception.CommandAbstractException;
24  import goldengate.common.utility.GgStringUtils;
25  
26  import java.io.File;
27  import java.text.DateFormat;
28  import java.text.SimpleDateFormat;
29  import java.util.Date;
30  
31  import openr66.context.ErrorCode;
32  import openr66.context.R66Session;
33  import openr66.context.filesystem.R66Dir;
34  import openr66.context.filesystem.R66File;
35  import openr66.protocol.configuration.Configuration;
36  import openr66.protocol.exception.OpenR66ProtocolNoSslException;
37  import openr66.protocol.utils.R66Future;
38  
39  /**
40   * Abstract implementation of task
41   *
42   * @author Frederic Bregier
43   *
44   */
45  public abstract class AbstractTask implements Runnable {
46      /**
47       * Current full path of current FILENAME
48       */
49      public static final String TRUEFULLPATH = "#TRUEFULLPATH#";
50  
51      /**
52       * Current FILENAME (basename) (change in retrieval part)
53       */
54      public static final String TRUEFILENAME = "#TRUEFILENAME#";
55      /**
56       * Current full path of Original FILENAME (as transmitted) (before changing in retrieval part)
57       */
58      public static final String ORIGINALFULLPATH = "#ORIGINALFULLPATH#";
59  
60      /**
61       * Original FILENAME (basename) (before changing in retrieval part)
62       */
63      public static final String ORIGINALFILENAME = "#ORIGINALFILENAME#";
64  
65      /**
66       * Size of the current FILE
67       */
68      public static final String FILESIZE = "#FILESIZE#";
69  
70      /**
71       * Current full path of current RULE
72       */
73      public static final String RULE = "#RULE#";
74  
75      /**
76       * Date in yyyyMMdd format
77       */
78      public static final String DATE = "#DATE#";
79  
80      /**
81       * Hour in HHmmss format
82       */
83      public static final String HOUR = "#HOUR#";
84  
85      /**
86       * Remote host id (if not the initiator of the call)
87       */
88      public static final String REMOTEHOST = "#REMOTEHOST#";
89  
90      /**
91       * Remote host address
92       */
93      public static final String REMOTEHOSTADDR = "#REMOTEHOSTADDR#";
94  
95      /**
96       * Local host id
97       */
98      public static final String LOCALHOST = "#LOCALHOST#";
99  
100     /**
101      * Local host address
102      */
103     public static final String LOCALHOSTADDR = "#LOCALHOSTADDR#";
104 
105     /**
106      * Transfer id
107      */
108     public static final String TRANSFERID = "#TRANSFERID#";
109 
110     /**
111      * Requester Host
112      */
113     public static final String REQUESTERHOST = "#REQUESTERHOST#";
114 
115     /**
116      * Requested Host
117      */
118     public static final String REQUESTEDHOST = "#REQUESTEDHOST#";
119 
120     /**
121      * Full Transfer id (TRANSFERID_REQUESTERHOST_REQUESTEDHOST)
122      */
123     public static final String FULLTRANSFERID = "#FULLTRANSFERID#";
124 
125     /**
126      * Current or final RANK of block
127      */
128     public static final String RANKTRANSFER = "#RANKTRANSFER#";
129 
130     /**
131      * Block size used
132      */
133     public static final String BLOCKSIZE = "#BLOCKSIZE#";
134 
135     /**
136      * IN Path used
137      */
138     public static final String INPATH = "#INPATH#";
139 
140     /**
141      * OUT Path used
142      */
143     public static final String OUTPATH = "#OUTPATH#";
144 
145     /**
146      * WORK Path used
147      */
148     public static final String WORKPATH = "#WORKPATH#";
149 
150     /**
151      * ARCH Path used
152      */
153     public static final String ARCHPATH = "#ARCHPATH#";
154 
155     /**
156      * HOME Path used
157      */
158     public static final String HOMEPATH = "#HOMEPATH#";
159     /**
160      * Last Current Error Message
161      */
162     public static final String ERRORMSG = "#ERRORMSG#";
163     /**
164      * Last Current Error Code
165      */
166     public static final String ERRORCODE = "#ERRORCODE#";
167     /**
168      * Last Current Error Code in Full String
169      */
170     public static final String ERRORSTRCODE = "#ERRORSTRCODE#";
171     /**
172      * If specified, no Wait for Task Validation (default is wait)
173      */
174     public static final String NOWAIT = "#NOWAIT#";
175     /**
176      * If specified, use the LocalExec Daemon specified in the global configuration
177      * (default no usage of LocalExec)
178      */
179     public static final String LOCALEXEC = "#LOCALEXEC#";
180     
181     /**
182      * Type of operation
183      */
184     final TaskType type;
185 
186     /**
187      * Argument from Rule
188      */
189     final String argRule;
190 
191     /**
192      * Delay from Rule (if applicable)
193      */
194     final int delay;
195 
196     /**
197      * Argument from Transfer
198      */
199     final String argTransfer;
200 
201     /**
202      * Current session
203      */
204     final R66Session session;
205 
206     /**
207      * R66Future of completion
208      */
209     final R66Future futureCompletion;
210     /**
211      * Do we wait for a validation of the task ?
212      * Default = True
213      */
214     boolean waitForValidation = true;
215     /**
216      * Do we need to use LocalExec for an Exec Task ?
217      * Default = False
218      */
219     boolean useLocalExec = false;
220     /**
221      * Constructor
222      *
223      * @param type
224      * @param delay
225      * @param arg
226      * @param session
227      */
228     AbstractTask(TaskType type, int delay, String argRule, String argTransfer,
229             R66Session session) {
230         this.type = type;
231         this.delay = delay;
232         this.argRule = argRule;
233         this.argTransfer = argTransfer;
234         this.session = session;
235         futureCompletion = new R66Future(true);
236     }
237 
238     /**
239      * This is the only interface to execute an operator.
240      */
241     abstract public void run();
242 
243     /**
244      *
245      * @return True if the operation is in success status
246      */
247     public boolean isSuccess() {
248         futureCompletion.awaitUninterruptibly();
249         return futureCompletion.isSuccess();
250     }
251 
252     /**
253      *
254      * @return the R66Future of completion
255      */
256     public R66Future getFutureCompletion() {
257         return futureCompletion;
258     }
259 
260     /**
261      *
262      * @param arg
263      *            as the Format string where FIXED items will be replaced by
264      *            context values and next using argFormat as format second
265      *            argument; this arg comes from the rule itself
266      * @param argFormat
267      *            as format second argument; this argFormat comes from the transfer
268      *            Information itself
269      * @return The string with replaced values from context and second argument
270      */
271     protected String getReplacedValue(String arg, Object[] argFormat) {
272         StringBuilder builder = new StringBuilder(arg);
273         // check NOWAIT and LOCALEXEC
274         if (arg.contains(NOWAIT)) {
275             waitForValidation = false;
276             GgStringUtils.replaceAll(builder, NOWAIT, "");
277         }
278         if (arg.contains(LOCALEXEC)) {
279             useLocalExec = true;
280             GgStringUtils.replaceAll(builder, LOCALEXEC, "");
281         }
282         File trueFile = null;
283         if (session.getFile() != null) {
284             trueFile = session.getFile().getTrueFile();
285         }
286         if (trueFile != null) {
287             GgStringUtils.replaceAll(builder, TRUEFULLPATH, trueFile.getAbsolutePath());
288             GgStringUtils.replaceAll(builder, TRUEFILENAME, R66Dir
289                     .getFinalUniqueFilename(session.getFile()));
290             GgStringUtils.replaceAll(builder, FILESIZE, Long.toString(trueFile.length()));
291         } else {
292             GgStringUtils.replaceAll(builder, TRUEFULLPATH, "nofile");
293             GgStringUtils.replaceAll(builder, TRUEFILENAME, "nofile");
294             GgStringUtils.replaceAll(builder, FILESIZE, "0");
295         }
296         GgStringUtils.replaceAll(builder, ORIGINALFULLPATH, session.getRunner()
297                 .getOriginalFilename());
298         GgStringUtils.replaceAll(builder, ORIGINALFILENAME, R66File.getBasename(session.getRunner()
299                 .getOriginalFilename()));
300         GgStringUtils.replaceAll(builder, RULE, session.getRunner()
301                 .getRuleId());
302         DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
303         Date date = new Date();
304         GgStringUtils.replaceAll(builder, DATE, dateFormat.format(date));
305         dateFormat = new SimpleDateFormat("HHmmss");
306         GgStringUtils.replaceAll(builder, HOUR, dateFormat.format(date));
307         if (session.getAuth() != null) {
308             GgStringUtils.replaceAll(builder, REMOTEHOST, session.getAuth().getUser());
309             try {
310                 GgStringUtils.replaceAll(builder, LOCALHOST,
311                         Configuration.configuration.getHostId(session.getAuth().isSsl()));
312             } catch (OpenR66ProtocolNoSslException e) {
313                 // replace by standard name
314                 GgStringUtils.replaceAll(builder, LOCALHOST,
315                         Configuration.configuration.HOST_ID);
316             }
317         }
318         if (session.getRemoteAddress() != null) {
319             GgStringUtils.replaceAll(builder, REMOTEHOSTADDR, session.getRemoteAddress().toString());
320             GgStringUtils.replaceAll(builder, LOCALHOSTADDR, session.getLocalAddress().toString());
321         } else {
322             GgStringUtils.replaceAll(builder, REMOTEHOSTADDR, "unknown");
323             GgStringUtils.replaceAll(builder, LOCALHOSTADDR, "unknown");
324         }
325         GgStringUtils.replaceAll(builder, TRANSFERID, Long.toString(session
326                 .getRunner().getSpecialId()));
327         String requester = session.getRunner().getRequester();
328         GgStringUtils.replaceAll(builder, REQUESTERHOST, requester);
329         String requested = session.getRunner().getRequested();
330         GgStringUtils.replaceAll(builder, REQUESTEDHOST, requested);
331         GgStringUtils.replaceAll(builder, FULLTRANSFERID, session
332                 .getRunner().getSpecialId()+"_"+requester+"_"+requested);
333         GgStringUtils.replaceAll(builder, RANKTRANSFER, Integer.toString(session
334                 .getRunner().getRank()));
335         GgStringUtils.replaceAll(builder, BLOCKSIZE, Integer.toString(session
336                 .getBlockSize()));
337         R66Dir dir = new R66Dir(session);
338         if (session.getRunner().isRecvThrough() || session.getRunner().isSendThrough()) {
339             try {
340                 dir.changeDirectoryNotChecked(session.getRunner().getRule().recvPath);
341                 GgStringUtils.replaceAll(builder, INPATH, dir.getFullPath());
342             } catch (CommandAbstractException e) {
343             }
344             dir = new R66Dir(session);
345             try {
346                 dir.changeDirectoryNotChecked(session.getRunner().getRule().sendPath);
347                 GgStringUtils.replaceAll(builder, OUTPATH, dir.getFullPath());
348             } catch (CommandAbstractException e) {
349             }
350             dir = new R66Dir(session);
351             try {
352                 dir.changeDirectoryNotChecked(session.getRunner().getRule().workPath);
353                 GgStringUtils.replaceAll(builder, WORKPATH, dir.getFullPath());
354             } catch (CommandAbstractException e) {
355             }
356             dir = new R66Dir(session);
357             try {
358                 dir.changeDirectoryNotChecked(session.getRunner().getRule().archivePath);
359                 GgStringUtils.replaceAll(builder, ARCHPATH, dir.getFullPath());
360             } catch (CommandAbstractException e) {
361             }
362         } else {
363             try {
364                 dir.changeDirectoryNotChecked(session.getRunner().getRule().recvPath);
365                 GgStringUtils.replaceAll(builder, INPATH, dir.getFullPath());
366             } catch (CommandAbstractException e) {
367             }
368             dir = new R66Dir(session);
369             try {
370                 dir.changeDirectory(session.getRunner().getRule().sendPath);
371                 GgStringUtils.replaceAll(builder, OUTPATH, dir.getFullPath());
372             } catch (CommandAbstractException e) {
373             }
374             dir = new R66Dir(session);
375             try {
376                 dir.changeDirectory(session.getRunner().getRule().workPath);
377                 GgStringUtils.replaceAll(builder, WORKPATH, dir.getFullPath());
378             } catch (CommandAbstractException e) {
379             }
380             dir = new R66Dir(session);
381             try {
382                 dir.changeDirectory(session.getRunner().getRule().archivePath);
383                 GgStringUtils.replaceAll(builder, ARCHPATH, dir.getFullPath());
384             } catch (CommandAbstractException e) {
385             }
386         }
387         GgStringUtils.replaceAll(builder, HOMEPATH, Configuration.configuration.baseDirectory);
388         if (session.getLocalChannelReference() == null) {
389             GgStringUtils.replaceAll(builder, ERRORMSG, "NoError");
390             GgStringUtils.replaceAll(builder, ERRORCODE, "-");
391             GgStringUtils.replaceAll(builder, ERRORSTRCODE, ErrorCode.Unknown.name());
392         } else {
393             try {
394                 GgStringUtils.replaceAll(builder, ERRORMSG, session.getLocalChannelReference().getErrorMessage());
395             } catch (NullPointerException e) {
396                 GgStringUtils.replaceAll(builder, ERRORMSG, "NoError");
397             }
398             try {
399                 GgStringUtils.replaceAll(builder, ERRORCODE, session.getLocalChannelReference().getCurrentCode().getCode());
400             } catch (NullPointerException e) {
401                 GgStringUtils.replaceAll(builder, ERRORCODE, "-");
402             }
403             try {
404                 GgStringUtils.replaceAll(builder, ERRORSTRCODE, session.getLocalChannelReference().getCurrentCode().name());
405             } catch (NullPointerException e) {
406                 GgStringUtils.replaceAll(builder, ERRORSTRCODE, ErrorCode.Unknown.name());
407             }
408         }
409         // finalname
410         if (argFormat != null && argFormat.length > 0)
411             return String.format(builder.toString(), argFormat);
412         return builder.toString();
413     }
414 }