changeset 12639:baa9838343d8

8068749: Restrict javax.imageio.spi.ServiceRegistry to ImageIO types Reviewed-by: prr, serb
author smarks
date Thu, 13 Aug 2015 13:30:15 -0700
parents df4b74d0e6ee
children 8164bc5dd95e
files src/java.desktop/share/classes/javax/imageio/spi/IIOServiceProvider.java src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java src/java.desktop/share/classes/javax/imageio/spi/package.html test/javax/imageio/spi/ServiceRegistryRestriction.java
diffstat 4 files changed, 162 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/javax/imageio/spi/IIOServiceProvider.java	Thu Aug 13 12:27:06 2015 -0700
+++ b/src/java.desktop/share/classes/javax/imageio/spi/IIOServiceProvider.java	Thu Aug 13 13:30:15 2015 -0700
@@ -40,7 +40,7 @@
  * @see javax.imageio.spi.ImageWriterSpi
  * @see javax.imageio.spi.ImageTranscoderSpi
  * @see javax.imageio.spi.ImageInputStreamSpi
- *
+ * @see javax.imageio.spi.ImageOutputStreamSpi
  */
 public abstract class IIOServiceProvider implements RegisterableService {
 
--- a/src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java	Thu Aug 13 12:27:06 2015 -0700
+++ b/src/java.desktop/share/classes/javax/imageio/spi/ServiceRegistry.java	Thu Aug 13 13:30:15 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,18 +47,33 @@
  * <p> Service providers are stored in one or more <i>categories</i>,
  * each of which is defined by a class of interface (described by a
  * <code>Class</code> object) that all of its members must implement.
- * The set of categories may be changed dynamically.
+ *
+ * <p>The set of categories supported is limited
+ * to the following standard Image I/O service types:
+ *
+ * <ul>
+ * <li>{@link ImageInputStreamSpi}
+ * <li>{@link ImageOutputStreamSpi}
+ * <li>{@link ImageReaderSpi}
+ * <li>{@link ImageTranscoderSpi}
+ * <li>{@link ImageWriterSpi}
+ * </ul>
+ *
+ * <p>An attempt to load a provider that is not a subtype of one of the
+ * above types will result in {@code IllegalArgumentException}. For
+ * a general mechanism to load service providers, see
+ * {@link java.util.ServiceLoader ServiceLoader}.
  *
  * <p> Only a single instance of a given leaf class (that is, the
  * actual class returned by <code>getClass()</code>, as opposed to any
  * inherited classes or interfaces) may be registered.  That is,
  * suppose that the
- * <code>com.mycompany.mypkg.GreenServiceProvider</code> class
- * implements the <code>com.mycompany.mypkg.MyService</code>
- * interface.  If a <code>GreenServiceProvider</code> instance is
+ * <code>com.mycompany.mypkg.GreenImageReaderProvider</code> class
+ * is a subclass of <code>javax.imageio.spi.ImageReaderSpi</code>.
+ * If a <code>GreenImageReaderProvider</code> instance is
  * registered, it will be stored in the category defined by the
- * <code>MyService</code> class.  If a new instance of
- * <code>GreenServiceProvider</code> is registered, it will replace
+ * <code>ImageReaderSpi</code> class.  If a new instance of
+ * <code>GreenImageReaderProvider</code> is registered, it will replace
  * the previous instance.  In practice, service provider objects are
  * usually singletons so this behavior is appropriate.
  *
@@ -68,15 +83,15 @@
  * for each service provider interface that has one or more
  * implementation classes present in the JAR file.  For example, if
  * the JAR file contained a class named
- * <code>com.mycompany.mypkg.MyServiceImpl</code> which implements the
- * <code>javax.someapi.SomeService</code> interface, the JAR file
+ * <code>com.mycompany.mypkg.GreenImageReaderProvider</code> which implements the
+ * <code>javax.imageio.spi.ImageReaderSpi</code> interface, the JAR file
  * would contain a file named: <pre>
- * META-INF/services/javax.someapi.SomeService </pre>
+ * META-INF/services/javax.imageio.spi.ImageReaderSpi</pre>
  *
  * containing the line:
  *
  * <pre>
- * com.mycompany.mypkg.MyService
+ * com.mycompany.mypkg.GreenImageReaderProvider
  * </pre>
  *
  * <p> The service provider classes should be to be lightweight and
@@ -94,7 +109,7 @@
  * JAR File Specification</a>.
  *
  * @see RegisterableService
- *
+ * @see java.util.ServiceLoader
  */
 public class ServiceRegistry {
 
@@ -104,13 +119,15 @@
     /**
      * Constructs a <code>ServiceRegistry</code> instance with a
      * set of categories taken from the <code>categories</code>
-     * argument.
+     * argument. The categories must all be members of the set
+     * of service types listed in the class specification.
      *
      * @param categories an <code>Iterator</code> containing
      * <code>Class</code> objects to be used to define categories.
      *
      * @exception IllegalArgumentException if
-     * <code>categories</code> is <code>null</code>.
+     * <code>categories</code> is <code>null</code>, or if
+     * one of the categories is not an allowed service type.
      */
     public ServiceRegistry(Iterator<Class<?>> categories) {
         if (categories == null) {
@@ -118,6 +135,7 @@
         }
         while (categories.hasNext()) {
             Class<?> category = categories.next();
+            checkClassAllowed(category);
             SubRegistry reg = new SubRegistry(this, category);
             categoryMap.put(category, reg);
         }
@@ -127,6 +145,10 @@
      * Searches for implementations of a particular service class
      * using the given class loader.
      *
+     * <p>The service class must be one of the service types listed
+     * in the class specification. If it is not, {@code IllegalArgumentException}
+     * will be thrown.
+     *
      * <p> This method transforms the name of the given service class
      * into a provider-configuration filename as described in the
      * class comment and then uses the <code>getResources</code>
@@ -157,7 +179,8 @@
      * cannot be found and instantiated.
      *
      * @exception IllegalArgumentException if
-     * <code>providerClass</code> is <code>null</code>.
+     * <code>providerClass</code> is <code>null</code>, or if it is
+     * not one of the allowed service types.
      */
     public static <T> Iterator<T> lookupProviders(Class<T> providerClass,
                                                   ClassLoader loader)
@@ -165,6 +188,7 @@
         if (providerClass == null) {
             throw new IllegalArgumentException("providerClass == null!");
         }
+        checkClassAllowed(providerClass);
         return ServiceLoader.load(providerClass, loader).iterator();
     }
 
@@ -178,6 +202,10 @@
      *   return Service.providers(service, cl);
      * </pre>
      *
+     * <p>The service class must be one of the service types listed
+     * in the class specification. If it is not, {@code IllegalArgumentException}
+     * will be thrown.
+     *
      * @param providerClass a <code>Class</code>object indicating the
      * class or interface of the service providers being detected.
      *
@@ -190,12 +218,14 @@
      * cannot be found and instantiated.
      *
      * @exception IllegalArgumentException if
-     * <code>providerClass</code> is <code>null</code>.
+     * <code>providerClass</code> is <code>null</code>, or if it is
+     * not one of the allowed service types.
      */
     public static <T> Iterator<T> lookupProviders(Class<T> providerClass) {
         if (providerClass == null) {
             throw new IllegalArgumentException("providerClass == null!");
         }
+        checkClassAllowed(providerClass);
         return ServiceLoader.load(providerClass).iterator();
     }
 
@@ -681,6 +711,28 @@
         deregisterAll();
         super.finalize();
     }
+
+    /**
+     * Checks whether the provided class is one of the allowed
+     * ImageIO service provider classes. If it is, returns normally.
+     * If it is not, throws IllegalArgumentException.
+     *
+     * @param clazz
+     * @throws IllegalArgumentException if clazz is null or is not one of the allowed set
+     */
+    private static void checkClassAllowed(Class<?> clazz) {
+        if (clazz == null) {
+            throw new IllegalArgumentException("class must not be null");
+        }
+
+        if (   clazz != ImageInputStreamSpi.class
+            && clazz != ImageOutputStreamSpi.class
+            && clazz != ImageReaderSpi.class
+            && clazz != ImageTranscoderSpi.class
+            && clazz != ImageWriterSpi.class) {
+            throw new IllegalArgumentException(clazz.getName() + " is not an ImageIO SPI class");
+        }
+    }
 }
 
 
