001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.net;
015
016import java.security.NoSuchAlgorithmException;
017
018import javax.net.ServerSocketFactory;
019import javax.net.ssl.SSLContext;
020
021import ch.qos.logback.classic.LoggerContext;
022import ch.qos.logback.classic.joran.JoranConfigurator;
023import ch.qos.logback.core.net.ssl.ConfigurableSSLServerSocketFactory;
024import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;
025
026/**
027 * A {@link SimpleSocketServer} that supports SSL.
028 * 
029 * <pre>
030 *      &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.classic.net.ssl.SimpleSSLSocketServer port configFile
031 * </pre>
032 * 
033 * where <em>port</em> is a port number where the server listens and
034 * <em>configFile</em> is an XML configuration file fed to
035 * {@link JoranConfigurator}.
036 * 
037 * When running the SimpleSSLServerFactory as shown above, it is necessary to
038 * configure JSSE system properties using {@code -Dname=value} on the
039 * command-line when starting the server. In particular, you will probably
040 * want/need to configure the following system properties:
041 * <ul>
042 * <li>javax.net.ssl.keyStore</li>
043 * <li>javax.net.ssl.keyStorePassword</li>
044 * <li>javax.net.ssl.keyStoreType</li>
045 * <li>javax.net.ssl.trustStore</li>
046 * <li>javax.net.ssl.trustStorePassword</li>
047 * <li>javax.net.ssl.trustStoreType</li>
048 * </ul>
049 * <p>
050 * See the <a href=
051 * "http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#InstallationAndCustomization">
052 * Customizing the JSSE</a> in the JSSE Reference Guide for details on how to
053 * set these system properties.
054 * 
055 * @author Carl Harris
056 */
057public class SimpleSSLSocketServer extends SimpleSocketServer {
058
059    private final ServerSocketFactory socketFactory;
060
061    public static void main(String argv[]) throws Exception {
062        doMain(SimpleSSLSocketServer.class, argv);
063    }
064
065    /**
066     * Creates a new server using the default SSL context.
067     * 
068     * @param lc   logger context for received events
069     * @param port port on which the server is to listen
070     * @throws NoSuchAlgorithmException if the default SSL context cannot be created
071     */
072    public SimpleSSLSocketServer(LoggerContext lc, int port) throws NoSuchAlgorithmException {
073        this(lc, port, SSLContext.getDefault());
074    }
075
076    /**
077     * Creates a new server using a custom SSL context.
078     * 
079     * @param lc         logger context for received events
080     * @param port       port on which the server is to listen
081     * @param sslContext custom SSL context
082     */
083    public SimpleSSLSocketServer(LoggerContext lc, int port, SSLContext sslContext) {
084        super(lc, port);
085        if (sslContext == null) {
086            throw new NullPointerException("SSL context required");
087        }
088        SSLParametersConfiguration parameters = new SSLParametersConfiguration();
089
090        parameters.setContext(lc);
091        this.socketFactory = new ConfigurableSSLServerSocketFactory(parameters, sslContext.getServerSocketFactory());
092    }
093
094    @Override
095    protected ServerSocketFactory getServerSocketFactory() {
096        return socketFactory;
097    }
098
099}