提交 109708ab 编写于 作者: C chegar

7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader...

7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
Reviewed-by: alanb, lancea
上级 4ea7c751
......@@ -25,50 +25,58 @@
package com.sun.net.httpserver.spi;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.ServiceLoader;
import sun.misc.ServiceConfigurationError;
import sun.security.action.GetPropertyAction;
import java.util.ServiceConfigurationError;
import com.sun.net.httpserver.*;
/**
* Service provider class for HttpServer.
* Sub-classes of HttpServerProvider provide an implementation of {@link HttpServer} and
* associated classes. Applications do not normally use this class.
* See {@link #provider()} for how providers are found and loaded.
* Sub-classes of HttpServerProvider provide an implementation of
* {@link HttpServer} and associated classes. Applications do not normally use
* this class. See {@link #provider()} for how providers are found and loaded.
*/
public abstract class HttpServerProvider {
/**
* creates a HttpServer from this provider
* @param addr the address to bind to. May be <code>null</code>
* @param backlog the socket backlog. A value of <code>zero</code> means the systems default
*
* @param addr
* the address to bind to. May be {@code null}
*
* @param backlog
* the socket backlog. A value of {@code zero} means the systems default
*/
public abstract HttpServer createHttpServer (InetSocketAddress addr, int backlog) throws IOException;
public abstract HttpServer createHttpServer(InetSocketAddress addr,
int backlog)
throws IOException;
/**
* creates a HttpsServer from this provider
* @param addr the address to bind to. May be <code>null</code>
* @param backlog the socket backlog. A value of <code>zero</code> means the systems default
*
* @param addr
* the address to bind to. May be {@code null}
*
* @param backlog
* the socket backlog. A value of {@code zero} means the systems default
*/
public abstract HttpsServer createHttpsServer (InetSocketAddress addr, int backlog) throws IOException;
public abstract HttpsServer createHttpsServer(InetSocketAddress addr,
int backlog)
throws IOException;
private static final Object lock = new Object();
private static HttpServerProvider provider = null;
/**
* Initializes a new instance of this class. </p>
* Initializes a new instance of this class.
*
* @throws SecurityException
* If a security manager has been installed and it denies
* {@link RuntimePermission}<tt>("httpServerProvider")</tt>
* {@link RuntimePermission}{@code("httpServerProvider")}
*/
protected HttpServerProvider() {
SecurityManager sm = System.getSecurityManager();
......@@ -82,21 +90,21 @@ public abstract class HttpServerProvider {
return false;
try {
Class<?> c = Class.forName(cn, true,
ClassLoader.getSystemClassLoader());
ClassLoader.getSystemClassLoader());
provider = (HttpServerProvider)c.newInstance();
return true;
} catch (ClassNotFoundException |
IllegalAccessException |
InstantiationException |
SecurityException x) {
throw new ServiceConfigurationError(x);
throw new ServiceConfigurationError(null, x);
}
}
private static boolean loadProviderAsService() {
Iterator<HttpServerProvider> i =
ServiceLoader.load(HttpServerProvider.class,
ClassLoader.getSystemClassLoader())
ClassLoader.getSystemClassLoader())
.iterator();
for (;;) {
try {
......@@ -124,19 +132,19 @@ public abstract class HttpServerProvider {
* <ol>
*
* <li><p> If the system property
* <tt>com.sun.net.httpserver.HttpServerProvider</tt> is defined then it is
* taken to be the fully-qualified name of a concrete provider class.
* {@code com.sun.net.httpserver.HttpServerProvider} is defined then it
* is taken to be the fully-qualified name of a concrete provider class.
* The class is loaded and instantiated; if this process fails then an
* unspecified unchecked error or exception is thrown. </p></li>
*
* <li><p> If a provider class has been installed in a jar file that is
* visible to the system class loader, and that jar file contains a
* provider-configuration file named
* <tt>com.sun.net.httpserver.HttpServerProvider</tt> in the resource
* {@code com.sun.net.httpserver.HttpServerProvider} in the resource
* directory <tt>META-INF/services</tt>, then the first class name
* specified in that file is taken. The class is loaded and
* instantiated; if this process fails then an unspecified unchecked error or exception is
* thrown. </p></li>
* instantiated; if this process fails then an unspecified unchecked error
* or exception is thrown. </p></li>
*
* <li><p> Finally, if no provider has been specified by any of the above
* means then the system-default provider class is instantiated and the
......
......@@ -25,11 +25,9 @@
package javax.script;
import java.util.*;
import java.net.URL;
import java.io.*;
import java.security.*;
import sun.misc.Service;
import sun.misc.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.ServiceConfigurationError;
import sun.reflect.Reflection;
import sun.security.util.SecurityConstants;
......@@ -104,11 +102,13 @@ public class ScriptEngineManager {
private void initEngines(final ClassLoader loader) {
Iterator<ScriptEngineFactory> itr = null;
try {
ServiceLoader<ScriptEngineFactory> sl;
if (loader != null) {
itr = Service.providers(ScriptEngineFactory.class, loader);
sl = ServiceLoader.load(ScriptEngineFactory.class, loader);
} else {
itr = Service.installedProviders(ScriptEngineFactory.class);
sl = ServiceLoader.loadInstalled(ScriptEngineFactory.class);
}
itr = sl.iterator();
} catch (ServiceConfigurationError err) {
System.err.println("Can't find ScriptEngineFactory providers: " +
err.getMessage());
......
......@@ -27,7 +27,7 @@ package sun.net.ftp;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ServiceConfigurationError;
//import sun.misc.Service;
//import java.util.ServiceLoader;
/**
* Service provider class for FtpClient.
......@@ -79,20 +79,22 @@ public abstract class FtpClientProvider {
}
private static boolean loadProviderAsService() {
// Iterator i = Service.providers(FtpClientProvider.class,
// ClassLoader.getSystemClassLoader());
// while (i.hasNext()) {
// try {
// provider = (FtpClientProvider) i.next();
// return true;
// } catch (ServiceConfigurationError sce) {
// if (sce.getCause() instanceof SecurityException) {
// // Ignore, try next provider, if any
// continue;
// }
// throw sce;
// }
// }
// Iterator<FtpClientProvider> i =
// ServiceLoader.load(FtpClientProvider.class,
// ClassLoader.getSystemClassLoader()).iterator();
//
// while (i.hasNext()) {
// try {
// provider = i.next();
// return true;
// } catch (ServiceConfigurationError sce) {
// if (sce.getCause() instanceof SecurityException) {
// // Ignore, try next provider, if any
// continue;
// }
// throw sce;
// }
// }
return false;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册