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       * @param lc logger context for received events
68       * @param port port on which the server is to listen
69       * @throws NoSuchAlgorithmException if the default SSL context cannot be
70       *         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       * @param lc logger context for received events
79       * @param port port on which the server is to listen
80       * @param sslContext custom SSL context
81       */
82      public SimpleSSLSocketServer(LoggerContext lc, int port, SSLContext sslContext) {
83          super(lc, port);
84          if (sslContext == null) {
85              throw new NullPointerException("SSL context required");
86          }
87          SSLParametersConfiguration parameters = new SSLParametersConfiguration();
88  
89          parameters.setContext(lc);
90          this.socketFactory = new ConfigurableSSLServerSocketFactory(parameters, sslContext.getServerSocketFactory());
91      }
92  
93      @Override
94      protected ServerSocketFactory getServerSocketFactory() {
95          return socketFactory;
96      }
97  
98  }