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.core.net.ssl;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.ServerSocket;
19  
20  import javax.net.ServerSocketFactory;
21  import javax.net.ssl.SSLServerSocket;
22  import javax.net.ssl.SSLServerSocketFactory;
23  
24  /**
25   * An {@link SSLServerSocketFactory} that configures SSL parameters 
26   * (those specified in {@link SSLParametersConfiguration} on each newly 
27   * created socket. 
28   * <p>
29   * When any of this factory's {@code createServerSocket} methods are invoked, 
30   * it calls on a delegate {@link SSLServerSocketFactory} to create the socket, 
31   * and then sets the SSL parameters of the socket (using the provided 
32   * configuration) before returning the socket to the caller.
33   *
34   * @author Carl Harris
35   */
36  public class ConfigurableSSLServerSocketFactory extends ServerSocketFactory {
37  
38      private final SSLParametersConfiguration parameters;
39      private final SSLServerSocketFactory delegate;
40  
41      /**
42       * Creates a new factory.
43       * @param parameters parameters that will be configured on each
44       *    socket created by the factory
45       * @param delegate socket factory that will be called upon to create
46       *    server sockets before configuration
47       */
48      public ConfigurableSSLServerSocketFactory(SSLParametersConfiguration parameters, SSLServerSocketFactory delegate) {
49          this.parameters = parameters;
50          this.delegate = delegate;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
58          SSLServerSocket socket = (SSLServerSocket) delegate.createServerSocket(port, backlog, ifAddress);
59          parameters.configure(new SSLConfigurableServerSocket(socket));
60          return socket;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public ServerSocket createServerSocket(int port, int backlog) throws IOException {
68          SSLServerSocket socket = (SSLServerSocket) delegate.createServerSocket(port, backlog);
69          parameters.configure(new SSLConfigurableServerSocket(socket));
70          return socket;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public ServerSocket createServerSocket(int port) throws IOException {
78          SSLServerSocket socket = (SSLServerSocket) delegate.createServerSocket(port);
79          parameters.configure(new SSLConfigurableServerSocket(socket));
80          return socket;
81      }
82  
83  }