--- a/src/java.desktop/share/classes/javax/imageio/spi/package.html	Thu Aug 13 12:27:06 2015 -0700
+++ b/src/java.desktop/share/classes/javax/imageio/spi/package.html	Thu Aug 13 13:30:15 2015 -0700
@@ -39,8 +39,8 @@
 
 The <code>javax.imageio.spi</code> package contains service
 provider interfaces for reading, writing, and transcoding images, and
-obtaining input and output streams, as well as a run-time registry
-that discovers installed service provider instances and allows new
+obtaining image input and output streams, as well as a run-time registry
+that discovers installed instances of Image I/O service providers and allows new
 instances to be registered dynamically.
 
 @since 1.4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/imageio/spi/ServiceRegistryRestriction.java	Thu Aug 13 13:30:15 2015 -0700
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8068749
+ * @run main ServiceRegistryRestriction
+ * @summary Tests ServiceRegistry's restriction on handling
+ *          only standard Image I/O service types.
+ */
+
+import java.util.*;
+import java.util.function.Consumer;
+import javax.imageio.spi.*;
+
+public class ServiceRegistryRestriction {
+    static class DummyTestSpi {
+    }
+
+    ClassLoader cl = ServiceRegistryRestriction.class.getClassLoader();
+
+    <T> void construct(Class<T> clazz) {
+        List<Class<?>> list = Arrays.<Class<?>>asList(clazz);
+        ServiceRegistry sr = new ServiceRegistry(list.iterator());
+    }
+
+    <T> void lookup(Class<T> clazz) {
+        Iterator<T> i = ServiceRegistry.lookupProviders(clazz);
+    }
+
+    <T> void lookupCL(Class<T> clazz) {
+        Iterator<T> i = ServiceRegistry.lookupProviders(clazz, cl);
+    }
+
+    <T> void doOneTest(String label, Class<T> clazz, boolean expectFail, Consumer<Class<T>> op) {
+        System.out.printf("testing %s with %s...", label, clazz.getName());
+        try {
+            op.accept(clazz);
+            if (expectFail) {
+                throw new AssertionError("fail, operation succeeded unexpectedly");
+            } else {
+                System.out.println("success");
+            }
+        } catch (IllegalArgumentException iae) {
+            if (expectFail) {
+                System.out.println("success, got expected IAE");
+            } else {
+                throw new AssertionError("fail, unexpected exception", iae);
+            }
+        }
+    }
+
+    void doTests(Class<?> clazz, boolean expectFail) {
+        doOneTest("constructor", clazz, expectFail, this::construct);
+        doOneTest("lookup", clazz, expectFail, this::lookup);
+        doOneTest("lookupCL", clazz, expectFail, this::lookupCL);
+    }
+
+    void run() {
+        doTests(ImageInputStreamSpi.class, false);
+        doTests(ImageOutputStreamSpi.class, false);
+        doTests(ImageReaderSpi.class, false);
+        doTests(ImageTranscoderSpi.class, false);
+        doTests(ImageWriterSpi.class, false);
+        doTests(DummyTestSpi.class, true);
+    }
+
+    public static void main(String[] args) {
+        new ServiceRegistryRestriction().run();
+    }
+}