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.server;
15  
16  import javax.net.ServerSocketFactory;
17  import javax.net.ssl.SSLContext;
18  
19  import ch.qos.logback.core.net.ssl.ConfigurableSSLServerSocketFactory;
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 ServerSocketReceiver} that supports SSL.
26   *
27   * @author Carl Harris
28   */
29  public class SSLServerSocketReceiver extends ServerSocketReceiver implements SSLComponent {
30  
31      private SSLConfiguration ssl;
32      private ServerSocketFactory socketFactory;
33  
34      /**
35       * {@inheritDoc}
36       */
37      @Override
38      protected ServerSocketFactory getServerSocketFactory() throws Exception {
39          if (socketFactory == null) {
40              SSLContext sslContext = getSsl().createContext(this);
41              SSLParametersConfiguration parameters = getSsl().getParameters();
42              parameters.setContext(getContext());
43              socketFactory = new ConfigurableSSLServerSocketFactory(parameters, sslContext.getServerSocketFactory());
44          }
45          return socketFactory;
46      }
47  
48      /**
49       * Gets the server's SSL configuration.
50       * @return SSL configuration; if no SSL configuration was provided
51       *    a default configuration is returned
52       */
53      public SSLConfiguration getSsl() {
54          if (ssl == null) {
55              ssl = new SSLConfiguration();
56          }
57          return ssl;
58      }
59  
60      /**
61       * Gets the server's SSL configuration.
62       * @param ssl the SSL configuration to set.
63       */
64      public void setSsl(SSLConfiguration ssl) {
65          this.ssl = ssl;
66      }
67  
68  }