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.util.concurrent.ExecutorService;
27  import java.util.concurrent.Executors;
28  import java.util.concurrent.TimeUnit;
29  
30  import openr66.context.ErrorCode;
31  import openr66.context.R66Result;
32  import openr66.context.R66Session;
33  
34  
35  /**
36   * Execute a Java command through Class.forName call
37   *
38   *
39   * @author Frederic Bregier
40   *
41   */
42  public class ExecJavaTask extends AbstractTask {
43  
44      /**
45       * Internal Logger
46       */
47      private static final GgInternalLogger logger = GgInternalLoggerFactory
48              .getLogger(ExecJavaTask.class);
49  
50      /**
51       * @param argRule
52       * @param delay
53       * @param argTransfer
54       * @param session
55       */
56      public ExecJavaTask(String argRule, int delay, String argTransfer,
57              R66Session session) {
58          super(TaskType.EXECJAVA, delay, argRule, argTransfer, session);
59      }
60  
61      /*
62       * (non-Javadoc)
63       *
64       * @see openr66.context.task.AbstractTask#run()
65       */
66      @Override
67      public void run() {
68          /*
69           * First apply all replacements and format to argRule from context and
70           * argTransfer. Will call exec (from first element of resulting string)
71           * with arguments as the following value from the replacements. Return 0
72           * if OK, else 1 for a warning else as an error. No change should be done
73           * in the FILENAME
74           */
75          logger.debug("Exec with " + argRule + ":" + argTransfer + " and {}",
76                  session);
77          String finalname = argRule;
78          if (argTransfer != null) {
79              finalname = getReplacedValue(finalname, argTransfer.split(" "));
80          }
81          // First get the Class Name
82          String[] args = finalname.split(" ");
83          String className = args[0];
84          R66Runnable runnable = null;
85          try {
86              runnable = (R66Runnable) Class.forName(className).newInstance();
87          } catch (Exception e) {
88              logger.error("ExecJava command is not available: " + className, e);
89              R66Result result = new R66Result(session, false,
90                      ErrorCode.CommandNotFound, session.getRunner());
91              futureCompletion.setResult(result);
92              futureCompletion.cancel();
93              return;
94          }
95          runnable.setArgs(this.session, this.waitForValidation, this.useLocalExec, 
96                  this.delay, args);
97          logger.debug(className+" "+runnable.getClass().getName());
98          if (! waitForValidation) {
99              // Do not wait for validation
100             futureCompletion.setSuccess();
101             logger.info("Exec will start but no WAIT with {}", runnable);
102         }
103         int status = -1;
104         if (waitForValidation && delay <= 0) {
105             runnable.run();
106             status = runnable.getFinalStatus();
107         } else {
108             ExecutorService executorService = Executors.newFixedThreadPool(1);
109             executorService.execute(runnable);
110             try {
111                 Thread.yield();
112                 executorService.shutdown();
113                 if (waitForValidation) {
114                     if (delay > 0) {
115                         if (! executorService.awaitTermination(delay, TimeUnit.MILLISECONDS)) {
116                             executorService.shutdownNow();
117                             logger.error("Exec is in Time Out");
118                             status = -1;
119                         } else {
120                             status = runnable.getFinalStatus();
121                         }
122                     } else {
123                         while (!executorService.awaitTermination(30, TimeUnit.SECONDS)) ;
124                         status = runnable.getFinalStatus();
125                     }
126                 } else {
127                     while (!executorService.awaitTermination(30, TimeUnit.SECONDS)) ;
128                     status = runnable.getFinalStatus();
129                 }
130             } catch (InterruptedException e) {
131                 logger.error("Status: " + e.getMessage() + "\n\t Exec in error with " +
132                         runnable);
133                 if (waitForValidation) {
134                     futureCompletion.cancel();
135                 }
136                 return;
137             }
138         }
139         if (status == 0) {
140             if (waitForValidation) {
141                 futureCompletion.setSuccess();
142             }
143             logger.info("Exec OK with {}", runnable);
144         } else if (status == 1) {
145             logger.warn("Exec in warning with " + runnable);
146             if (waitForValidation) {
147                 futureCompletion.setSuccess();
148             }
149         } else {
150             logger.error("Status: " + status + " Exec in error with " +
151                     runnable);
152             if (waitForValidation) {
153                 futureCompletion.cancel();
154             }
155         }
156     }
157 
158 }