jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
child 11119 6ff03c1202ce
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) 2005, 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
package com.sun.net.httpserver.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.misc.ServiceConfigurationError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Service provider class for HttpServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Sub-classes of HttpServerProvider provide an implementation of {@link HttpServer} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * associated classes. Applications do not normally use this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * See {@link #provider()} for how providers are found and loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public abstract class HttpServerProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * creates a HttpServer from this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param addr the address to bind to. May be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param backlog the socket backlog. A value of <code>zero</code> means the systems default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public abstract HttpServer createHttpServer (InetSocketAddress addr, int backlog) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * creates a HttpsServer from this provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param addr the address to bind to. May be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param backlog the socket backlog. A value of <code>zero</code> means the systems default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public abstract HttpsServer createHttpsServer (InetSocketAddress addr, int backlog) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static HttpServerProvider provider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Initializes a new instance of this class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *          If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *          {@link RuntimePermission}<tt>("httpServerProvider")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected HttpServerProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            sm.checkPermission(new RuntimePermission("httpServerProvider"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static boolean loadProviderFromProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (cn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    84
            Class<?> c = Class.forName(cn, true,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                    ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            provider = (HttpServerProvider)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return true;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    88
        } catch (ClassNotFoundException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    89
                 IllegalAccessException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    90
                 InstantiationException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    91
                 SecurityException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new ServiceConfigurationError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static boolean loadProviderAsService() {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    97
        @SuppressWarnings("unchecked")
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    98
        Iterator<HttpServerProvider> i = Service.providers(HttpServerProvider.class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                       ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (!i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return false;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   104
                provider = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } catch (ServiceConfigurationError sce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (sce.getCause() instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    // Ignore the security exception, try the next provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw sce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns the system wide default HttpServerProvider for this invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p> The first invocation of this method locates the default provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * object as follows: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *   <li><p> If the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *   <tt>com.sun.net.httpserver.HttpServerProvider</tt> is defined then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *   taken to be the fully-qualified name of a concrete provider class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *   The class is loaded and instantiated; if this process fails then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *   unspecified unchecked error or exception is thrown.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *   <li><p> If a provider class has been installed in a jar file that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *   visible to the system class loader, and that jar file contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *   provider-configuration file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *   <tt>com.sun.net.httpserver.HttpServerProvider</tt> in the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *   directory <tt>META-INF/services</tt>, then the first class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *   specified in that file is taken.  The class is loaded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *   instantiated; if this process fails then an unspecified unchecked error or exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *   thrown.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *   <li><p> Finally, if no provider has been specified by any of the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *   means then the system-default provider class is instantiated and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *   result is returned.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p> Subsequent invocations of this method return the provider that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * returned by the first invocation.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return  The system-wide default HttpServerProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static HttpServerProvider provider () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (provider != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return (HttpServerProvider)AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                .doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            if (loadProviderFromProperty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            if (loadProviderAsService())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            provider = new sun.net.httpserver.DefaultHttpServerProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}