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 (those
26   * specified in {@link SSLParametersConfiguration}) on each newly created socket.
27   * <p>
28   * When any of this factory's {@code createServerSocket} methods are invoked, it
29   * calls on a delegate {@link SSLServerSocketFactory} to create the socket, and
30   * then sets the SSL parameters of the socket (using the provided configuration)
31   * before returning the socket to the caller.
32   *
33   * @author Carl Harris
34   */
35  public class ConfigurableSSLServerSocketFactory extends ServerSocketFactory {
36  
37      private final SSLParametersConfiguration parameters;
38      private final SSLServerSocketFactory delegate;
39  
40      /**
41       * Creates a new factory.
42       * 
43       * @param parameters parameters that will be configured on each socket created
44       *                   by the factory
45       * @param delegate   socket factory that will be called upon to create server
46       *                   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  }