Home | History | Annotate | Download | only in jsse
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import java.io.IOException;
     21 import java.net.InetAddress;
     22 import java.net.ServerSocket;
     23 import java.security.KeyManagementException;
     24 import javax.net.ssl.SSLServerSocketFactory;
     25 
     26 /**
     27  * Implementation of SSLServerSocketFactory.
     28  */
     29 public class SSLServerSocketFactoryImpl extends SSLServerSocketFactory {
     30 
     31     private SSLParametersImpl sslParameters;
     32     private IOException instantiationException;
     33 
     34     /**
     35      * Constructor.
     36      */
     37     public SSLServerSocketFactoryImpl() {
     38         super();
     39         try {
     40             this.sslParameters = SSLParametersImpl.getDefault();
     41             this.sslParameters.setUseClientMode(false);
     42         } catch (KeyManagementException e) {
     43             instantiationException =
     44                 new IOException("Delayed instantiation exception:");
     45             instantiationException.initCause(e);
     46         }
     47     }
     48 
     49     /**
     50      * Constructor.
     51      */
     52     protected SSLServerSocketFactoryImpl(SSLParametersImpl sslParameters) {
     53         super();
     54         this.sslParameters = (SSLParametersImpl) sslParameters.clone();
     55         this.sslParameters.setUseClientMode(false);
     56     }
     57 
     58     /**
     59      * @see javax.net.ssl.SSLServerSocketFactory#getDefaultCipherSuites()
     60      */
     61     @Override
     62     public String[] getDefaultCipherSuites() {
     63         if (instantiationException != null) {
     64             return new String[0];
     65         }
     66         return sslParameters.getEnabledCipherSuites();
     67     }
     68 
     69     /**
     70      * @see javax.net.ssl.SSLServerSocketFactory#getSupportedCipherSuites()
     71      */
     72     @Override
     73     public String[] getSupportedCipherSuites() {
     74         if (instantiationException != null) {
     75             return new String[0];
     76         }
     77         return CipherSuite.getSupportedCipherSuiteNames();
     78     }
     79 
     80     /**
     81      * @see javax.net.ServerSocketFactory#createServerSocket()
     82      */
     83     @Override
     84     public ServerSocket createServerSocket() throws IOException {
     85         if (instantiationException != null) {
     86             throw instantiationException;
     87         }
     88         return new SSLServerSocketImpl((SSLParametersImpl) sslParameters.clone());
     89     }
     90 
     91 
     92     /**
     93      * @see javax.net.ServerSocketFactory#createServerSocket(int)
     94      */
     95     @Override
     96     public ServerSocket createServerSocket(int port) throws IOException {
     97         if (instantiationException != null) {
     98             throw instantiationException;
     99         }
    100         return new SSLServerSocketImpl(port,
    101                 (SSLParametersImpl) sslParameters.clone());
    102     }
    103 
    104     /**
    105      * @see javax.net.ServerSocketFactory#createServerSocket(int,int)
    106      */
    107     @Override
    108     public ServerSocket createServerSocket(int port, int backlog)
    109             throws IOException {
    110         if (instantiationException != null) {
    111             throw instantiationException;
    112         }
    113         return new SSLServerSocketImpl(port, backlog,
    114                 (SSLParametersImpl) sslParameters.clone());
    115     }
    116 
    117     /**
    118      * @see javax.net.ServerSocketFactory#createServerSocket(int,int,InetAddress)
    119      */
    120     @Override
    121     public ServerSocket createServerSocket(int port, int backlog,
    122             InetAddress iAddress) throws IOException {
    123         if (instantiationException != null) {
    124             throw instantiationException;
    125         }
    126         return new SSLServerSocketImpl(port, backlog, iAddress,
    127                 (SSLParametersImpl) sslParameters.clone());
    128     }
    129 }
    130 
    131