1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43
44
45 public abstract class AbstractTask implements Runnable {
46
47
48
49 public static final String TRUEFULLPATH = "#TRUEFULLPATH#";
50
51
52
53
54 public static final String TRUEFILENAME = "#TRUEFILENAME#";
55
56
57
58 public static final String ORIGINALFULLPATH = "#ORIGINALFULLPATH#";
59
60
61
62
63 public static final String ORIGINALFILENAME = "#ORIGINALFILENAME#";
64
65
66
67
68 public static final String FILESIZE = "#FILESIZE#";
69
70
71
72
73 public static final String RULE = "#RULE#";
74
75
76
77
78 public static final String DATE = "#DATE#";
79
80
81
82
83 public static final String HOUR = "#HOUR#";
84
85
86
87
88 public static final String REMOTEHOST = "#REMOTEHOST#";
89
90
91
92
93 public static final String REMOTEHOSTADDR = "#REMOTEHOSTADDR#";
94
95
96
97
98 public static final String LOCALHOST = "#LOCALHOST#";
99
100
101
102
103 public static final String LOCALHOSTADDR = "#LOCALHOSTADDR#";
104
105
106
107
108 public static final String TRANSFERID = "#TRANSFERID#";
109
110
111
112
113 public static final String REQUESTERHOST = "#REQUESTERHOST#";
114
115
116
117
118 public static final String REQUESTEDHOST = "#REQUESTEDHOST#";
119
120
121
122
123 public static final String FULLTRANSFERID = "#FULLTRANSFERID#";
124
125
126
127
128 public static final String RANKTRANSFER = "#RANKTRANSFER#";
129
130
131
132
133 public static final String BLOCKSIZE = "#BLOCKSIZE#";
134
135
136
137
138 public static final String INPATH = "#INPATH#";
139
140
141
142
143 public static final String OUTPATH = "#OUTPATH#";
144
145
146
147
148 public static final String WORKPATH = "#WORKPATH#";
149
150
151
152
153 public static final String ARCHPATH = "#ARCHPATH#";
154
155
156
157
158 public static final String HOMEPATH = "#HOMEPATH#";
159
160
161
162 public static final String ERRORMSG = "#ERRORMSG#";
163
164
165
166 public static final String ERRORCODE = "#ERRORCODE#";
167
168
169
170 public static final String ERRORSTRCODE = "#ERRORSTRCODE#";
171
172
173
174 public static final String NOWAIT = "#NOWAIT#";
175
176
177
178
179 public static final String LOCALEXEC = "#LOCALEXEC#";
180
181
182
183
184 final TaskType type;
185
186
187
188
189 final String argRule;
190
191
192
193
194 final int delay;
195
196
197
198
199 final String argTransfer;
200
201
202
203
204 final R66Session session;
205
206
207
208
209 final R66Future futureCompletion;
210
211
212
213
214 boolean waitForValidation = true;
215
216
217
218
219 boolean useLocalExec = false;
220
221
222
223
224
225
226
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
240
241 abstract public void run();
242
243
244
245
246
247 public boolean isSuccess() {
248 futureCompletion.awaitUninterruptibly();
249 return futureCompletion.isSuccess();
250 }
251
252
253
254
255
256 public R66Future getFutureCompletion() {
257 return futureCompletion;
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271 protected String getReplacedValue(String arg, Object[] argFormat) {
272 StringBuilder builder = new StringBuilder(arg);
273
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
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
410 if (argFormat != null && argFormat.length > 0)
411 return String.format(builder.toString(), argFormat);
412 return builder.toString();
413 }
414 }