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;
21  
22  import org.waarp.common.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  import org.waarp.common.tar.TarUtility;
25  import org.waarp.openr66.context.R66Session;
26  import org.waarp.openr66.protocol.exception.OpenR66ProtocolSystemException;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * TAR task
35   */
36  public class TarTask extends AbstractTask {
37    /**
38     * Internal Logger
39     */
40    private static final WaarpLogger logger =
41        WaarpLoggerFactory.getLogger(TarTask.class);
42  
43    /**
44     * @param argRule
45     * @param delay
46     * @param argTransfer
47     * @param session
48     */
49    public TarTask(final String argRule, final int delay,
50                   final String argTransfer, final R66Session session) {
51      super(TaskType.TAR, delay, argRule, argTransfer, session);
52    }
53  
54    @Override
55    public void run() {
56      logger.info("TAR with {}:{}:{} and {}", argRule, argTransfer, delay,
57                  session);
58      String finalname = argRule;
59      finalname = getReplacedValue(finalname, argTransfer == null? null :
60          argTransfer.split(" "));
61      boolean tar;
62      switch (delay) {
63        case 2: {
64          // directory: tar finalname where finalname="target directory"
65          final String[] args = finalname.split(" ");
66          tar = TarUtility.createTarFromDirectory(args[1], args[0], true);
67          break;
68        }
69        case 3: {
70          // list of files: tar finalname where finalname="target file1 file2..."
71          final String[] args = finalname.split(" ");
72          final List<File> files = new ArrayList<File>(args.length - 1);
73          for (int i = 1; i < args.length; i++) {
74            files.add(new File(args[i]));
75          }
76          tar = TarUtility.createTarFromFiles(files, args[0]);
77          break;
78        }
79        default:
80          // untar
81          // directory: untar finalname where finalname="source directory"
82          final String[] args = finalname.split(" ");
83          final File tarFile = new File(args[0]);
84          final File directory = new File(args[1]);
85          try {
86            TarUtility.unTar(tarFile, directory);
87            tar = true;
88          } catch (final IOException e) {
89            logger.warn("Error while untar: {}", e.getMessage());
90            tar = false;
91          }
92          break;
93      }
94      if (!tar) {
95        logger.error(
96            "Tar error with " + argRule + ':' + argTransfer + ':' + delay +
97            " and " + session);
98        futureCompletion.setFailure(
99            new OpenR66ProtocolSystemException("Tar error"));
100       return;
101     }
102     futureCompletion.setSuccess();
103   }
104 
105 }