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.file.AbstractDir;
23  import org.waarp.common.file.FileUtils;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.common.utility.ParametersChecker;
27  import org.waarp.common.utility.WaarpStringUtils;
28  import org.waarp.openr66.context.R66Session;
29  import org.waarp.openr66.context.task.exception.OpenR66RunnerException;
30  
31  import java.io.File;
32  import java.io.FileOutputStream;
33  import java.io.IOException;
34  
35  /**
36   * This task add 1 byte to empty file if the current file is empty (0
37   * length).<br>
38   * <br>
39   * <p>
40   * The task will be in error only if the file is of length 0 but cannot be
41   * unzeroed.<br>
42   * The content of PATH, if not empty, will be the content when unzeroed. If
43   * empty, the 'blank' character will
44   * be used.<br>
45   * <p>
46   * delay >= 1 will make a log using info level for 1, warn level for 2.
47   */
48  public class UnzeroedFileTask extends AbstractTask {
49    /**
50     * Internal Logger
51     */
52    private static final WaarpLogger logger =
53        WaarpLoggerFactory.getLogger(UnzeroedFileTask.class);
54  
55    /**
56     * @param argRule
57     * @param delay
58     * @param argTransfer
59     * @param session
60     */
61    public UnzeroedFileTask(final String argRule, final int delay,
62                            final String argTransfer, final R66Session session) {
63      super(TaskType.UNZEROED, delay, argRule, argTransfer, session);
64    }
65  
66    @Override
67    public final void run() {
68      final File currentFile = session.getFile().getTrueFile();
69      final String toWrite = ParametersChecker.isEmpty(argRule)? " " : argRule;
70      final String curpath =
71          AbstractDir.normalizePath(currentFile.getAbsolutePath());
72      if (currentFile.exists() && currentFile.length() == 0) {
73        FileOutputStream out = null;
74        try {
75          out = new FileOutputStream(currentFile);
76          out.write(toWrite.getBytes(WaarpStringUtils.UTF8));
77          if (delay > 0) {
78            if (delay > 1) {
79              logger.warn("Unzeroed File: " + curpath + " from " + session);
80            } else {
81              logger.info("Unzeroed File: {} from {}", curpath, session);
82            }
83          }
84          futureCompletion.setSuccess();
85        } catch (final IOException e) {
86          logger.error("Cannot unzeroed File: " + curpath + " from " + session);
87          futureCompletion.setFailure(
88              new OpenR66RunnerException("File not Unzeroed"));
89        } finally {
90          FileUtils.close(out);
91        }
92        return;
93      }
94      if (delay > 0) {
95        if (delay > 1) {
96          logger.warn(
97              "Unzeroed File not applicable to " + curpath + " from " + session);
98        } else {
99          logger.info("Unzeroed File not applicable to {} from {}", curpath,
100                     session);
101       }
102     }
103     futureCompletion.setSuccess();
104   }
105 
106 }