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