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.localexec;
22
23 import goldengate.commandexec.client.LocalExecClientHandler;
24 import goldengate.commandexec.client.LocalExecClientPipelineFactory;
25 import goldengate.commandexec.utils.LocalExecResult;
26 import goldengate.common.future.GgFuture;
27 import goldengate.common.logging.GgInternalLogger;
28 import goldengate.common.logging.GgInternalLoggerFactory;
29
30 import java.net.InetSocketAddress;
31
32 import openr66.protocol.configuration.Configuration;
33
34 import org.jboss.netty.bootstrap.ClientBootstrap;
35 import org.jboss.netty.channel.Channel;
36 import org.jboss.netty.channel.ChannelFuture;
37 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
38
39
40
41
42
43
44
45 public class LocalExecClient {
46
47
48
49 private static final GgInternalLogger logger = GgInternalLoggerFactory
50 .getLogger(LocalExecClient.class);
51
52 static public InetSocketAddress address;
53
54 static private ClientBootstrap bootstrapLocalExec;
55
56 static private LocalExecClientPipelineFactory localExecClientPipelineFactory;
57
58
59
60
61 public static void initialize() {
62
63 bootstrapLocalExec = new ClientBootstrap(
64 new NioClientSocketChannelFactory(
65 Configuration.configuration.getLocalPipelineExecutor(),
66 Configuration.configuration.getLocalPipelineExecutor()));
67
68 localExecClientPipelineFactory =
69 new LocalExecClientPipelineFactory();
70 bootstrapLocalExec.setPipelineFactory(localExecClientPipelineFactory);
71 }
72
73
74
75
76 public static void releaseResources() {
77 if (bootstrapLocalExec == null) {
78 return;
79 }
80
81 bootstrapLocalExec.releaseExternalResources();
82 localExecClientPipelineFactory.releaseResources();
83 }
84
85 private Channel channel;
86 private LocalExecResult result;
87
88 public LocalExecClient() {
89
90 }
91
92 public LocalExecResult getLocalExecResult() {
93 return result;
94 }
95
96
97
98
99
100
101
102
103 public void runOneCommand(String command, long delay, boolean waitFor, GgFuture futureCompletion) {
104
105 LocalExecClientHandler clientHandler =
106 (LocalExecClientHandler) channel.getPipeline().getLast();
107 clientHandler.initExecClient();
108
109
110 ChannelFuture lastWriteFuture = null;
111 String line = delay+" "+command+"\n";
112
113
114 lastWriteFuture = channel.write(line);
115 if (!waitFor) {
116 futureCompletion.setSuccess();
117 logger.info("Exec OK with {}", command);
118 }
119
120 if (lastWriteFuture != null) {
121 if (delay <= 0) {
122 try {
123 lastWriteFuture.await();
124 } catch (InterruptedException e) {
125 }
126 } else {
127 try {
128 lastWriteFuture.await(delay);
129 } catch (InterruptedException e) {
130 }
131 }
132 }
133
134 LocalExecResult localExecResult = clientHandler.waitFor(delay*2);
135 result = localExecResult;
136 if (futureCompletion == null) {
137 return;
138 }
139 if (result.status == 0) {
140 if (waitFor) {
141 futureCompletion.setSuccess();
142 }
143 logger.info("Exec OK with {}", command);
144 } else if (result.status == 1) {
145 logger.warn("Exec in warning with {}", command);
146 if (waitFor) {
147 futureCompletion.setSuccess();
148 }
149 } else {
150 logger.error("Status: " + result.status + " Exec in error with " +
151 command+"\n"+result.result);
152 if (waitFor) {
153 futureCompletion.cancel();
154 }
155 }
156 }
157
158
159
160
161 public boolean connect() {
162
163 ChannelFuture future = bootstrapLocalExec.connect(address);
164
165
166 try {
167 channel = future.await().getChannel();
168 } catch (InterruptedException e) {
169 }
170 if (!future.isSuccess()) {
171 logger.error("Client Not Connected", future.getCause());
172 return false;
173 }
174 return true;
175 }
176
177
178
179 public void disconnect() {
180
181
182 try {
183 channel.close().await();
184 } catch (InterruptedException e) {
185 }
186 }
187 }