SSLServerSocketFactory.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 javax.net.ssl;
19
20import java.security.AccessController;
21import java.security.Security;
22
23import javax.net.ServerSocketFactory;
24
25/**
26 * The factory for SSL server sockets.
27 *
28 * @since Android 1.0
29 */
30public abstract class SSLServerSocketFactory extends ServerSocketFactory {
31// TODO EXPORT CONTROL
32
33    // The default SSL socket factory
34    private static ServerSocketFactory defaultServerSocketFactory;
35
36    private static String defaultName;
37
38    /**
39     * Creates a new {@code SSLServerSocketFactory} instance.
40     *
41     * @since Android 1.0
42     */
43    protected SSLServerSocketFactory() {
44        super();
45    }
46
47    /**
48     * Returns the default {@code SSLServerSocketFactory} instance. The default
49     * implementation is defined by the security property
50     * "ssl.ServerSocketFactory.provider".
51     *
52     * @return the default {@code SSLServerSocketFactory} instance.
53     * @since Android 1.0
54     */
55    public static ServerSocketFactory getDefault() {
56        if (defaultServerSocketFactory != null) {
57            return defaultServerSocketFactory;
58        }
59        if (defaultName == null) {
60            AccessController.doPrivileged(new java.security.PrivilegedAction(){
61                public Object run() {
62                    defaultName = Security.getProperty("ssl.ServerSocketFactory.provider");
63                    if (defaultName != null) {
64                        ClassLoader cl = Thread.currentThread().getContextClassLoader();
65                        if (cl == null) {
66                            cl = ClassLoader.getSystemClassLoader();
67                        }
68                        try {
69                            defaultServerSocketFactory = (ServerSocketFactory) Class
70                                    .forName(defaultName, true, cl)
71                                    .newInstance();
72                        } catch (Exception e) {
73                            return e;
74                        }
75                    }
76                    return null;
77                }
78            });
79        }
80        if (defaultServerSocketFactory == null) {
81            // Try to find in providers
82            SSLContext context = DefaultSSLContext.getContext();
83            if (context != null) {
84                defaultServerSocketFactory = context.getServerSocketFactory();
85            }
86        }
87        if (defaultServerSocketFactory == null) {
88            // Use internal dummy implementation
89            defaultServerSocketFactory = new DefaultSSLServerSocketFactory("No ServerSocketFactory installed");
90        }
91        return defaultServerSocketFactory;
92    }
93
94    /**
95     * Returns the names of the cipher suites that are enabled by default.
96     *
97     * @return the names of the cipher suites that are enabled by default
98     * @since Android 1.0
99     */
100    public abstract String[] getDefaultCipherSuites();
101
102    /**
103     * Returns the list of supported cipher suites that could be enabled for an
104     * SSL connection created by this factory.
105     *
106     * @return the list of supported cipher suites
107     * @since Android 1.0
108     */
109    public abstract String[] getSupportedCipherSuites();
110
111}
112