author | darcy |
Tue, 12 Nov 2019 10:45:23 -0800 | |
changeset 59037 | 3d2575331a41 |
parent 58514 | 270fdb5898f2 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.net.httpserver.spi; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.net.*; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.security.PrivilegedAction; |
|
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 | 35 |
import com.sun.net.httpserver.*; |
36 |
||
37 |
/** |
|
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 | 42 |
*/ |
43 |
public abstract class HttpServerProvider { |
|
44 |
||
45 |
/** |
|
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 | 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 | 57 |
|
58 |
/** |
|
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 | 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 | 70 |
|
71 |
private static final Object lock = new Object(); |
|
72 |
private static HttpServerProvider provider = null; |
|
73 |
||
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 | 76 |
* |
77 |
* @throws SecurityException |
|
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 | 80 |
*/ |
81 |
protected HttpServerProvider() { |
|
82 |
SecurityManager sm = System.getSecurityManager(); |
|
83 |
if (sm != null) |
|
84 |
sm.checkPermission(new RuntimePermission("httpServerProvider")); |
|
85 |
} |
|
86 |
||
87 |
private static boolean loadProviderFromProperty() { |
|
88 |
String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider"); |
|
89 |
if (cn == null) |
|
90 |
return false; |
|
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 | 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 | 102 |
} |
103 |
} |
|
104 |
||
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 | 110 |
for (;;) { |
111 |
try { |
|
112 |
if (!i.hasNext()) |
|
113 |
return false; |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
114 |
provider = i.next(); |
2 | 115 |
return true; |
116 |
} catch (ServiceConfigurationError sce) { |
|
117 |
if (sce.getCause() instanceof SecurityException) { |
|
118 |
// Ignore the security exception, try the next provider |
|
119 |
continue; |
|
120 |
} |
|
121 |
throw sce; |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Returns the system wide default HttpServerProvider for this invocation of |
|
128 |
* the Java virtual machine. |
|
129 |
* |
|
130 |
* <p> The first invocation of this method locates the default provider |
|
131 |
* object as follows: </p> |
|
132 |
* |
|
133 |
* <ol> |
|
134 |
* |
|
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 | 139 |
* The class is loaded and instantiated; if this process fails then an |
140 |
* unspecified unchecked error or exception is thrown. </p></li> |
|
141 |
* |
|
142 |
* <li><p> If a provider class has been installed in a jar file that is |
|
143 |
* visible to the system class loader, and that jar file contains a |
|
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 | 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 | 150 |
* |
151 |
* <li><p> Finally, if no provider has been specified by any of the above |
|
152 |
* means then the system-default provider class is instantiated and the |
|
153 |
* result is returned. </p></li> |
|
154 |
* |
|
155 |
* </ol> |
|
156 |
* |
|
157 |
* <p> Subsequent invocations of this method return the provider that was |
|
158 |
* returned by the first invocation. </p> |
|
159 |
* |
|
160 |
* @return The system-wide default HttpServerProvider |
|
161 |
*/ |
|
162 |
public static HttpServerProvider provider () { |
|
163 |
synchronized (lock) { |
|
164 |
if (provider != null) |
|
165 |
return provider; |
|
166 |
return (HttpServerProvider)AccessController |
|
167 |
.doPrivileged(new PrivilegedAction<Object>() { |
|
168 |
public Object run() { |
|
169 |
if (loadProviderFromProperty()) |
|
170 |
return provider; |
|
171 |
if (loadProviderAsService()) |
|
172 |
return provider; |
|
173 |
provider = new sun.net.httpserver.DefaultHttpServerProvider(); |
|
174 |
return provider; |
|
175 |
} |
|
176 |
}); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
} |