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.core.net.ssl;
015
016import java.io.IOException;
017import java.net.InetAddress;
018import java.net.ServerSocket;
019
020import javax.net.ServerSocketFactory;
021import javax.net.ssl.SSLServerSocket;
022import javax.net.ssl.SSLServerSocketFactory;
023
024/**
025 * An {@link SSLServerSocketFactory} that configures SSL parameters (those
026 * specified in {@link SSLParametersConfiguration}) on each newly created socket.
027 * <p>
028 * When any of this factory's {@code createServerSocket} methods are invoked, it
029 * calls on a delegate {@link SSLServerSocketFactory} to create the socket, and
030 * then sets the SSL parameters of the socket (using the provided configuration)
031 * before returning the socket to the caller.
032 *
033 * @author Carl Harris
034 */
035public class ConfigurableSSLServerSocketFactory extends ServerSocketFactory {
036
037    private final SSLParametersConfiguration parameters;
038    private final SSLServerSocketFactory delegate;
039
040    /**
041     * Creates a new factory.
042     * 
043     * @param parameters parameters that will be configured on each socket created
044     *                   by the factory
045     * @param delegate   socket factory that will be called upon to create server
046     *                   sockets before configuration
047     */
048    public ConfigurableSSLServerSocketFactory(SSLParametersConfiguration parameters, SSLServerSocketFactory delegate) {
049        this.parameters = parameters;
050        this.delegate = delegate;
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
058        SSLServerSocket socket = (SSLServerSocket) delegate.createServerSocket(port, backlog, ifAddress);
059        parameters.configure(new SSLConfigurableServerSocket(socket));
060        return socket;
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public ServerSocket createServerSocket(int port, int backlog) throws IOException {
068        SSLServerSocket socket = (SSLServerSocket) delegate.createServerSocket(port, backlog);
069        parameters.configure(new SSLConfigurableServerSocket(socket));
070        return socket;
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public ServerSocket createServerSocket(int port) throws IOException {
078        SSLServerSocket socket = (SSLServerSocket) delegate.createServerSocket(port);
079        parameters.configure(new SSLConfigurableServerSocket(socket));
080        return socket;
081    }
082
083}