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 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 * A {@link SocketReceiver} that supports SSL. 026 * 027 * @author Carl Harris 028 */ 029public class SSLSocketReceiver extends SocketReceiver implements SSLComponent { 030 031 private SSLConfiguration ssl; 032 private SocketFactory socketFactory; 033 034 /** 035 * Gets an {@link SocketFactory} that produces SSL sockets using an 036 * {@link SSLContext} that is derived from the receiver's configuration. 037 * 038 * @return socket factory 039 */ 040 @Override 041 protected SocketFactory getSocketFactory() { 042 return socketFactory; 043 } 044 045 /** 046 * {@inheritDoc} 047 */ 048 @Override 049 protected boolean shouldStart() { 050 try { 051 SSLContext sslContext = getSsl().createContext(this); 052 SSLParametersConfiguration parameters = getSsl().getParameters(); 053 parameters.setContext(getContext()); 054 socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory()); 055 return super.shouldStart(); 056 } catch (Exception ex) { 057 addError(ex.getMessage(), ex); 058 return false; 059 } 060 } 061 062 /** 063 * Gets the SSL configuration. 064 * 065 * @return SSL configuration; if no configuration has been set, a default 066 * configuration is returned 067 */ 068 public SSLConfiguration getSsl() { 069 if (ssl == null) { 070 ssl = new SSLConfiguration(); 071 } 072 return ssl; 073 } 074 075 /** 076 * Sets the SSL configuration. 077 * 078 * @param ssl the SSL configuration to set 079 */ 080 public void setSsl(SSLConfiguration ssl) { 081 this.ssl = ssl; 082 } 083 084}