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