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