1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
34
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 public ServerSocket createServerSocket(int port) throws IOException {
48 return new ServerSocket(port);
49 }
50
51 public ServerSocket createServerSocket(int port, int backlog)
52 throws IOException {
53 return new ServerSocket(port, backlog);
54 }
55
56 public ServerSocket createServerSocket(int port, int backlog,
57 InetAddress ifAddress) throws IOException {
58 return new ServerSocket(port, backlog, ifAddress);
59 }
60
61 public static javax.net.ServerSocketFactory getServerSocketFactory()
62 throws IOException {
63 if (isSslEnabled()) {
64 if (sslFactory == null) {
65 try {
66 sslFactory = BogusSSLContextFactory.getInstance(true)
67 .getServerSocketFactory();
68 } catch (GeneralSecurityException e) {
69 IOException ioe = new IOException(
70 "could not create SSL socket");
71 ioe.initCause(e);
72 throw ioe;
73 }
74 }
75 return sslFactory;
76 } else {
77 if (factory == null) {
78 factory = new SSLServerSocketFactory();
79 }
80 return factory;
81 }
82
83 }
84
85 public static boolean isSslEnabled() {
86 return sslEnabled;
87 }
88
89 public static void setSslEnabled(boolean newSslEnabled) {
90 sslEnabled = newSslEnabled;
91 }
92 }