1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package goldengate.ftp.core.utils;
22
23 import goldengate.common.logging.GgInternalLogger;
24 import goldengate.common.logging.GgInternalLoggerFactory;
25 import goldengate.common.logging.GgSlf4JLoggerFactory;
26 import goldengate.ftp.core.config.FtpConfiguration;
27
28 import java.net.InetAddress;
29 import java.net.InetSocketAddress;
30 import java.net.UnknownHostException;
31 import java.util.Iterator;
32 import java.util.Timer;
33
34 import org.jboss.netty.channel.Channel;
35 import org.jboss.netty.channel.ChannelFactory;
36 import org.jboss.netty.channel.Channels;
37 import org.jboss.netty.channel.group.ChannelGroupFuture;
38 import org.jboss.netty.channel.group.ChannelGroupFutureListener;
39 import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
40 import org.slf4j.LoggerFactory;
41
42 import ch.qos.logback.classic.LoggerContext;
43
44
45
46
47
48
49
50 public class FtpChannelUtils implements Runnable {
51
52
53
54 private static final GgInternalLogger logger = GgInternalLoggerFactory
55 .getLogger(FtpChannelUtils.class);
56
57
58
59
60
61
62
63 public static InetAddress getRemoteInetAddress(Channel channel) {
64 InetSocketAddress socketAddress = (InetSocketAddress) channel
65 .getRemoteAddress();
66 if (socketAddress == null) {
67 socketAddress = new InetSocketAddress(20);
68 }
69 return socketAddress.getAddress();
70 }
71
72
73
74
75
76
77
78 public static InetAddress getLocalInetAddress(Channel channel) {
79 InetSocketAddress socketAddress = (InetSocketAddress) channel
80 .getLocalAddress();
81 return socketAddress.getAddress();
82 }
83
84
85
86
87
88
89
90 public static InetSocketAddress getRemoteInetSocketAddress(Channel channel) {
91 return (InetSocketAddress) channel.getRemoteAddress();
92 }
93
94
95
96
97
98
99
100 public static InetSocketAddress getLocalInetSocketAddress(Channel channel) {
101 return (InetSocketAddress) channel.getLocalAddress();
102 }
103
104
105
106
107
108
109
110 public static InetSocketAddress getInetSocketAddress(String arg) {
111 String[] elements = arg.split(",");
112 if (elements.length != 6) {
113 return null;
114 }
115 byte[] address = new byte[4];
116 int[] iElements = new int[6];
117 for (int i = 0; i < 6; i ++) {
118 try {
119 iElements[i] = Integer.parseInt(elements[i]);
120 } catch (NumberFormatException e) {
121 return null;
122 }
123 if (iElements[i] < 0 || iElements[i] > 255) {
124 return null;
125 }
126 }
127 for (int i = 0; i < 4; i ++) {
128 address[i] = (byte) iElements[i];
129 }
130 int port = iElements[4] << 8 | iElements[5];
131 InetAddress inetAddress;
132 try {
133 inetAddress = InetAddress.getByAddress(address);
134 } catch (UnknownHostException e) {
135 return null;
136 }
137 return new InetSocketAddress(inetAddress, port);
138 }
139
140
141
142
143
144
145
146 public static String getAddress(InetSocketAddress address) {
147 InetAddress servAddr = address.getAddress();
148 int servPort = address.getPort();
149 return servAddr.getHostAddress().replace('.', ',') + ',' +
150 (servPort >> 8) + ',' + (servPort & 0xFF);
151 }
152
153
154
155
156
157
158
159
160 public static InetSocketAddress get2428InetSocketAddress(String arg) {
161
162
163 if (arg == null || arg.length() == 0) {
164
165 return null;
166 }
167 String delim = arg.substring(0, 1);
168 String[] infos = arg.split(delim);
169 if (infos.length != 3) {
170
171 return null;
172 }
173 boolean isIPV4 = true;
174 if (infos[0].equals("1")) {
175 isIPV4 = true;
176 } else if (infos[0].equals("2")) {
177 isIPV4 = false;
178 } else {
179
180 return null;
181 }
182 byte[] address = null;
183 if (isIPV4) {
184
185 address = new byte[4];
186 String[] elements = infos[1].split(".");
187 if (elements.length != 4) {
188 return null;
189 }
190 for (int i = 0; i < 4; i ++) {
191 try {
192 address[i] = (byte) Integer.parseInt(elements[i]);
193 } catch (NumberFormatException e) {
194 return null;
195 }
196 }
197 } else {
198
199 address = new byte[16];
200 int[] value = new int[8];
201 String[] elements = infos[1].split(":");
202 if (elements.length != 8) {
203 return null;
204 }
205 for (int i = 0, j = 0; i < 8; i ++) {
206 if (elements[i] == null || elements[i].length() == 0) {
207 value[i] = 0;
208 } else {
209 try {
210 value[i] = Integer.parseInt(elements[i]);
211 } catch (NumberFormatException e) {
212 return null;
213 }
214 }
215 address[j ++] = (byte) (value[i] >> 8);
216 address[j ++] = (byte) (value[i] & 0xFF);
217 }
218 }
219 int port = 0;
220 try {
221 port = Integer.parseInt(infos[2]);
222 } catch (NumberFormatException e) {
223 return null;
224 }
225 InetAddress inetAddress;
226 try {
227 inetAddress = InetAddress.getByAddress(address);
228 } catch (UnknownHostException e) {
229 return null;
230 }
231 return new InetSocketAddress(inetAddress, port);
232 }
233
234
235
236
237
238
239
240 public static String get2428Address(InetSocketAddress address) {
241 InetAddress servAddr = address.getAddress();
242 int servPort = address.getPort();
243 StringBuilder builder = new StringBuilder();
244 String hostaddress = servAddr.getHostAddress();
245 builder.append('|');
246 if (hostaddress.contains(":")) {
247 builder.append('2');
248 } else {
249 builder.append('1');
250 }
251 builder.append('|');
252 builder.append(hostaddress);
253 builder.append('|');
254 builder.append(servPort);
255 builder.append('|');
256 return builder.toString();
257 }
258
259
260
261
262
263
264
265 private static class FtpChannelGroupFutureListener implements
266 ChannelGroupFutureListener {
267 OrderedMemoryAwareThreadPoolExecutor pool;
268
269 ChannelFactory channelFactory;
270
271 ChannelFactory channelFactory2;
272
273 public FtpChannelGroupFutureListener(
274 OrderedMemoryAwareThreadPoolExecutor pool,
275 ChannelFactory channelFactory, ChannelFactory channelFactory2) {
276 this.pool = pool;
277 this.channelFactory = channelFactory;
278 this.channelFactory2 = channelFactory2;
279 }
280
281 public void operationComplete(ChannelGroupFuture future)
282 throws Exception {
283 pool.shutdownNow();
284 channelFactory.releaseExternalResources();
285 if (channelFactory2 != null) {
286 channelFactory2.releaseExternalResources();
287 }
288 }
289 }
290
291
292
293
294
295
296
297 static int terminateCommandChannels(FtpConfiguration configuration) {
298 int result = configuration.getFtpInternalConfiguration()
299 .getCommandChannelGroup().size();
300 configuration.getFtpInternalConfiguration().getCommandChannelGroup()
301 .close().addListener(
302 new FtpChannelGroupFutureListener(configuration
303 .getFtpInternalConfiguration()
304 .getPipelineExecutor(), configuration
305 .getFtpInternalConfiguration()
306 .getCommandChannelFactory(), null));
307 return result;
308 }
309
310
311
312
313
314
315
316 private static int terminateDataChannels(FtpConfiguration configuration) {
317 int result = configuration.getFtpInternalConfiguration()
318 .getDataChannelGroup().size();
319 configuration.getFtpInternalConfiguration().getDataChannelGroup()
320 .close().addListener(
321 new FtpChannelGroupFutureListener(configuration
322 .getFtpInternalConfiguration()
323 .getDataPipelineExecutor(), configuration
324 .getFtpInternalConfiguration()
325 .getDataPassiveChannelFactory(), configuration
326 .getFtpInternalConfiguration()
327 .getDataActiveChannelFactory()));
328 return result;
329 }
330
331
332
333
334
335
336
337 public static int nbCommandChannels(FtpConfiguration configuration) {
338 return configuration.getFtpInternalConfiguration()
339 .getCommandChannelGroup().size();
340 }
341
342
343
344
345
346
347
348 public static int nbDataChannels(FtpConfiguration configuration) {
349 return configuration.getFtpInternalConfiguration()
350 .getDataChannelGroup().size();
351 }
352
353
354
355
356
357
358
359 public static int validCommandChannels(FtpConfiguration configuration) {
360 int result = 0;
361 Channel channel = null;
362 Iterator<Channel> iterator = configuration
363 .getFtpInternalConfiguration().getCommandChannelGroup()
364 .iterator();
365 while (iterator.hasNext()) {
366 channel = iterator.next();
367 if (channel.getParent() != null) {
368
369 if (channel.isConnected()) {
370
371 result ++;
372 } else {
373 Channels.close(channel);
374 }
375 } else {
376
377 result ++;
378 }
379 }
380 return result;
381 }
382
383
384
385
386
387
388 private static void exit(FtpConfiguration configuration) {
389 configuration.isShutdown = true;
390 long delay = configuration.TIMEOUTCON;
391 logger.warn("Exit: Give a delay of " + delay + " ms");
392 configuration.inShutdownProcess();
393 try {
394 Thread.sleep(delay);
395 } catch (InterruptedException e) {
396 }
397 configuration.getFtpInternalConfiguration()
398 .getGlobalTrafficShapingHandler().releaseExternalResources();
399 Timer timer = new Timer(true);
400 FtpTimerTask timerTask = new FtpTimerTask(FtpTimerTask.TIMER_CONTROL);
401 timerTask.configuration = configuration;
402 timer.schedule(timerTask, configuration.TIMEOUTCON/2);
403 configuration.releaseResources();
404 logger.info("Exit Shutdown Data");
405 terminateDataChannels(configuration);
406 logger.warn("Exit end of Data Shutdown");
407 stopLogger();
408 }
409
410
411
412
413
414
415
416 public static void teminateServer(FtpConfiguration configuration) {
417 FtpSignalHandler.terminate(true, configuration);
418 }
419
420
421
422
423
424
425
426 public static void addCommandChannel(Channel channel,
427 FtpConfiguration configuration) {
428
429 configuration.getFtpInternalConfiguration().getCommandChannelGroup()
430 .add(channel);
431 }
432
433
434
435
436
437
438
439 public static void addDataChannel(Channel channel,
440 FtpConfiguration configuration) {
441
442 configuration.getFtpInternalConfiguration().getDataChannelGroup().add(
443 channel);
444 }
445
446
447
448
449 private FtpConfiguration configuration;
450
451 public FtpChannelUtils(FtpConfiguration configuration) {
452 this.configuration = configuration;
453 }
454
455 @Override
456 public void run() {
457 exit(configuration);
458 }
459 public static void stopLogger() {
460 if (GgInternalLoggerFactory.getDefaultFactory() instanceof GgSlf4JLoggerFactory) {
461 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
462 lc.stop();
463 }
464 }
465
466 }