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  import java.io.FileNotFoundException;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  
31  import openr66.context.ErrorCode;
32  import openr66.context.R66Session;
33  
34  /**
35   * This class is for logging or write to an external file some info:<br>
36   * - if delay is 0, no echo at all will be done<br>
37   * - if delay is 1, will echo some information in the normal log<br>
38   * - if delay is 2, will echo some information in the file
39   * (last deduced argument will be the full path for the file output)<br>
40   * - if delay is 3, will echo both in the normal log and in the file
41   * (last deduced argument will be the full path for the file output)<br>
42   *
43   * @author Frederic Bregier
44   *
45   */
46  public class LogTask extends AbstractTask {
47      /**
48       * Internal Logger
49       */
50      private static final GgInternalLogger logger = GgInternalLoggerFactory
51              .getLogger(LogTask.class);
52  
53      /**
54       * @param argRule
55       * @param delay
56       * @param argTransfer
57       * @param session
58       */
59      public LogTask(String argRule, int delay, String argTransfer,
60              R66Session session) {
61          super(TaskType.LOG, delay, argRule, argTransfer, session);
62      }
63  
64      /*
65       * (non-Javadoc)
66       *
67       * @see openr66.context.task.AbstractTask#run()
68       */
69      @Override
70      public void run() {
71          String finalValue = argRule;
72          finalValue = getReplacedValue(finalValue, argTransfer.split(" "));
73          switch (delay) {
74              case 0:
75                  break;
76              case 1:
77                  logger.warn(finalValue+"\n    " + session.toString());
78                  break;
79              case 3:
80                  logger.warn(finalValue+"\n    " + session.toString());
81              case 2:
82                  String []args = finalValue.split(" ");
83                  String filename = args[args.length-1];
84                  File file = new File(filename);
85                  if (file.getParentFile() == null ||
86                      (!file.canWrite())) {
87                      // File cannot be written so revert to log
88                      session.getRunner().setErrorExecutionStatus(ErrorCode.Warning);
89                      logger.warn(finalValue+"\n    " + session.toString());
90                      futureCompletion.setSuccess();
91                      return;
92                  }
93                  FileOutputStream outputStream = null;
94                  try {
95                      outputStream = new FileOutputStream(file);
96                  } catch (FileNotFoundException e) {
97                      // File cannot be written so revert to log
98                      session.getRunner().setErrorExecutionStatus(ErrorCode.Warning);
99                      logger.warn(finalValue+"\n    " + session.toString());
100                     futureCompletion.setSuccess();
101                     return;
102                 }
103                 try {
104                     outputStream.write(finalValue.getBytes());
105                 } catch (IOException e) {
106                     // File cannot be written so revert to log
107                     try {
108                         outputStream.close();
109                     } catch (IOException e1) {
110                     }
111                     file.delete();
112                     session.getRunner().setErrorExecutionStatus(ErrorCode.Warning);
113                     logger.warn(finalValue+"\n    " + session.toString());
114                     futureCompletion.setSuccess();
115                     return;
116                 }
117                 try {
118                     outputStream.close();
119                 } catch (IOException e) {
120                 }
121                 break;
122             default:
123         }
124         futureCompletion.setSuccess();
125     }
126 
127 }