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