View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net;
15  
16  import java.security.NoSuchAlgorithmException;
17  
18  import javax.net.ServerSocketFactory;
19  import javax.net.ssl.SSLContext;
20  
21  import ch.qos.logback.classic.LoggerContext;
22  import ch.qos.logback.classic.joran.JoranConfigurator;
23  import ch.qos.logback.core.net.ssl.ConfigurableSSLServerSocketFactory;
24  import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;
25  
26  /**
27   * A {@link SimpleSocketServer} that supports SSL.
28   * 
29   * <pre>
30   *      &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.classic.net.ssl.SimpleSSLSocketServer port configFile
31   * </pre>
32   * 
33   * where <em>port</em> is a port number where the server listens and
34   * <em>configFile</em> is an XML configuration file fed to
35   * {@link JoranConfigurator}.
36   * 
37   * When running the SimpleSSLServerFactory as shown above, it is necessary to
38   * configure JSSE system properties using {@code -Dname=value} on the
39   * command-line when starting the server. In particular, you will probably
40   * want/need to configure the following system properties:
41   * <ul>
42   * <li>javax.net.ssl.keyStore</li>
43   * <li>javax.net.ssl.keyStorePassword</li>
44   * <li>javax.net.ssl.keyStoreType</li>
45   * <li>javax.net.ssl.trustStore</li>
46   * <li>javax.net.ssl.trustStorePassword</li>
47   * <li>javax.net.ssl.trustStoreType</li>
48   * </ul>
49   * <p>
50   * See the <a href=
51   * "http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#InstallationAndCustomization">
52   * Customizing the JSSE</a> in the JSSE Reference Guide for details on how to
53   * set these system properties.
54   * 
55   * @author Carl Harris
56   */
57  public class SimpleSSLSocketServer extends SimpleSocketServer {
58  
59      private final ServerSocketFactory socketFactory;
60  
61      public static void main(String argv[]) throws Exception {
62          doMain(SimpleSSLSocketServer.class, argv);
63      }
64  
65      /**
66       * Creates a new server using the default SSL context.
67       * 
68       * @param lc   logger context for received events
69       * @param port port on which the server is to listen
70       * @throws NoSuchAlgorithmException if the default SSL context cannot be created
71       */
72      public SimpleSSLSocketServer(LoggerContext lc, int port) throws NoSuchAlgorithmException {
73          this(lc, port, SSLContext.getDefault());
74      }
75  
76      /**
77       * Creates a new server using a custom SSL context.
78       * 
79       * @param lc         logger context for received events
80       * @param port       port on which the server is to listen
81       * @param sslContext custom SSL context
82       */
83      public SimpleSSLSocketServer(LoggerContext lc, int port, SSLContext sslContext) {
84          super(lc, port);
85          if (sslContext == null) {
86              throw new NullPointerException("SSL context required");
87          }
88          SSLParametersConfiguration parameters = new SSLParametersConfiguration();
89  
90          parameters.setContext(lc);
91          this.socketFactory = new ConfigurableSSLServerSocketFactory(parameters, sslContext.getServerSocketFactory());
92      }
93  
94      @Override
95      protected ServerSocketFactory getServerSocketFactory() {
96          return socketFactory;
97      }
98  
99  }