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     * 
045     * @return socket factory
046     */
047    @Override
048    protected SocketFactory getSocketFactory() {
049        return socketFactory;
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public void start() {
057        try {
058            SSLContext sslContext = getSsl().createContext(this);
059            SSLParametersConfiguration parameters = getSsl().getParameters();
060            parameters.setContext(getContext());
061            socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory());
062            super.start();
063        } catch (Exception ex) {
064            addError(ex.getMessage(), ex);
065        }
066    }
067
068    /**
069     * Gets the SSL configuration.
070     * 
071     * @return SSL configuration; if no configuration has been set, a default
072     *         configuration is returned
073     */
074    public SSLConfiguration getSsl() {
075        if (ssl == null) {
076            ssl = new SSLConfiguration();
077        }
078        return ssl;
079    }
080
081    /**
082     * Sets the SSL configuration.
083     * 
084     * @param ssl the SSL configuration to set
085     */
086    public void setSsl(SSLConfiguration ssl) {
087        this.ssl = ssl;
088    }
089
090}