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 goldengate.commandexec.ssl.server;
22  
23  import goldengate.commandexec.utils.LocalExecDefaultResult;
24  import goldengate.common.crypto.ssl.GgSecureKeyStore;
25  import goldengate.common.crypto.ssl.GgSslContextFactory;
26  import goldengate.common.logging.GgSlf4JLoggerFactory;
27  
28  import java.net.InetAddress;
29  import java.net.InetSocketAddress;
30  import java.util.concurrent.ExecutorService;
31  import java.util.concurrent.Executors;
32  
33  import org.jboss.netty.bootstrap.ServerBootstrap;
34  import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
35  import org.jboss.netty.logging.InternalLoggerFactory;
36  
37  /**
38   * LocalExec server Main method.
39   *
40   */
41  public class LocalExecSslServer {
42  
43      static ExecutorService threadPool;
44      static ExecutorService threadPool2;
45  
46      /**
47       * Takes 3 to 6 arguments (last 3 are optional arguments):<br>
48       * - mandatory arguments: filename keystorepaswwd keypassword<br>
49       * - if no more arguments are provided, it implies 127.0.0.1 + 9999 port and no certificates<br>
50       * - optional arguments:<br>
51       *  "trustfilename" "trustpassword"<br>
52       *  "trustfilename" "trustpassword" "addresse" "port"<br>
53       *  "trustfilename" "trustpassword" "addresse" "port" "default delay"<br>
54       * @param args
55       * @throws Exception
56       */
57      public static void main(String[] args) throws Exception {
58          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
59          int port = 9999;
60          InetAddress addr;
61          long delay = LocalExecDefaultResult.MAXWAITPROCESS;
62          String keyStoreFilename, keyStorePasswd, keyPassword;
63          String trustStoreFilename = null, trustStorePasswd = null;
64          byte []loop = {127,0,0,1};
65          addr = InetAddress.getByAddress(loop);
66          if (args.length >=3) {
67              keyStoreFilename = args[0];
68              keyStorePasswd = args[1];
69              keyPassword = args[2];
70              if (args.length >= 5) {
71                  trustStoreFilename = args[3];
72                  trustStorePasswd = args[4];
73                  if (args.length >= 7) {
74                      addr = InetAddress.getByName(args[5]);
75                      port = Integer.parseInt(args[6]);
76                      if (args.length > 7) {
77                          delay = Long.parseLong(args[7]);
78                      }
79                  }
80              }
81          } else {
82              System.err.println("Need at least 3 arguments: Filename KeyStorePswd KeyPswd");
83              return;
84          }
85          threadPool = Executors.newCachedThreadPool();
86          threadPool2 = Executors.newCachedThreadPool();
87          // Configure the server.
88          ServerBootstrap bootstrap = new ServerBootstrap(
89                  new NioServerSocketChannelFactory(threadPool, threadPool2));
90          // Load the KeyStore (No certificates)
91          GgSecureKeyStore ggSecureKeyStore =
92              new GgSecureKeyStore(keyStoreFilename, keyStorePasswd, keyPassword);
93          if (trustStoreFilename != null) {
94              // Include certificates
95              ggSecureKeyStore.initTrustStore(trustStoreFilename, trustStorePasswd, true);
96          } else {
97              ggSecureKeyStore.initEmptyTrustStore();
98          }
99          GgSslContextFactory ggSslContextFactory =
100             new GgSslContextFactory(ggSecureKeyStore, true);
101         // Configure the pipeline factory.
102         bootstrap.setPipelineFactory(
103                 new LocalExecSslServerPipelineFactory(ggSslContextFactory, delay));
104 
105         // Bind and start to accept incoming connections only on local address.
106         bootstrap.bind(new InetSocketAddress(addr, port));
107     }
108 }