01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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-17 19:41:29 -0600 (Mon, 17 Sep 2007) $
35: */
36: public class SslServerSocketFactory extends
37: javax.net.ServerSocketFactory {
38: private static boolean sslEnabled = false;
39:
40: private static javax.net.ServerSocketFactory sslFactory = null;
41:
42: private static ServerSocketFactory factory = null;
43:
44: public SslServerSocketFactory() {
45: super ();
46: }
47:
48: @Override
49: public ServerSocket createServerSocket(int port) throws IOException {
50: return new ServerSocket(port);
51: }
52:
53: @Override
54: public ServerSocket createServerSocket(int port, int backlog)
55: throws IOException {
56: return new ServerSocket(port, backlog);
57: }
58:
59: @Override
60: public ServerSocket createServerSocket(int port, int backlog,
61: InetAddress ifAddress) throws IOException {
62: return new ServerSocket(port, backlog, ifAddress);
63: }
64:
65: public static javax.net.ServerSocketFactory getServerSocketFactory()
66: throws IOException {
67: if (isSslEnabled()) {
68: if (sslFactory == null) {
69: try {
70: sslFactory = BogusSslContextFactory.getInstance(
71: true).getServerSocketFactory();
72: } catch (GeneralSecurityException e) {
73: IOException ioe = new IOException(
74: "could not create SSL socket");
75: ioe.initCause(e);
76: throw ioe;
77: }
78: }
79: return sslFactory;
80: } else {
81: if (factory == null) {
82: factory = new SslServerSocketFactory();
83: }
84: return factory;
85: }
86:
87: }
88:
89: public static boolean isSslEnabled() {
90: return sslEnabled;
91: }
92:
93: public static void setSslEnabled(boolean newSslEnabled) {
94: sslEnabled = newSslEnabled;
95: }
96: }
|