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  package org.waarp.openr66.context.task.javatask;
21  
22  import org.waarp.common.database.exception.WaarpDatabaseException;
23  import org.waarp.common.guid.GUID;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.openr66.context.task.AbstractExecJavaTask;
27  
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  /**
32   * Add an UUID in the TransferInformation to the current Task.</br>
33   * This should be called on caller side in pre-task since the transfer
34   * information will be transfered just
35   * after.</br>
36   * The second argument is -1 = added in front, +1 = added at last, default
37   * being
38   * -1.</br>
39   * The third argument, optional, is "-format" followed by a string containing
40   * "#UUID#" to be replaced by the
41   * uuid and starting with - or +, meaning this will be added at the beginning
42   * or
43   * the end of the generated new
44   * string. Default is equivalent to "-format -##UUID##". </br>
45   * To be called as: <task><type>EXECJAVA</type><path>org.waarp.openr66.context.task.javatask.AddUuidJavaTask
46   * [-format (-/+)##UUID##]</path></task>
47   */
48  public class AddUuidJavaTask extends AbstractExecJavaTask {
49    /**
50     * Internal Logger
51     */
52    private static final WaarpLogger logger =
53        WaarpLoggerFactory.getLogger(AddUuidJavaTask.class);
54    private static final String S_UUID = "#UUID#";
55    private static final Pattern UUID_COMPILE =
56        Pattern.compile(S_UUID, Pattern.LITERAL);
57  
58    @Override
59    public final void run() {
60      logger.debug("{}", this);
61      final GUID uuid = new GUID();
62      final String[] args = BLANK.split(fullarg);
63      final String fileInfo;
64      String format = "-##UUID##";
65      int way = -1;
66      for (int i = 0; i < args.length; i++) {
67        if ("-format".equalsIgnoreCase(args[i])) {
68          format = args[i + 1];
69          if (format.charAt(0) == '-') {
70            way = -1;
71            format = format.substring(1);
72          } else if (format.charAt(0) == '+') {
73            way = 1;
74            format = format.substring(1);
75          }
76          i++;
77        }
78      }
79      logger.debug("Replace UUID in {} way: {}", format, way);
80      if (format.isEmpty()) {
81        fileInfo = session.getRunner().getFileInformation();
82      } else {
83        if (way < 0) {
84          fileInfo = UUID_COMPILE.matcher(format).replaceAll(
85              Matcher.quoteReplacement(uuid.toString())) + ' ' +
86                     session.getRunner().getFileInformation();
87        } else {
88          fileInfo = session.getRunner().getFileInformation() + ' ' +
89                     UUID_COMPILE.matcher(format).replaceAll(
90                         Matcher.quoteReplacement(uuid.toString()));
91        }
92      }
93      session.getRunner().setFileInformation(fileInfo);
94      session.getRunner().addToTransferMap("uuid", uuid.toString());
95      try {
96        session.getRunner().update();
97      } catch (final WaarpDatabaseException e) {
98        logger.error("UUID cannot be saved to fileInformation:" + fileInfo);
99        status = 2;
100       return;
101     }
102     logger.debug("UUID saved to fileInformation: {}", fileInfo);
103     status = 0;
104   }
105 }