1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.context.task;
21
22 import org.apache.commons.exec.CommandLine;
23 import org.waarp.common.json.JsonHandler;
24 import org.waarp.common.logging.WaarpLogger;
25 import org.waarp.common.logging.WaarpLoggerFactory;
26 import org.waarp.common.utility.ParametersChecker;
27 import org.waarp.openr66.client.SubmitTransfer;
28 import org.waarp.openr66.client.TransferArgs;
29 import org.waarp.openr66.context.R66Session;
30 import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
31 import org.waarp.openr66.database.DbConstantR66;
32 import org.waarp.openr66.database.data.DbTaskRunner;
33 import org.waarp.openr66.protocol.utils.R66Future;
34
35 import java.util.HashMap;
36 import java.util.Map;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class TransferTask extends AbstractTask {
76
77
78
79 private static final WaarpLogger logger =
80 WaarpLoggerFactory.getLogger(TransferTask.class);
81
82
83
84
85
86
87
88 public TransferTask(final String argRule, final int delay,
89 final String argTransfer, final R66Session session) {
90 super(TaskType.TRANSFER, delay, argRule, argTransfer, session);
91 }
92
93 @Override
94 public final void run() {
95 logger.info("Transfer with {}}:{} and {}", argRule, argTransfer, session);
96 String finalname = applyTransferSubstitutions(argRule);
97
98 finalname = finalname.replaceAll("#([A-Z]+)#", "\\${$1}");
99 final CommandLine commandLine = new CommandLine("dummy");
100 commandLine.setSubstitutionMap(getSubstitutionMap());
101 commandLine.addArguments(finalname, false);
102 final String[] args = commandLine.getArguments();
103
104 if (args.length < 6) {
105 futureCompletion.setFailure(
106 new OpenR66RunnerErrorException("Not enough argument in Transfer"));
107 return;
108 }
109 final TransferArgs transferArgs =
110 TransferArgs.getParamsInternal(0, args, false);
111 final DbTaskRunner taskRunner = session.getRunner();
112 if (transferArgs != null) {
113 String copied = null;
114 for (final String arg : args) {
115 if ("-copyinfo".equalsIgnoreCase(arg)) {
116 copied = taskRunner.getFileInformation();
117 break;
118 }
119 }
120
121
122 final String follow = taskRunner.getFollowId();
123 if (copied == null && ParametersChecker.isNotEmpty(follow) &&
124 !transferArgs.getTransferInfo()
125 .contains(TransferArgs.FOLLOW_JSON_KEY)) {
126 transferArgs.setFollowId(follow);
127 final Map<String, Long> map = new HashMap<String, Long>();
128 map.put(TransferArgs.FOLLOW_JSON_KEY, Long.parseLong(follow));
129 copied = JsonHandler.writeAsStringEscaped(map);
130 }
131 TransferArgs.getAllInfo(transferArgs, 0, args, copied);
132 } else {
133 logger.error("Not enough argument in Transfer");
134 futureCompletion.setFailure(
135 new OpenR66RunnerErrorException("Not enough argument in Transfer"));
136 return;
137 }
138 logger.debug("TransferArgs: {} Rule: {} Filename: {}",
139 transferArgs.getTransferInfo(), transferArgs.getRulename(),
140 transferArgs.getFilename());
141 final R66Future future = new R66Future(true);
142 final SubmitTransfer transaction =
143 new SubmitTransfer(future, transferArgs.getRemoteHost(),
144 transferArgs.getFilename(),
145 transferArgs.getRulename(),
146 transferArgs.getTransferInfo(), transferArgs.isMD5(),
147 transferArgs.getBlockSize(),
148 DbConstantR66.ILLEGALVALUE,
149 transferArgs.getStartTime());
150 transaction.run();
151 future.awaitOrInterruptible();
152 final DbTaskRunner runner;
153 if (future.isSuccess()) {
154 futureCompletion.setResult(future.getResult());
155 runner = future.getResult().getRunner();
156 if (logger.isInfoEnabled()) {
157 logger.info(
158 "Prepare transfer in SUCCESS " + runner.toShortString() +
159 " <REMOTE>" + transferArgs.getRemoteHost() + "</REMOTE>");
160 }
161 futureCompletion.setSuccess();
162 } else {
163 if (future.getResult() != null) {
164 futureCompletion.setResult(future.getResult());
165 runner = future.getResult().getRunner();
166 } else {
167 runner = null;
168 }
169 if (runner != null) {
170 if (future.getCause() == null) {
171 futureCompletion.cancel();
172 } else {
173 futureCompletion.setFailure(future.getCause());
174 }
175 logger.error(
176 "Prepare transfer in FAILURE " + runner.toShortString() +
177 " <REMOTE>" + transferArgs.getRemoteHost() + "</REMOTE>",
178 future.getCause());
179 } else {
180 if (future.getCause() == null) {
181 futureCompletion.cancel();
182 } else {
183 futureCompletion.setFailure(future.getCause());
184 }
185 logger.error("Prepare transfer in FAILURE without any runner back",
186 future.getCause());
187 }
188 }
189 }
190
191 }