1 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 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 public Socket createSocket(String arg1, int arg2) throws IOException , 49 UnknownHostException { 50 if (isSslEnabled()) { 51 return getSSLFactory().createSocket(arg1, arg2); 52 } else { 53 return new Socket (arg1, arg2); 54 } 55 } 56 57 public Socket createSocket(String arg1, int arg2, InetAddress arg3, int arg4) 58 throws IOException , UnknownHostException { 59 if (isSslEnabled()) { 60 return getSSLFactory().createSocket(arg1, arg2, arg3, arg4); 61 } else { 62 return new Socket (arg1, arg2, arg3, arg4); 63 } 64 } 65 66 public Socket createSocket(InetAddress arg1, int arg2) throws IOException { 67 if (isSslEnabled()) { 68 return getSSLFactory().createSocket(arg1, arg2); 69 } else { 70 return new Socket (arg1, arg2); 71 } 72 } 73 74 public Socket createSocket(InetAddress arg1, int arg2, InetAddress arg3, 75 int arg4) throws IOException { 76 if (isSslEnabled()) { 77 return getSSLFactory().createSocket(arg1, arg2, arg3, arg4); 78 } else { 79 return new Socket (arg1, arg2, arg3, arg4); 80 } 81 } 82 83 public static javax.net.SocketFactory getSocketFactory() { 84 if (factory == null) { 85 factory = new SSLSocketFactory(); 86 } 87 return factory; 88 } 89 90 private javax.net.ssl.SSLSocketFactory getSSLFactory() { 91 if (sslFactory == null) { 92 try { 93 sslFactory = BogusSSLContextFactory.getInstance(false) 94 .getSocketFactory(); 95 } catch (GeneralSecurityException e) { 96 throw new RuntimeException ("could not create SSL socket", e); 97 } 98 } 99 return sslFactory; 100 } 101 102 public static boolean isSslEnabled() { 103 return sslEnabled; 104 } 105 106 public static void setSslEnabled(boolean newSslEnabled) { 107 sslEnabled = newSslEnabled; 108 } 109 110 } 111 | Popular Tags |