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 MINA Project (dev@mina.apache.org)
34   */
35  public class SslServerSocketFactory extends javax.net.ServerSocketFactory {
36      private static boolean sslEnabled = false;
37  
38      private static javax.net.ServerSocketFactory sslFactory = null;
39  
40      private static ServerSocketFactory factory = null;
41  
42      public SslServerSocketFactory() {
43          super();
44      }
45  
46      @Override
47      public ServerSocket createServerSocket(int port) throws IOException {
48          return new ServerSocket(port);
49      }
50  
51      @Override
52      public ServerSocket createServerSocket(int port, int backlog)
53              throws IOException {
54          return new ServerSocket(port, backlog);
55      }
56  
57      @Override
58      public ServerSocket createServerSocket(int port, int backlog,
59              InetAddress ifAddress) throws IOException {
60          return new ServerSocket(port, backlog, ifAddress);
61      }
62  
63      public static javax.net.ServerSocketFactory getServerSocketFactory()
64              throws IOException {
65          if (isSslEnabled()) {
66              if (sslFactory == null) {
67                  try {
68                      sslFactory = BogusSslContextFactory.getInstance(true)
69                              .getServerSocketFactory();
70                  } catch (GeneralSecurityException e) {
71                      IOException ioe = new IOException(
72                              "could not create SSL socket");
73                      ioe.initCause(e);
74                      throw ioe;
75                  }
76              }
77              return sslFactory;
78          } else {
79              if (factory == null) {
80                  factory = new SslServerSocketFactory();
81              }
82              return factory;
83          }
84  
85      }
86  
87      public static boolean isSslEnabled() {
88          return sslEnabled;
89      }
90  
91      public static void setSslEnabled(boolean newSslEnabled) {
92          sslEnabled = newSslEnabled;
93      }
94  }