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.ServerSocket; 025import java.security.GeneralSecurityException; 026 027import javax.net.ServerSocketFactory; 028 029/** 030 * Simple Server Socket factory to create sockets with or without SSL enabled. 031 * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes) 032 * 033 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 034 */ 035public class SslServerSocketFactory extends javax.net.ServerSocketFactory { 036 private static boolean sslEnabled = false; 037 038 private static javax.net.ServerSocketFactory sslFactory = null; 039 040 private static ServerSocketFactory factory = null; 041 042 public SslServerSocketFactory() { 043 super(); 044 } 045 046 @Override 047 public ServerSocket createServerSocket(int port) throws IOException { 048 return new ServerSocket(port); 049 } 050 051 @Override 052 public ServerSocket createServerSocket(int port, int backlog) 053 throws IOException { 054 return new ServerSocket(port, backlog); 055 } 056 057 @Override 058 public ServerSocket createServerSocket(int port, int backlog, 059 InetAddress ifAddress) throws IOException { 060 return new ServerSocket(port, backlog, ifAddress); 061 } 062 063 public static javax.net.ServerSocketFactory getServerSocketFactory() 064 throws IOException { 065 if (isSslEnabled()) { 066 if (sslFactory == null) { 067 try { 068 sslFactory = BogusSslContextFactory.getInstance(true) 069 .getServerSocketFactory(); 070 } catch (GeneralSecurityException e) { 071 IOException ioe = new IOException( 072 "could not create SSL socket"); 073 ioe.initCause(e); 074 throw ioe; 075 } 076 } 077 return sslFactory; 078 } else { 079 if (factory == null) { 080 factory = new SslServerSocketFactory(); 081 } 082 return factory; 083 } 084 085 } 086 087 public static boolean isSslEnabled() { 088 return sslEnabled; 089 } 090 091 public static void setSslEnabled(boolean newSslEnabled) { 092 sslEnabled = newSslEnabled; 093 } 094}