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.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   * Local client to shutdown the server (using network)
49   *
50   * @author Frederic Bregier
51   */
52  public class ServerShutdown {
53  
54      /**
55       * @param args the configuration file as first argument
56       * @throws OpenR66ProtocolPacketException
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 }