1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40
41
42 public class ExecJavaTask extends AbstractTask {
43
44
45
46
47 private static final GgInternalLogger logger = GgInternalLoggerFactory
48 .getLogger(ExecJavaTask.class);
49
50
51
52
53
54
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
63
64
65
66 @Override
67 public void run() {
68
69
70
71
72
73
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
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
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 }