View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, 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 Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.context.task;
22  
23  import org.apache.commons.exec.CommandLine;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.openr66.context.R66Session;
27  import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
28  import org.waarp.openr66.s3.taskfactory.S3TaskFactory.S3TaskType;
29  
30  /**
31   * Abstract Class for S3 Tasks<br>
32   * <p>
33   * Arguments will comme from both Rule argument and Transfer information,
34   * using substitution such that arguments can be passed through transfer
35   * and not set statically into rule.</p>
36   */
37  public abstract class S3AbstractTask extends AbstractTask {
38    static final WaarpLogger logger =
39        WaarpLoggerFactory.getLogger(S3AbstractTask.class);
40  
41    S3TaskArgs taskUtil = null;
42  
43    /**
44     * Constructor
45     *
46     * @param type
47     * @param delay
48     * @param argRule
49     * @param argTransfer
50     * @param session
51     */
52    S3AbstractTask(final TaskType type, final int delay, final String argRule,
53                   final String argTransfer, final R66Session session) {
54      super(type, delay, argRule, argTransfer, session);
55    }
56  
57    public final boolean getParams() {
58      logger.info("Transfer with {}:{} and {}", argRule, argTransfer, session);
59      try {
60        String finalname = applyTransferSubstitutions(argRule);
61  
62        finalname = finalname.replaceAll("#([A-Z]+)#", "\\${$1}");
63        final CommandLine commandLine = new CommandLine("dummy");
64        commandLine.setSubstitutionMap(getSubstitutionMap());
65        commandLine.addArguments(finalname, false);
66        final String[] args = commandLine.getArguments();
67        taskUtil = S3TaskArgs.getS3Params(this.getS3TaskType(), 0, args);
68        return taskUtil != null;
69      } catch (final Exception e) {
70        finalizeInError(e, "Not enough argument in Transfer");
71        return false;
72      }
73    }
74  
75    protected final void finalizeInError(final Exception e,
76                                         final String message) {
77      logger.error("{} {}: {}", getS3TaskType().name(), message, e.getMessage());
78      futureCompletion.setFailure(
79          new OpenR66RunnerErrorException(getS3TaskType() + " " + message, e));
80    }
81  
82    public abstract S3TaskType getS3TaskType();
83  
84  }