1 | /* |
2 | * @(#) $Id: SSLServerSocketFactory.java 332218 2005-11-10 03:52:42Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.examples.echoserver.ssl; |
20 | |
21 | import java.io.IOException; |
22 | import java.net.InetAddress; |
23 | import java.net.ServerSocket; |
24 | import java.security.GeneralSecurityException; |
25 | |
26 | import javax.net.ServerSocketFactory; |
27 | |
28 | /** |
29 | * Simple Server Socket factory to create sockets with or without SSL enabled. |
30 | * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes) |
31 | * |
32 | * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $ |
33 | */ |
34 | public class SSLServerSocketFactory extends javax.net.ServerSocketFactory |
35 | { |
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 | { |
44 | super(); |
45 | } |
46 | |
47 | public ServerSocket createServerSocket( int port ) throws IOException |
48 | { |
49 | return new ServerSocket( port ); |
50 | } |
51 | |
52 | public ServerSocket createServerSocket( int port, int backlog ) |
53 | throws IOException |
54 | { |
55 | return new ServerSocket( port, backlog ); |
56 | } |
57 | |
58 | public ServerSocket createServerSocket( int port, int backlog, |
59 | InetAddress ifAddress ) |
60 | throws IOException |
61 | { |
62 | return new ServerSocket( port, backlog, ifAddress ); |
63 | } |
64 | |
65 | public static javax.net.ServerSocketFactory getServerSocketFactory() |
66 | throws IOException |
67 | { |
68 | if( isSslEnabled() ) |
69 | { |
70 | if( sslFactory == null ) |
71 | { |
72 | try |
73 | { |
74 | sslFactory = BogusSSLContextFactory.getInstance( true ) |
75 | .getServerSocketFactory(); |
76 | } |
77 | catch( GeneralSecurityException e ) |
78 | { |
79 | IOException ioe = new IOException( |
80 | "could not create SSL socket" ); |
81 | ioe.initCause( e ); |
82 | throw ioe; |
83 | } |
84 | } |
85 | return sslFactory; |
86 | } |
87 | else |
88 | { |
89 | if( factory == null ) |
90 | { |
91 | factory = new SSLServerSocketFactory(); |
92 | } |
93 | return factory; |
94 | } |
95 | |
96 | } |
97 | |
98 | public static boolean isSslEnabled() |
99 | { |
100 | return sslEnabled; |
101 | } |
102 | |
103 | public static void setSslEnabled( boolean newSslEnabled ) |
104 | { |
105 | sslEnabled = newSslEnabled; |
106 | } |
107 | } |