src/jdk.httpserver/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java
author darcy
Tue, 12 Nov 2019 10:45:23 -0800
changeset 59037 3d2575331a41
parent 58514 270fdb5898f2
permissions -rw-r--r--
8233940: Preview API tests for String methods should use ${jdk.version} as -source arg Reviewed-by: jlaskey, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58514
270fdb5898f2 8214560: Use {@systemProperty} for definitions of system properties (httpserver)
coffeys
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2019, 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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Iterator;
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10596
diff changeset
    33
import java.util.ServiceLoader;
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    34
import java.util.ServiceConfigurationError;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Service provider class for HttpServer.
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    39
 * Sub-classes of HttpServerProvider provide an implementation of
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    40
 * {@link HttpServer} and associated classes. Applications do not normally use
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    41
 * this class. See {@link #provider()} for how providers are found and loaded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class HttpServerProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * creates a HttpServer from this provider
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    47
     *
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    48
     * @param  addr
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    49
     *         the address to bind to. May be {@code null}
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    50
     *
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    51
     * @param  backlog
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    52
     *         the socket backlog. A value of {@code zero} means the systems default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    54
    public abstract HttpServer createHttpServer(InetSocketAddress addr,
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    55
                                                int backlog)
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    56
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * creates a HttpsServer from this provider
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    60
     *
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    61
     * @param  addr
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    62
     *         the address to bind to. May be {@code null}
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    63
     *
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    64
     * @param  backlog
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    65
     *         the socket backlog. A value of {@code zero} means the systems default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    67
    public abstract HttpsServer createHttpsServer(InetSocketAddress addr,
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    68
                                                  int backlog)
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    69
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static HttpServerProvider provider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    75
     * Initializes a new instance of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *          If a security manager has been installed and it denies
12041
c6c40d1330c1 7145043: HttpServerProvider.java:81: warning - @code("httpServerProvider") is an unknown tag
chegar
parents: 11124
diff changeset
    79
     *          {@link RuntimePermission}{@code ("httpServerProvider")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected HttpServerProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            sm.checkPermission(new RuntimePermission("httpServerProvider"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static boolean loadProviderFromProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (cn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34894
diff changeset
    92
            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34894
diff changeset
    93
            Object o = Class.forName(cn, true,
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34894
diff changeset
    94
                                     ClassLoader.getSystemClassLoader()).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 34894
diff changeset
    95
            provider = (HttpServerProvider)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return true;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    97
        } catch (ClassNotFoundException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    98
                 IllegalAccessException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    99
                 InstantiationException |
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   100
                 SecurityException x) {
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   101
            throw new ServiceConfigurationError(null, x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static boolean loadProviderAsService() {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10596
diff changeset
   106
        Iterator<HttpServerProvider> i =
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10596
diff changeset
   107
            ServiceLoader.load(HttpServerProvider.class,
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   108
                               ClassLoader.getSystemClassLoader())
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10596
diff changeset
   109
                .iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (!i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    return false;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   114
                provider = i.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } catch (ServiceConfigurationError sce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (sce.getCause() instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    // Ignore the security exception, try the next provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                throw sce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Returns the system wide default HttpServerProvider for this invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * the Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p> The first invocation of this method locates the default provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * object as follows: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *   <li><p> If the system property
58514
270fdb5898f2 8214560: Use {@systemProperty} for definitions of system properties (httpserver)
coffeys
parents: 47216
diff changeset
   136
     *   {@systemProperty com.sun.net.httpserver.HttpServerProvider}
270fdb5898f2 8214560: Use {@systemProperty} for definitions of system properties (httpserver)
coffeys
parents: 47216
diff changeset
   137
     *   is defined then it is taken to be the fully-qualified name
270fdb5898f2 8214560: Use {@systemProperty} for definitions of system properties (httpserver)
coffeys
parents: 47216
diff changeset
   138
     *   of a concrete provider class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *   The class is loaded and instantiated; if this process fails then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *   unspecified unchecked error or exception is thrown.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *   <li><p> If a provider class has been installed in a jar file that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *   visible to the system class loader, and that jar file contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *   provider-configuration file named
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   145
     *   {@code com.sun.net.httpserver.HttpServerProvider} in the resource
32210
958d823579c3 8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents: 25859
diff changeset
   146
     *   directory {@code META-INF/services}, then the first class name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *   specified in that file is taken.  The class is loaded and
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   148
     *   instantiated; if this process fails then an unspecified unchecked error
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   149
     *   or exception is thrown.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *   <li><p> Finally, if no provider has been specified by any of the above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *   means then the system-default provider class is instantiated and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *   result is returned.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p> Subsequent invocations of this method return the provider that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * returned by the first invocation.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return  The system-wide default HttpServerProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static HttpServerProvider provider () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (provider != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return (HttpServerProvider)AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                .doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            if (loadProviderFromProperty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            if (loadProviderAsService())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            provider = new sun.net.httpserver.DefaultHttpServerProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}