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;
015
016import javax.net.SocketFactory;
017import javax.net.ssl.SSLContext;
018
019import ch.qos.logback.core.net.ssl.ConfigurableSSLSocketFactory;
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 * An abstract base for module specific {@code SSLSocketAppender}
026 * implementations located in other logback modules.
027 *
028 * @author Carl Harris
029 */
030public abstract class AbstractSSLSocketAppender<E> extends AbstractSocketAppender<E> implements SSLComponent {
031
032    private SSLConfiguration ssl;
033    private SocketFactory socketFactory;
034
035    /**
036     * Constructs a new appender.
037     */
038    protected AbstractSSLSocketAppender() {
039    }
040
041    /**
042     * Gets an {@link SocketFactory} that produces SSL sockets using an
043     * {@link SSLContext} that is derived from the appender's configuration.
044     * @return socket factory
045     */
046    @Override
047    protected SocketFactory getSocketFactory() {
048        return socketFactory;
049    }
050
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public void start() {
056        try {
057            SSLContext sslContext = getSsl().createContext(this);
058            SSLParametersConfiguration parameters = getSsl().getParameters();
059            parameters.setContext(getContext());
060            socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory());
061            super.start();
062        } catch (Exception ex) {
063            addError(ex.getMessage(), ex);
064        }
065    }
066
067    /**
068     * Gets the SSL configuration.
069     * @return SSL configuration; if no configuration has been set, a
070     *    default configuration is returned
071     */
072    public SSLConfiguration getSsl() {
073        if (ssl == null) {
074            ssl = new SSLConfiguration();
075        }
076        return ssl;
077    }
078
079    /**
080     * Sets the SSL configuration.
081     * @param ssl the SSL configuration to set
082     */
083    public void setSsl(SSLConfiguration ssl) {
084        this.ssl = ssl;
085    }
086
087}