7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
authorchegar
Fri, 02 Dec 2011 14:17:08 +0000
changeset 11124 5fdabdc21259
parent 11123 399112af8803
child 11125 99b115114fa3
7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers Reviewed-by: alanb, lancea
jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java
jdk/src/share/classes/javax/script/ScriptEngineManager.java
jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java
--- a/jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java	Fri Dec 02 11:39:48 2011 +0000
+++ b/jdk/src/share/classes/com/sun/net/httpserver/spi/HttpServerProvider.java	Fri Dec 02 14:17:08 2011 +0000
@@ -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 @@
             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 @@
      * <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
--- a/jdk/src/share/classes/javax/script/ScriptEngineManager.java	Fri Dec 02 11:39:48 2011 +0000
+++ b/jdk/src/share/classes/javax/script/ScriptEngineManager.java	Fri Dec 02 14:17:08 2011 +0000
@@ -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 @@
     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());
--- a/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java	Fri Dec 02 11:39:48 2011 +0000
+++ b/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java	Fri Dec 02 14:17:08 2011 +0000
@@ -27,7 +27,7 @@
 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 @@
     }
 
     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;
     }