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.server;
015
016import javax.net.ServerSocketFactory;
017import javax.net.ssl.SSLContext;
018
019import ch.qos.logback.core.net.ssl.ConfigurableSSLServerSocketFactory;
020import ch.qos.logback.core.net.ssl.SSLComponent;
021import ch.qos.logback.core.net.ssl.SSLConfiguration;
022import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;
023
024/**
025 * 
026 * This is the base class for module specific ServerSocketAppender
027 * implementations.
028 * 
029 * @author Carl Harris
030 */
031public abstract class SSLServerSocketAppenderBase<E> extends AbstractServerSocketAppender<E> implements SSLComponent {
032
033    private SSLConfiguration ssl;
034    private ServerSocketFactory socketFactory;
035
036    @Override
037    protected ServerSocketFactory getServerSocketFactory() {
038        return socketFactory;
039    }
040
041    /**
042     * {@inheritDoc}
043     */
044    @Override
045    public void start() {
046        try {
047            SSLContext sslContext = getSsl().createContext(this);
048            SSLParametersConfiguration parameters = getSsl().getParameters();
049            parameters.setContext(getContext());
050            socketFactory = new ConfigurableSSLServerSocketFactory(parameters, sslContext.getServerSocketFactory());
051            super.start();
052        } catch (Exception ex) {
053            addError(ex.getMessage(), ex);
054        }
055    }
056
057    /**
058     * Gets the SSL configuration.
059     * 
060     * @return SSL configuration; if no configuration has been set, a default
061     *         configuration is returned
062     */
063    public SSLConfiguration getSsl() {
064        if (ssl == null) {
065            ssl = new SSLConfiguration();
066        }
067        return ssl;
068    }
069
070    /**
071     * Sets the SSL configuration.
072     * 
073     * @param ssl the SSL configuration to set
074     */
075    public void setSsl(SSLConfiguration ssl) {
076        this.ssl = ssl;
077    }
078
079}