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  
26  import java.io.File;
27  
28  import openr66.context.R66Session;
29  import openr66.protocol.exception.OpenR66ProtocolSystemException;
30  import openr66.protocol.utils.FileUtils;
31  
32  /**
33   * Create a link of the current file and make the file pointing to it.
34   *
35   * The link first tries to be a hard link, then a soft link, and if really not
36   * possible (not supported by the filesystem), it does a copy and rename task.
37   *
38   * @author Frederic Bregier
39   *
40   */
41  public class LinkRenameTask extends AbstractTask {
42      /**
43       * Internal Logger
44       */
45      private static final GgInternalLogger logger = GgInternalLoggerFactory
46              .getLogger(LinkRenameTask.class);
47  
48      /**
49       * @param argRule
50       * @param delay
51       * @param argTransfer
52       * @param session
53       */
54      public LinkRenameTask(String argRule, int delay, String argTransfer,
55              R66Session session) {
56          super(TaskType.LINKRENAME, delay, argRule, argTransfer, session);
57      }
58  
59      /*
60       * (non-Javadoc)
61       *
62       * @see openr66.context.task.AbstractTask#run()
63       */
64      @Override
65      public void run() {
66          String finalname = argRule;
67          finalname = getReplacedValue(finalname, argTransfer.split(" "));
68          logger.info("Move and Rename to " + finalname + " with " + argRule +
69                  ":" + argTransfer + " and {}", session);
70          // First try hard link
71          // FIXME wait for NIO.2 in JDK7 to have such functions, in the meantime only move...
72          File from = session.getFile().getTrueFile();
73          File to = new File(finalname);
74          try {
75              FileUtils.copy(from, to, false, false);
76          } catch (OpenR66ProtocolSystemException e1) {
77              logger.error("Copy and Rename to " + finalname + " with " +
78                      argRule + ":" + argTransfer + " and " + session, e1);
79              futureCompletion.setFailure(new OpenR66ProtocolSystemException(e1));
80              return;
81          }
82          session.getRunner().setFileMoved(finalname, true);
83          futureCompletion.setSuccess();
84      }
85  
86  }