SSLServerSocketFactoryImpl.java revision 6186821cb13f4ac7ff50950c813394367e021eae
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
18package org.apache.harmony.xnet.provider.jsse;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.ServerSocket;
23import java.security.KeyManagementException;
24import javax.net.ssl.SSLServerSocketFactory;
25import libcore.util.EmptyArray;
26
27/**
28 * Implementation of SSLServerSocketFactory.
29 */
30public class SSLServerSocketFactoryImpl extends SSLServerSocketFactory {
31
32    private SSLParametersImpl sslParameters;
33    private IOException instantiationException;
34
35    /**
36     * Constructor.
37     */
38    public SSLServerSocketFactoryImpl() {
39        super();
40        try {
41            this.sslParameters = SSLParametersImpl.getDefault();
42            this.sslParameters.setUseClientMode(false);
43        } catch (KeyManagementException e) {
44            instantiationException =
45                new IOException("Delayed instantiation exception:");
46            instantiationException.initCause(e);
47        }
48    }
49
50    /**
51     * Constructor.
52     */
53    protected SSLServerSocketFactoryImpl(SSLParametersImpl sslParameters) {
54        super();
55        this.sslParameters = (SSLParametersImpl) sslParameters.clone();
56        this.sslParameters.setUseClientMode(false);
57    }
58
59    /**
60     * @see javax.net.ssl.SSLServerSocketFactory#getDefaultCipherSuites()
61     */
62    @Override
63    public String[] getDefaultCipherSuites() {
64        if (instantiationException != null) {
65            return EmptyArray.STRING;
66        }
67        return sslParameters.getEnabledCipherSuites();
68    }
69
70    /**
71     * @see javax.net.ssl.SSLServerSocketFactory#getSupportedCipherSuites()
72     */
73    @Override
74    public String[] getSupportedCipherSuites() {
75        if (instantiationException != null) {
76            return EmptyArray.STRING;
77        }
78        return CipherSuite.getSupportedCipherSuiteNames();
79    }
80
81    /**
82     * @see javax.net.ServerSocketFactory#createServerSocket()
83     */
84    @Override
85    public ServerSocket createServerSocket() throws IOException {
86        if (instantiationException != null) {
87            throw instantiationException;
88        }
89        return new SSLServerSocketImpl((SSLParametersImpl) sslParameters.clone());
90    }
91
92
93    /**
94     * @see javax.net.ServerSocketFactory#createServerSocket(int)
95     */
96    @Override
97    public ServerSocket createServerSocket(int port) throws IOException {
98        if (instantiationException != null) {
99            throw instantiationException;
100        }
101        return new SSLServerSocketImpl(port,
102                (SSLParametersImpl) sslParameters.clone());
103    }
104
105    /**
106     * @see javax.net.ServerSocketFactory#createServerSocket(int,int)
107     */
108    @Override
109    public ServerSocket createServerSocket(int port, int backlog)
110            throws IOException {
111        if (instantiationException != null) {
112            throw instantiationException;
113        }
114        return new SSLServerSocketImpl(port, backlog,
115                (SSLParametersImpl) sslParameters.clone());
116    }
117
118    /**
119     * @see javax.net.ServerSocketFactory#createServerSocket(int,int,InetAddress)
120     */
121    @Override
122    public ServerSocket createServerSocket(int port, int backlog,
123            InetAddress iAddress) throws IOException {
124        if (instantiationException != null) {
125            throw instantiationException;
126        }
127        return new SSLServerSocketImpl(port, backlog, iAddress,
128                (SSLParametersImpl) sslParameters.clone());
129    }
130}
131