1 | /* |
2 | * @(#) $Id: SSLSocketFactory.java 210062 2005-07-11 03:52:38Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
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.Socket; |
24 | import java.net.UnknownHostException; |
25 | import java.security.GeneralSecurityException; |
26 | |
27 | import javax.net.SocketFactory; |
28 | |
29 | /** |
30 | * Simple Socket factory to create sockets with or without SSL enabled. |
31 | * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes) |
32 | * |
33 | * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $ |
34 | */ |
35 | public class SSLSocketFactory extends SocketFactory |
36 | { |
37 | private static boolean sslEnabled = false; |
38 | |
39 | private static javax.net.ssl.SSLSocketFactory sslFactory = null; |
40 | |
41 | private static javax.net.SocketFactory factory = null; |
42 | |
43 | public SSLSocketFactory() |
44 | { |
45 | super(); |
46 | } |
47 | |
48 | public Socket createSocket( String arg1, int arg2 ) throws IOException, |
49 | UnknownHostException |
50 | { |
51 | if( isSslEnabled() ) |
52 | { |
53 | return getSSLFactory().createSocket( arg1, arg2 ); |
54 | } |
55 | else |
56 | { |
57 | return new Socket( arg1, arg2 ); |
58 | } |
59 | } |
60 | |
61 | public Socket createSocket( String arg1, int arg2, InetAddress arg3, |
62 | int arg4 ) throws IOException, |
63 | UnknownHostException |
64 | { |
65 | if( isSslEnabled() ) |
66 | { |
67 | return getSSLFactory().createSocket( arg1, arg2, arg3, arg4 ); |
68 | } |
69 | else |
70 | { |
71 | return new Socket( arg1, arg2, arg3, arg4 ); |
72 | } |
73 | } |
74 | |
75 | public Socket createSocket( InetAddress arg1, int arg2 ) |
76 | throws IOException |
77 | { |
78 | if( isSslEnabled() ) |
79 | { |
80 | return getSSLFactory().createSocket( arg1, arg2 ); |
81 | } |
82 | else |
83 | { |
84 | return new Socket( arg1, arg2 ); |
85 | } |
86 | } |
87 | |
88 | public Socket createSocket( InetAddress arg1, int arg2, InetAddress arg3, |
89 | int arg4 ) throws IOException |
90 | { |
91 | if( isSslEnabled() ) |
92 | { |
93 | return getSSLFactory().createSocket( arg1, arg2, arg3, arg4 ); |
94 | } |
95 | else |
96 | { |
97 | return new Socket( arg1, arg2, arg3, arg4 ); |
98 | } |
99 | } |
100 | |
101 | public static javax.net.SocketFactory getSocketFactory() |
102 | { |
103 | if( factory == null ) |
104 | { |
105 | factory = new SSLSocketFactory(); |
106 | } |
107 | return factory; |
108 | } |
109 | |
110 | private javax.net.ssl.SSLSocketFactory getSSLFactory() |
111 | { |
112 | if( sslFactory == null ) |
113 | { |
114 | try |
115 | { |
116 | sslFactory = BogusSSLContextFactory.getInstance( false ) |
117 | .getSocketFactory(); |
118 | } |
119 | catch( GeneralSecurityException e ) |
120 | { |
121 | throw new RuntimeException( "could not create SSL socket", e ); |
122 | } |
123 | } |
124 | return sslFactory; |
125 | } |
126 | |
127 | public static boolean isSslEnabled() |
128 | { |
129 | return sslEnabled; |
130 | } |
131 | |
132 | public static void setSslEnabled( boolean newSslEnabled ) |
133 | { |
134 | sslEnabled = newSslEnabled; |
135 | } |
136 | |
137 | } |