From 6303a14f1ae73fa0e6b17c027c5ae29cab7fba3d Mon Sep 17 00:00:00 2001 From: Chris Hegarty Date: Fri, 2 Dec 2011 14:17:08 +0000 Subject: [PATCH] 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers Reviewed-by: alanb, lancea --- .../httpserver/spi/HttpServerProvider.java | 56 +++++++++++-------- .../javax/script/ScriptEngineManager.java | 12 ++-- .../sun/net/ftp/FtpClientProvider.java | 32 ++++++----- 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java b/jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java index cfc80c534d9..7b5c05331fe 100644 --- a/jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java +++ b/jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java @@ -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 null - * @param backlog the socket backlog. A value of zero 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 null - * @param backlog the socket backlog. A value of zero 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.

+ * Initializes a new instance of this class. * * @throws SecurityException * If a security manager has been installed and it denies - * {@link RuntimePermission}("httpServerProvider") + * {@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 i = ServiceLoader.load(HttpServerProvider.class, - ClassLoader.getSystemClassLoader()) + ClassLoader.getSystemClassLoader()) .iterator(); for (;;) { try { @@ -124,19 +132,19 @@ public abstract class HttpServerProvider { *
    * *
  1. If the system property - * com.sun.net.httpserver.HttpServerProvider 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.

  2. * *
  3. 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 - * com.sun.net.httpserver.HttpServerProvider in the resource + * {@code com.sun.net.httpserver.HttpServerProvider} in the resource * directory META-INF/services, 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.

  4. + * instantiated; if this process fails then an unspecified unchecked error + * or exception is thrown.

    * *
  5. Finally, if no provider has been specified by any of the above * means then the system-default provider class is instantiated and the diff --git a/jdk/src/share/classes/javax/script/ScriptEngineManager.java b/jdk/src/share/classes/javax/script/ScriptEngineManager.java index 9dfb6fd9221..bb43053426c 100644 --- a/jdk/src/share/classes/javax/script/ScriptEngineManager.java +++ b/jdk/src/share/classes/javax/script/ScriptEngineManager.java @@ -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 itr = null; try { + ServiceLoader 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()); diff --git a/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java b/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java index 7a09810718c..06deae65817 100644 --- a/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java +++ b/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java @@ -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 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; }