1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.server;
22
23 import goldengate.common.digest.FilesystemBasedDigest;
24 import goldengate.common.logging.GgInternalLogger;
25 import goldengate.common.logging.GgInternalLoggerFactory;
26 import goldengate.common.logging.GgSlf4JLoggerFactory;
27
28 import java.net.SocketAddress;
29
30 import openr66.configuration.FileBasedConfiguration;
31 import openr66.context.ErrorCode;
32 import openr66.context.R66FiniteDualStates;
33 import openr66.context.R66Result;
34 import openr66.database.DbConstant;
35 import openr66.database.data.DbHostAuth;
36 import openr66.protocol.configuration.Configuration;
37 import openr66.protocol.exception.OpenR66ProtocolPacketException;
38 import openr66.protocol.localhandler.LocalChannelReference;
39 import openr66.protocol.localhandler.packet.LocalPacketFactory;
40 import openr66.protocol.localhandler.packet.ShutdownPacket;
41 import openr66.protocol.localhandler.packet.ValidPacket;
42 import openr66.protocol.networkhandler.NetworkTransaction;
43 import openr66.protocol.utils.ChannelUtils;
44
45 import org.jboss.netty.logging.InternalLoggerFactory;
46
47
48
49
50
51
52 public class ServerShutdown {
53
54
55
56
57
58 public static void main(String[] args)
59 throws OpenR66ProtocolPacketException {
60 InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
61 final GgInternalLogger logger = GgInternalLoggerFactory
62 .getLogger(ServerShutdown.class);
63 if (args.length < 1) {
64 logger
65 .error("Needs the configuration file as first argument");
66 ChannelUtils.stopLogger();
67 System.exit(1);
68 return;
69 }
70 if (! FileBasedConfiguration
71 .setConfigurationServerShutdownFromXml(Configuration.configuration, args[0])) {
72 logger
73 .error("Needs a correct configuration file as first argument");
74 if (DbConstant.admin != null){
75 DbConstant.admin.close();
76 }
77 ChannelUtils.stopLogger();
78 System.exit(1);
79 return;
80 }
81 Configuration.configuration.pipelineInit();
82 byte[] key;
83 key = FilesystemBasedDigest.passwdCrypt(Configuration.configuration.getSERVERADMINKEY());
84 final ShutdownPacket packet = new ShutdownPacket(
85 key);
86 final NetworkTransaction networkTransaction = new NetworkTransaction();
87 DbHostAuth host = Configuration.configuration.HOST_SSLAUTH;
88 final SocketAddress socketServerAddress = host.getSocketAddress();
89 LocalChannelReference localChannelReference = null;
90 localChannelReference = networkTransaction
91 .createConnectionWithRetry(socketServerAddress,true, null);
92 if (localChannelReference == null) {
93 logger.error("Cannot connect to "+host.getSocketAddress());
94 networkTransaction.closeAll();
95 return;
96 }
97 localChannelReference.sessionNewState(R66FiniteDualStates.SHUTDOWN);
98 ChannelUtils.writeAbstractLocalPacket(localChannelReference, packet, false);
99 localChannelReference.getFutureRequest().awaitUninterruptibly();
100 if (localChannelReference.getFutureRequest().isSuccess()) {
101 logger.warn("Shutdown OK");
102 } else {
103 R66Result result = localChannelReference.getFutureRequest()
104 .getResult();
105 if (result.other instanceof ValidPacket &&
106 ((ValidPacket) result.other).getTypeValid() == LocalPacketFactory.SHUTDOWNPACKET) {
107 logger.warn("Shutdown command OK");
108 } else if (result.code == ErrorCode.Shutdown) {
109 logger.warn("Shutdown command On going");
110 } else {
111 logger.error("Cannot Shutdown: "+result.toString(), localChannelReference
112 .getFutureRequest().getCause());
113 }
114 }
115 networkTransaction.closeAll();
116 }
117
118 }