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       * @return socket factory
38       */
39      @Override
40      protected SocketFactory getSocketFactory() {
41          return socketFactory;
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      protected boolean shouldStart() {
49          try {
50              SSLContext sslContext = getSsl().createContext(this);
51              SSLParametersConfiguration parameters = getSsl().getParameters();
52              parameters.setContext(getContext());
53              socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory());
54              return super.shouldStart();
55          } catch (Exception ex) {
56              addError(ex.getMessage(), ex);
57              return false;
58          }
59      }
60  
61      /**
62       * Gets the SSL configuration.
63       * @return SSL configuration; if no configuration has been set, a
64       *    default configuration is returned
65       */
66      public SSLConfiguration getSsl() {
67          if (ssl == null) {
68              ssl = new SSLConfiguration();
69          }
70          return ssl;
71      }
72  
73      /**
74       * Sets the SSL configuration.
75       * @param ssl the SSL configuration to set
76       */
77      public void setSsl(SSLConfiguration ssl) {
78          this.ssl = ssl;
79      }
80  
81  }