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 */
020package org.apache.mina.example.echoserver.ssl;
021
022import java.io.IOException;
023import java.net.InetAddress;
024import java.net.Socket;
025import java.net.UnknownHostException;
026import java.security.GeneralSecurityException;
027
028import 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 <a href="http://mina.apache.org">Apache MINA Project</a>
035 */
036public class SslSocketFactory extends SocketFactory {
037    private static boolean sslEnabled = false;
038
039    private static javax.net.ssl.SSLSocketFactory sslFactory = null;
040
041    private static javax.net.SocketFactory factory = null;
042
043    public SslSocketFactory() {
044        super();
045    }
046
047    @Override
048    public Socket createSocket(String arg1, int arg2) throws IOException,
049            UnknownHostException {
050        if (isSslEnabled()) {
051            return getSSLFactory().createSocket(arg1, arg2);
052        } else {
053            return new Socket(arg1, arg2);
054        }
055    }
056
057    @Override
058    public Socket createSocket(String arg1, int arg2, InetAddress arg3, int arg4)
059            throws IOException, UnknownHostException {
060        if (isSslEnabled()) {
061            return getSSLFactory().createSocket(arg1, arg2, arg3, arg4);
062        } else {
063            return new Socket(arg1, arg2, arg3, arg4);
064        }
065    }
066
067    @Override
068    public Socket createSocket(InetAddress arg1, int arg2) throws IOException {
069        if (isSslEnabled()) {
070            return getSSLFactory().createSocket(arg1, arg2);
071        } else {
072            return new Socket(arg1, arg2);
073        }
074    }
075
076    @Override
077    public Socket createSocket(InetAddress arg1, int arg2, InetAddress arg3,
078            int arg4) throws IOException {
079        if (isSslEnabled()) {
080            return getSSLFactory().createSocket(arg1, arg2, arg3, arg4);
081        } else {
082            return new Socket(arg1, arg2, arg3, arg4);
083        }
084    }
085
086    public static javax.net.SocketFactory getSocketFactory() {
087        if (factory == null) {
088            factory = new SslSocketFactory();
089        }
090        return factory;
091    }
092
093    private javax.net.ssl.SSLSocketFactory getSSLFactory() {
094        if (sslFactory == null) {
095            try {
096                sslFactory = BogusSslContextFactory.getInstance(false)
097                        .getSocketFactory();
098            } catch (GeneralSecurityException e) {
099                throw new RuntimeException("could not create SSL socket", e);
100            }
101        }
102        return sslFactory;
103    }
104
105    public static boolean isSslEnabled() {
106        return sslEnabled;
107    }
108
109    public static void setSslEnabled(boolean newSslEnabled) {
110        sslEnabled = newSslEnabled;
111    }
112
113}