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.FileUtils;
23  import org.waarp.common.logging.WaarpLogLevel;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.common.utility.WaarpStringUtils;
27  import org.waarp.openr66.context.ErrorCode;
28  import org.waarp.openr66.context.R66Session;
29  
30  import java.io.File;
31  import java.io.FileNotFoundException;
32  import java.io.FileOutputStream;
33  import java.io.IOException;
34  
35  /**
36   * This class is for logging or write to an external file some info:<br>
37   * - if delay is 0, no echo at all will be done<br>
38   * - if delay is 1, will echo some information in the normal log<br>
39   * - if delay is 2, will echo some information in the file (last deduced
40   * argument will be the full path for
41   * the file output)<br>
42   * - if delay is 3, will echo both in the normal log and in the file (last
43   * deduced argument will be the full
44   * path for the file output)<br>
45   * <br>
46   * If first word for logging is one of debug, info, warn, error, then the
47   * corresponding level of log will be
48   * used.
49   */
50  public class LogTask extends AbstractTask {
51    private static final String SPACES = "     ";
52    /**
53     * Internal Logger
54     */
55    private static final WaarpLogger logger =
56        WaarpLoggerFactory.getLogger(LogTask.class);
57  
58    /**
59     * @param argRule
60     * @param delay
61     * @param argTransfer
62     * @param session
63     */
64    public LogTask(final String argRule, final int delay,
65                   final String argTransfer, final R66Session session) {
66      super(TaskType.LOG, delay, argRule, argTransfer, session);
67    }
68  
69    @Override
70    public void run() {
71      String finalValue = argRule;
72      finalValue = getReplacedValue(finalValue, BLANK.split(argTransfer));
73      final String tempValue = finalValue.toUpperCase();
74      WaarpLogLevel finalLevel = WaarpLogLevel.WARN;
75      for (final WaarpLogLevel level : WaarpLogLevel.values()) {
76        if (tempValue.startsWith(level.name())) {
77          finalLevel = level;
78          break;
79        }
80      }
81      switch (delay) {
82        case 0:
83          break;
84        case 1:
85          logger.log(finalLevel, finalValue + SPACES + session);
86          break;
87        case 3:
88          logger.log(finalLevel, finalValue + SPACES + session);
89          if (runFor2Or3(finalValue, finalLevel)) {
90            return;
91          }
92          break;
93        case 2:
94          if (runFor2Or3(finalValue, finalLevel)) {
95            return;
96          }
97          break;
98        default:
99      }
100     futureCompletion.setSuccess();
101   }
102 
103   private boolean runFor2Or3(final String finalValue,
104                              final WaarpLogLevel finalLevel) {
105     final String[] args = BLANK.split(finalValue);
106     final String filename = args[args.length - 1];
107     final File file = new File(filename);
108     if (file.getParentFile() == null || file.exists() && !file.canWrite()) {
109       // File cannot be written so revert to log
110       session.getRunner().setErrorExecutionStatus(ErrorCode.Warning);
111       if (delay == 2) {
112         logger.log(finalLevel, finalValue + SPACES + session);
113       }
114       futureCompletion.setSuccess();
115       return true;
116     }
117     final FileOutputStream outputStream;
118     try {
119       outputStream = new FileOutputStream(file, true);
120     } catch (final FileNotFoundException e) {
121       // File cannot be written so revert to log
122       session.getRunner().setErrorExecutionStatus(ErrorCode.Warning);
123       if (delay == 2) {
124         logger.log(finalLevel, finalValue + SPACES + session);
125       }
126       futureCompletion.setSuccess();
127       return true;
128     }
129     try {
130       final int len = args.length - 1;
131       for (int i = 0; i < len; i++) {
132         outputStream.write(args[i].getBytes(WaarpStringUtils.UTF8));
133         outputStream.write(' ');
134       }
135       outputStream.write(' ');
136     } catch (final IOException e) {
137       // File cannot be written so revert to log
138       FileUtils.close(outputStream);
139       if (!file.delete()) {
140         logger.info("Cannot delete log file");
141       }
142       session.getRunner().setErrorExecutionStatus(ErrorCode.Warning);
143       if (delay == 2) {
144         logger.log(finalLevel, finalValue + SPACES + session);
145       }
146       futureCompletion.setSuccess();
147       return true;
148     }
149     FileUtils.close(outputStream);
150     return false;
151   }
152 
153 }