1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }