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.Socket;
     23 import java.net.UnknownHostException;
     24 import java.security.KeyManagementException;
     25 import javax.net.ssl.SSLSocketFactory;
     26 
     27 /**
     28  * Implementation of SSLSocketFactory.
     29  */
     30 public class SSLSocketFactoryImpl extends SSLSocketFactory {
     31 
     32     private SSLParametersImpl sslParameters;
     33     private IOException instantiationException;
     34 
     35     /**
     36      * Constructor.
     37      */
     38     public SSLSocketFactoryImpl() {
     39         super();
     40         try {
     41             sslParameters = SSLParametersImpl.getDefault();
     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 SSLSocketFactoryImpl(SSLParametersImpl sslParameters) {
     53         super();
     54         this.sslParameters = sslParameters;
     55     }
     56 
     57     /**
     58      * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
     59      */
     60     @Override
     61     public String[] getDefaultCipherSuites() {
     62         if (instantiationException != null) {
     63             return new String[0];
     64         }
     65         return sslParameters.getEnabledCipherSuites();
     66     }
     67 
     68     /**
     69      * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
     70      */
     71     @Override
     72     public String[] getSupportedCipherSuites() {
     73         if (instantiationException != null) {
     74             return new String[0];
     75         }
     76         return CipherSuite.getSupportedCipherSuiteNames();
     77     }
     78 
     79     /**
     80      * @see javax.net.ssl.SSLSocketFactory#createSocket(Socket,String,int,boolean)
     81      */
     82     @Override
     83     public Socket createSocket(Socket s, String host, int port,
     84             boolean autoClose) throws IOException {
     85         if (instantiationException != null) {
     86             throw instantiationException;
     87         }
     88         return new SSLSocketWrapper(s, autoClose, (SSLParametersImpl) sslParameters
     89                 .clone());
     90     }
     91 
     92     // -------------- Methods inherided from SocketFactory --------------
     93 
     94     /**
     95      * @see javax.net.SocketFactory#createSocket()
     96      */
     97     @Override
     98     public Socket createSocket() throws IOException {
     99         if (instantiationException != null) {
    100             throw instantiationException;
    101         }
    102         return new SSLSocketImpl((SSLParametersImpl) sslParameters.clone());
    103     }
    104 
    105     /**
    106      * @see javax.net.SocketFactory#createSocket(String,int)
    107      */
    108     @Override
    109     public Socket createSocket(String host, int port)
    110             throws IOException, UnknownHostException {
    111         if (instantiationException != null) {
    112             throw instantiationException;
    113         }
    114         return new SSLSocketImpl(host, port,
    115                 (SSLParametersImpl) sslParameters.clone());
    116     }
    117 
    118     /**
    119      * @see javax.net.SocketFactory#createSocket(String,int,InetAddress,int)
    120      */
    121     @Override
    122     public Socket createSocket(String host, int port,
    123             InetAddress localHost, int localPort) throws IOException,
    124             UnknownHostException {
    125         if (instantiationException != null) {
    126             throw instantiationException;
    127         }
    128         return new SSLSocketImpl(host, port, localHost, localPort,
    129                 (SSLParametersImpl) sslParameters.clone());
    130     }
    131 
    132     /**
    133      * @see javax.net.SocketFactory#createSocket(InetAddress,int)
    134      */
    135     @Override
    136     public Socket createSocket(InetAddress host, int port)
    137             throws IOException {
    138         if (instantiationException != null) {
    139             throw instantiationException;
    140         }
    141         return new SSLSocketImpl(host, port,
    142                 (SSLParametersImpl) sslParameters.clone());
    143     }
    144 
    145     /**
    146      * @see javax.net.SocketFactory#createSocket(InetAddress,int,InetAddress,int)
    147      */
    148     @Override
    149     public Socket createSocket(InetAddress address, int port,
    150             InetAddress localAddress, int localPort) throws IOException {
    151         if (instantiationException != null) {
    152             throw instantiationException;
    153         }
    154         return new SSLSocketImpl(address, port, localAddress, localPort,
    155                 (SSLParametersImpl) sslParameters.clone());
    156     }
    157 
    158     // ------------------------------------------------------------------
    159 }
    160 
    161