View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.example.echoserver.ssl;
    21
    22  import java.io.IOException;
    23  import java.net.InetAddress;
    24  import java.net.ServerSocket;
    25  import java.security.GeneralSecurityException;
    26
    27  import javax.net.ServerSocketFactory;
    28
    29  /**
    30   * Simple Server Socket factory to create sockets with or without SSL enabled.
    31   * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes)
    32   *
    33   * @author The Apache Directory Project (mina-dev@directory.apache.org)
    34   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
    35   */
    36  public class SSLServerSocketFactory extends javax.net.ServerSocketFactory {
    37      private static boolean sslEnabled = false;
    38
    39      private static javax.net.ServerSocketFactory sslFactory = null;
    40
    41      private static ServerSocketFactory factory = null;
    42
    43      public SSLServerSocketFactory() {
    44          super();
    45      }
    46
    47      public ServerSocket createServerSocket(int port) throws IOException {
    48          return new ServerSocket(port);
    49      }
    50
    51      public ServerSocket createServerSocket(int port, int backlog)
    52              throws IOException {
    53          return new ServerSocket(port, backlog);
    54      }
    55
    56      public ServerSocket createServerSocket(int port, int backlog,
    57              InetAddress ifAddress) throws IOException {
    58          return new ServerSocket(port, backlog, ifAddress);
    59      }
    60
    61      public static javax.net.ServerSocketFactory getServerSocketFactory()
    62              throws IOException {
    63          if (isSslEnabled()) {
    64              if (sslFactory == null) {
    65                  try {
    66                      sslFactory = BogusSSLContextFactory.getInstance(true)
    67                              .getServerSocketFactory();
    68                  } catch (GeneralSecurityException e) {
    69                      IOException ioe = new IOException(
    70                              "could not create SSL socket");
    71                      ioe.initCause(e);
    72                      throw ioe;
    73                  }
    74              }
    75              return sslFactory;
    76          } else {
    77              if (factory == null) {
    78                  factory = new SSLServerSocketFactory();
    79              }
    80              return factory;
    81          }
    82
    83      }
    84
    85      public static boolean isSslEnabled() {
    86          return sslEnabled;
    87      }
    88
    89      public static void setSslEnabled(boolean newSslEnabled) {
    90          sslEnabled = newSslEnabled;
    91      }
    92  }