001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.echoserver.ssl;
021:
022: import java.io.IOException;
023: import java.net.InetAddress;
024: import java.net.Socket;
025: import java.net.UnknownHostException;
026: import java.security.GeneralSecurityException;
027:
028: import javax.net.SocketFactory;
029:
030: /**
031: * Simple Socket factory to create sockets with or without SSL enabled.
032: * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes)
033: *
034: * @author The Apache MINA Project (dev@mina.apache.org)
035: * @version $Rev: 576647 $, $Date: 2007-09-17 19:41:29 -0600 (Mon, 17 Sep 2007) $
036: */
037: public class SslSocketFactory extends SocketFactory {
038: private static boolean sslEnabled = false;
039:
040: private static javax.net.ssl.SSLSocketFactory sslFactory = null;
041:
042: private static javax.net.SocketFactory factory = null;
043:
044: public SslSocketFactory() {
045: super ();
046: }
047:
048: @Override
049: public Socket createSocket(String arg1, int arg2)
050: throws IOException, UnknownHostException {
051: if (isSslEnabled()) {
052: return getSSLFactory().createSocket(arg1, arg2);
053: } else {
054: return new Socket(arg1, arg2);
055: }
056: }
057:
058: @Override
059: public Socket createSocket(String arg1, int arg2, InetAddress arg3,
060: int arg4) throws IOException, UnknownHostException {
061: if (isSslEnabled()) {
062: return getSSLFactory().createSocket(arg1, arg2, arg3, arg4);
063: } else {
064: return new Socket(arg1, arg2, arg3, arg4);
065: }
066: }
067:
068: @Override
069: public Socket createSocket(InetAddress arg1, int arg2)
070: throws IOException {
071: if (isSslEnabled()) {
072: return getSSLFactory().createSocket(arg1, arg2);
073: } else {
074: return new Socket(arg1, arg2);
075: }
076: }
077:
078: @Override
079: public Socket createSocket(InetAddress arg1, int arg2,
080: InetAddress arg3, int arg4) throws IOException {
081: if (isSslEnabled()) {
082: return getSSLFactory().createSocket(arg1, arg2, arg3, arg4);
083: } else {
084: return new Socket(arg1, arg2, arg3, arg4);
085: }
086: }
087:
088: public static javax.net.SocketFactory getSocketFactory() {
089: if (factory == null) {
090: factory = new SslSocketFactory();
091: }
092: return factory;
093: }
094:
095: private javax.net.ssl.SSLSocketFactory getSSLFactory() {
096: if (sslFactory == null) {
097: try {
098: sslFactory = BogusSslContextFactory.getInstance(false)
099: .getSocketFactory();
100: } catch (GeneralSecurityException e) {
101: throw new RuntimeException(
102: "could not create SSL socket", e);
103: }
104: }
105: return sslFactory;
106: }
107:
108: public static boolean isSslEnabled() {
109: return sslEnabled;
110: }
111:
112: public static void setSslEnabled(boolean newSslEnabled) {
113: sslEnabled = newSslEnabled;
114: }
115:
116: }
|