View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.context.task;
22  
23  import goldengate.common.logging.GgInternalLogger;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.common.tar.TarUtility;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import openr66.context.R66Session;
33  import openr66.protocol.exception.OpenR66ProtocolSystemException;
34  
35  /**
36   * TAR task
37   * @author Frederic Bregier
38   *
39   */
40  public class TarTask extends AbstractTask {
41      /**
42       * Internal Logger
43       */
44      private static final GgInternalLogger logger = GgInternalLoggerFactory
45              .getLogger(TarTask.class);
46  
47      /**
48       * @param argRule
49       * @param delay
50       * @param argTransfer
51       * @param session
52       */
53      public TarTask(String argRule, int delay, String argTransfer,
54              R66Session session) {
55          super(TaskType.TAR, delay, argRule, argTransfer, session);
56      }
57  
58      /*
59       * (non-Javadoc)
60       *
61       * @see openr66.context.task.AbstractTask#run()
62       */
63      @Override
64      public void run() {
65          logger.info("TAR with " + argRule + ":" + argTransfer + ":" + delay + " and {}",
66                  session);
67          String finalname = argRule;
68          finalname = getReplacedValue(finalname, argTransfer.split(" "));
69          boolean tar = false;
70          if ((delay>2)) {
71              if (delay > 3) {
72                  // list of files: tar finalname where finalname="target file1 file2..."
73                  String []args = finalname.split(" ");
74                  List<File> files = new ArrayList<File>(args.length-1);
75                  for (int i = 1; i < args.length; i++) {
76                      files.add(new File(args[i]));
77                  }
78                  tar = TarUtility.createTarFromFiles(files, args[0]);
79              } else {
80                  // directory: tar finalname where finalname="target directory"
81                  String []args = finalname.split(" ");
82                  tar = TarUtility.createTarFromDirectory(args[1], args[0], false);
83              }
84          } else {
85              // untar
86              // directory: untar finalname where finalname="source directory"
87              String []args = finalname.split(" ");
88              File tarFile = new File(args[0]);
89              File directory = new File(args[1]);
90              try {
91                  TarUtility.unTar(tarFile, directory);
92              } catch (IOException e) {
93                  logger.warn("Error while untar",e);
94                  tar = false;
95              }            
96          }
97          if (!tar) {
98              logger.error("Tar error with " + argRule + ":" + argTransfer + ":" + delay +  " and " +
99                      session);
100             futureCompletion.setFailure(new OpenR66ProtocolSystemException("Tar error"));
101             return;
102         }
103         futureCompletion.setSuccess();
104     }
105 
106 }