View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net;
15  
16  import javax.net.SocketFactory;
17  import javax.net.ssl.SSLContext;
18  
19  import ch.qos.logback.core.net.ssl.ConfigurableSSLSocketFactory;
20  import ch.qos.logback.core.net.ssl.SSLComponent;
21  import ch.qos.logback.core.net.ssl.SSLConfiguration;
22  import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;
23  
24  /**
25   * A {@link SocketReceiver} that supports SSL.
26   *
27   * @author Carl Harris
28   */
29  public class SSLSocketReceiver extends SocketReceiver implements SSLComponent {
30  
31      private SSLConfiguration ssl;
32      private SocketFactory socketFactory;
33  
34      /**
35       * Gets an {@link SocketFactory} that produces SSL sockets using an
36       * {@link SSLContext} that is derived from the receiver's configuration.
37       * 
38       * @return socket factory
39       */
40      @Override
41      protected SocketFactory getSocketFactory() {
42          return socketFactory;
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      protected boolean shouldStart() {
50          try {
51              SSLContext sslContext = getSsl().createContext(this);
52              SSLParametersConfiguration parameters = getSsl().getParameters();
53              parameters.setContext(getContext());
54              socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory());
55              return super.shouldStart();
56          } catch (Exception ex) {
57              addError(ex.getMessage(), ex);
58              return false;
59          }
60      }
61  
62      /**
63       * Gets the SSL configuration.
64       * 
65       * @return SSL configuration; if no configuration has been set, a default
66       *         configuration is returned
67       */
68      public SSLConfiguration getSsl() {
69          if (ssl == null) {
70              ssl = new SSLConfiguration();
71          }
72          return ssl;
73      }
74  
75      /**
76       * Sets the SSL configuration.
77       * 
78       * @param ssl the SSL configuration to set
79       */
80      public void setSsl(SSLConfiguration ssl) {
81          this.ssl = ssl;
82      }
83  
84  }