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.Socket;
25 import java.net.UnknownHostException;
26 import java.security.GeneralSecurityException;
27
28 import javax.net.SocketFactory;
29
30
31
32
33
34
35
36
37 public class SslSocketFactory extends SocketFactory {
38 private static boolean sslEnabled = false;
39
40 private static javax.net.ssl.SSLSocketFactory sslFactory = null;
41
42 private static javax.net.SocketFactory factory = null;
43
44 public SslSocketFactory() {
45 super();
46 }
47
48 @Override
49 public Socket createSocket(String arg1, int arg2) throws IOException,
50 UnknownHostException {
51 if (isSslEnabled()) {
52 return getSSLFactory().createSocket(arg1, arg2);
53 } else {
54 return new Socket(arg1, arg2);
55 }
56 }
57
58 @Override
59 public Socket createSocket(String arg1, int arg2, InetAddress arg3, int arg4)
60 throws IOException, UnknownHostException {
61 if (isSslEnabled()) {
62 return getSSLFactory().createSocket(arg1, arg2, arg3, arg4);
63 } else {
64 return new Socket(arg1, arg2, arg3, arg4);
65 }
66 }
67
68 @Override
69 public Socket createSocket(InetAddress arg1, int arg2) throws IOException {
70 if (isSslEnabled()) {
71 return getSSLFactory().createSocket(arg1, arg2);
72 } else {
73 return new Socket(arg1, arg2);
74 }
75 }
76
77 @Override
78 public Socket createSocket(InetAddress arg1, int arg2, InetAddress arg3,
79 int arg4) throws IOException {
80 if (isSslEnabled()) {
81 return getSSLFactory().createSocket(arg1, arg2, arg3, arg4);
82 } else {
83 return new Socket(arg1, arg2, arg3, arg4);
84 }
85 }
86
87 public static javax.net.SocketFactory getSocketFactory() {
88 if (factory == null) {
89 factory = new SslSocketFactory();
90 }
91 return factory;
92 }
93
94 private javax.net.ssl.SSLSocketFactory getSSLFactory() {
95 if (sslFactory == null) {
96 try {
97 sslFactory = BogusSslContextFactory.getInstance(false)
98 .getSocketFactory();
99 } catch (GeneralSecurityException e) {
100 throw new RuntimeException("could not create SSL socket", e);
101 }
102 }
103 return sslFactory;
104 }
105
106 public static boolean isSslEnabled() {
107 return sslEnabled;
108 }
109
110 public static void setSslEnabled(boolean newSslEnabled) {
111 sslEnabled = newSslEnabled;
112 }
113
114 }