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   * @version $Rev: 576647 $, $Date: 2007-09-18 03:41:29 +0200 (mar, 18 sep 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      @Override
48      public ServerSocket createServerSocket(int port) throws IOException {
49          return new ServerSocket(port);
50      }
51  
52      @Override
53      public ServerSocket createServerSocket(int port, int backlog)
54              throws IOException {
55          return new ServerSocket(port, backlog);
56      }
57  
58      @Override
59      public ServerSocket createServerSocket(int port, int backlog,
60              InetAddress ifAddress) throws IOException {
61          return new ServerSocket(port, backlog, ifAddress);
62      }
63  
64      public static javax.net.ServerSocketFactory getServerSocketFactory()
65              throws IOException {
66          if (isSslEnabled()) {
67              if (sslFactory == null) {
68                  try {
69                      sslFactory = BogusSslContextFactory.getInstance(true)
70                              .getServerSocketFactory();
71                  } catch (GeneralSecurityException e) {
72                      IOException ioe = new IOException(
73                              "could not create SSL socket");
74                      ioe.initCause(e);
75                      throw ioe;
76                  }
77              }
78              return sslFactory;
79          } else {
80              if (factory == null) {
81                  factory = new SslServerSocketFactory();
82              }
83              return factory;
84          }
85  
86      }
87  
88      public static boolean isSslEnabled() {
89          return sslEnabled;
90      }
91  
92      public static void setSslEnabled(boolean newSslEnabled) {
93          sslEnabled = newSslEnabled;
94      }
95  }