jdk/src/share/classes/javax/net/ssl/SSLServerSocketFactory.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
child 14514 46a8ed03c7fc
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.net.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>SSLServerSocketFactory</code>s create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>SSLServerSocket</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see SSLSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see SSLServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public abstract class SSLServerSocketFactory extends ServerSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static SSLServerSocketFactory theFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static boolean propertyChecked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void log(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (SSLSocketFactory.DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Constructor is used only by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected SSLServerSocketFactory() { /* NOTHING */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Returns the default SSL server socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <p>The first time this method is called, the security property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * "ssl.ServerSocketFactory.provider" is examined. If it is non-null, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * class by that name is loaded and instantiated. If that is successful and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * the object is an instance of SSLServerSocketFactory, it is made the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * default SSL server socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>Otherwise, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <code>SSLContext.getDefault().getServerSocketFactory()</code>. If that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * call fails, an inoperative factory is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return the default <code>ServerSocketFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @see SSLContext#getDefault
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static synchronized ServerSocketFactory getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (theFactory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return theFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (propertyChecked == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            propertyChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            String clsName = SSLSocketFactory.getSecurityProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                        ("ssl.ServerSocketFactory.provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (clsName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                log("setting up default SSLServerSocketFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                try {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    90
                    Class<?> cls = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        cls = Class.forName(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                            cls = cl.loadClass(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    log("class " + clsName + " is loaded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    SSLServerSocketFactory fac = (SSLServerSocketFactory)cls.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    log("instantiated an instance of class " + clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    theFactory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    log("SSLServerSocketFactory instantiation failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    theFactory = new DefaultSSLServerSocketFactory(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    return theFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return SSLContext.getDefault().getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return new DefaultSSLServerSocketFactory(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns the list of cipher suites which are enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Unless a different list is enabled, handshaking on an SSL connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * will use one of these cipher suites.  The minimum quality of service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * for these defaults requires confidentiality protection and server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * authentication (that is, no anonymous cipher suites).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see #getSupportedCipherSuites()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return array of the cipher suites enabled by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public abstract String [] getDefaultCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns the names of the cipher suites which could be enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * on an SSL connection created by this factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Normally, only a subset of these will actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * be enabled by default, since this list may include cipher suites which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * do not meet quality of service requirements for those defaults.  Such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * cipher suites are useful in specialized applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return an array of cipher suite names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see #getDefaultCipherSuites()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public abstract String [] getSupportedCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
// The default factory does NOTHING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
class DefaultSSLServerSocketFactory extends SSLServerSocketFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private final Exception reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    DefaultSSLServerSocketFactory(Exception reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.reason = reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private ServerSocket throwException() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        throw (SocketException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            new SocketException(reason.toString()).initCause(reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public ServerSocket createServerSocket() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public ServerSocket createServerSocket(int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public ServerSocket createServerSocket(int port, int backlog)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    createServerSocket(int port, int backlog, InetAddress ifAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public String [] getDefaultCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public String [] getSupportedCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}