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