src/share/classes/javax/imageio/spi/IIORegistry.java

Print this page
rev 9345 : 8035487: Fix raw and unchecked lint warnings in javax.imageio.spi
Reviewed-by:


  98  * native code.
  99  *
 100  * <p> It is also possible to manually add service providers not found
 101  * automatically, as well as to remove those that are using the
 102  * interfaces of the <code>ServiceRegistry</code> class.  Thus
 103  * the application may customize the contents of the registry as it
 104  * sees fit.
 105  *
 106  * <p> For more details on declaring service providers, and the JAR
 107  * format in general, see the <a
 108  * href="{@docRoot}/../technotes/guides/jar/jar.html">
 109  * JAR File Specification</a>.
 110  *
 111  */
 112 public final class IIORegistry extends ServiceRegistry {
 113 
 114     /**
 115      * A <code>Vector</code> containing the valid IIO registry
 116      * categories (superinterfaces) to be used in the constructor.
 117      */
 118     private static final Vector initialCategories = new Vector(5);
 119 
 120     static {
 121         initialCategories.add(ImageReaderSpi.class);
 122         initialCategories.add(ImageWriterSpi.class);
 123         initialCategories.add(ImageTranscoderSpi.class);
 124         initialCategories.add(ImageInputStreamSpi.class);
 125         initialCategories.add(ImageOutputStreamSpi.class);
 126     }
 127 
 128     /**
 129      * Set up the valid service provider categories and automatically
 130      * register all available service providers.
 131      *
 132      * <p> The constructor is private in order to prevent creation of
 133      * additional instances.
 134      */
 135     private IIORegistry() {
 136         super(initialCategories.iterator());
 137         registerStandardSpis();
 138         registerApplicationClasspathSpis();


 181         registerServiceProvider(new RAFImageInputStreamSpi());
 182         registerServiceProvider(new RAFImageOutputStreamSpi());
 183 
 184         registerInstalledProviders();
 185     }
 186 
 187     /**
 188      * Registers all available service providers found on the
 189      * application class path, using the default
 190      * <code>ClassLoader</code>.  This method is typically invoked by
 191      * the <code>ImageIO.scanForPlugins</code> method.
 192      *
 193      * @see javax.imageio.ImageIO#scanForPlugins
 194      * @see ClassLoader#getResources
 195      */
 196     public void registerApplicationClasspathSpis() {
 197         // FIX: load only from application classpath
 198 
 199         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 200 
 201         Iterator categories = getCategories();
 202         while (categories.hasNext()) {
 203             Class<IIOServiceProvider> c = (Class)categories.next();

 204             Iterator<IIOServiceProvider> riter =
 205                     ServiceLoader.load(c, loader).iterator();
 206             while (riter.hasNext()) {
 207                 try {
 208                     // Note that the next() call is required to be inside
 209                     // the try/catch block; see 6342404.
 210                     IIOServiceProvider r = riter.next();
 211                     registerServiceProvider(r);
 212                 } catch (ServiceConfigurationError err) {
 213                     if (System.getSecurityManager() != null) {
 214                         // In the applet case, we will catch the  error so
 215                         // registration of other plugins can  proceed
 216                         err.printStackTrace();
 217                     } else {
 218                         // In the application case, we will  throw the
 219                         // error to indicate app/system  misconfiguration
 220                         throw err;
 221                     }
 222                 }
 223             }
 224         }
 225     }
 226 
 227     private void registerInstalledProviders() {
 228         /*
 229           We need to load installed providers from the
 230           system classpath (typically the <code>lib/ext</code>
 231           directory in in the Java installation directory)
 232           in the privileged mode in order to
 233           be able read corresponding jar files even if
 234           file read capability is restricted (like the
 235           applet context case).
 236          */
 237         PrivilegedAction doRegistration =
 238             new PrivilegedAction() {
 239                 public Object run() {
 240                     Iterator categories = getCategories();
 241                     while (categories.hasNext()) {
 242                         Class<IIOServiceProvider> c = (Class)categories.next();

 243                         for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
 244                             registerServiceProvider(p);
 245                         }
 246                     }
 247                     return this;
 248                 }
 249             };
 250 
 251         AccessController.doPrivileged(doRegistration);
 252     }
 253 }


  98  * native code.
  99  *
 100  * <p> It is also possible to manually add service providers not found
 101  * automatically, as well as to remove those that are using the
 102  * interfaces of the <code>ServiceRegistry</code> class.  Thus
 103  * the application may customize the contents of the registry as it
 104  * sees fit.
 105  *
 106  * <p> For more details on declaring service providers, and the JAR
 107  * format in general, see the <a
 108  * href="{@docRoot}/../technotes/guides/jar/jar.html">
 109  * JAR File Specification</a>.
 110  *
 111  */
 112 public final class IIORegistry extends ServiceRegistry {
 113 
 114     /**
 115      * A <code>Vector</code> containing the valid IIO registry
 116      * categories (superinterfaces) to be used in the constructor.
 117      */
 118     private static final Vector<Class<?>> initialCategories = new Vector<>(5);
 119 
 120     static {
 121         initialCategories.add(ImageReaderSpi.class);
 122         initialCategories.add(ImageWriterSpi.class);
 123         initialCategories.add(ImageTranscoderSpi.class);
 124         initialCategories.add(ImageInputStreamSpi.class);
 125         initialCategories.add(ImageOutputStreamSpi.class);
 126     }
 127 
 128     /**
 129      * Set up the valid service provider categories and automatically
 130      * register all available service providers.
 131      *
 132      * <p> The constructor is private in order to prevent creation of
 133      * additional instances.
 134      */
 135     private IIORegistry() {
 136         super(initialCategories.iterator());
 137         registerStandardSpis();
 138         registerApplicationClasspathSpis();


 181         registerServiceProvider(new RAFImageInputStreamSpi());
 182         registerServiceProvider(new RAFImageOutputStreamSpi());
 183 
 184         registerInstalledProviders();
 185     }
 186 
 187     /**
 188      * Registers all available service providers found on the
 189      * application class path, using the default
 190      * <code>ClassLoader</code>.  This method is typically invoked by
 191      * the <code>ImageIO.scanForPlugins</code> method.
 192      *
 193      * @see javax.imageio.ImageIO#scanForPlugins
 194      * @see ClassLoader#getResources
 195      */
 196     public void registerApplicationClasspathSpis() {
 197         // FIX: load only from application classpath
 198 
 199         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 200 
 201         Iterator<Class<?>> categories = getCategories();
 202         while (categories.hasNext()) {
 203             @SuppressWarnings("unchecked")
 204             Class<IIOServiceProvider> c = (Class<IIOServiceProvider>)categories.next();
 205             Iterator<IIOServiceProvider> riter =
 206                     ServiceLoader.load(c, loader).iterator();
 207             while (riter.hasNext()) {
 208                 try {
 209                     // Note that the next() call is required to be inside
 210                     // the try/catch block; see 6342404.
 211                     IIOServiceProvider r = riter.next();
 212                     registerServiceProvider(r);
 213                 } catch (ServiceConfigurationError err) {
 214                     if (System.getSecurityManager() != null) {
 215                         // In the applet case, we will catch the  error so
 216                         // registration of other plugins can  proceed
 217                         err.printStackTrace();
 218                     } else {
 219                         // In the application case, we will  throw the
 220                         // error to indicate app/system  misconfiguration
 221                         throw err;
 222                     }
 223                 }
 224             }
 225         }
 226     }
 227 
 228     private void registerInstalledProviders() {
 229         /*
 230           We need to load installed providers from the
 231           system classpath (typically the <code>lib/ext</code>
 232           directory in in the Java installation directory)
 233           in the privileged mode in order to
 234           be able read corresponding jar files even if
 235           file read capability is restricted (like the
 236           applet context case).
 237          */
 238         PrivilegedAction<Object> doRegistration =
 239             new PrivilegedAction<Object>() {
 240                 public Object run() {
 241                     Iterator<Class<?>> categories = getCategories();
 242                     while (categories.hasNext()) {
 243                         @SuppressWarnings("unchecked")
 244                         Class<IIOServiceProvider> c = (Class<IIOServiceProvider>)categories.next();
 245                         for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
 246                             registerServiceProvider(p);
 247                         }
 248                     }
 249                     return this;
 250                 }
 251             };
 252 
 253         AccessController.doPrivileged(doRegistration);
 254     }
 255 }