- /*
- * @(#)IIORegistry.java 1.65 04/06/17
- *
- * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
-
- package javax.imageio.spi;
-
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.NoSuchElementException;
- import java.util.Set;
- import java.util.Vector;
- import com.sun.imageio.spi.FileImageInputStreamSpi;
- import com.sun.imageio.spi.FileImageOutputStreamSpi;
- import com.sun.imageio.spi.InputStreamImageInputStreamSpi;
- import com.sun.imageio.spi.OutputStreamImageOutputStreamSpi;
- import com.sun.imageio.spi.RAFImageInputStreamSpi;
- import com.sun.imageio.spi.RAFImageOutputStreamSpi;
- import com.sun.imageio.plugins.gif.GIFImageReaderSpi;
- import com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi;
- import com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi;
- import com.sun.imageio.plugins.png.PNGImageReaderSpi;
- import com.sun.imageio.plugins.png.PNGImageWriterSpi;
- import com.sun.imageio.plugins.bmp.BMPImageReaderSpi;
- import com.sun.imageio.plugins.bmp.BMPImageWriterSpi;
- import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
- import com.sun.imageio.plugins.wbmp.WBMPImageWriterSpi;
- import sun.awt.AppContext;
- import sun.misc.Service;
-
- /**
- * A registry for service provider instances. Service provider
- * classes may be detected at run time by means of meta-information in
- * the JAR files containing them. The intent is that it be relatively
- * inexpensive to load and inspect all available service provider
- * classes. These classes may them be used to locate and instantiate
- * more heavyweight classes that will perform actual work, in this
- * case instances of <code>ImageReader</code>,
- * <code>ImageWriter</code>, <code>ImageTranscoder</code>,
- * <code>ImageInputStream</code>, and <code>ImageOutputStream</code>.
- *
- * <p> Service providers found on the system classpath (<i>e.g.</i>,
- * the <code>jre/lib/ext</code> directory in Sun's implementation of
- * JDK) are automatically loaded as soon as this class is
- * instantiated.
- *
- * <p> When the <code>registerApplicationClasspathSpis</code> method
- * is called, service provider instances declared in the
- * meta-information section of JAR files on the application class path
- * are loaded. To declare a service provider, a <code>services</code>
- * subdirectory is placed within the <code>META-INF</code> directory
- * that is present in every JAR file. This directory contains a file
- * 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.imageio.MyFormatReaderSpi</code> which
- * implements the <code>ImageReaderSpi</code> interface, the JAR file
- * would contain a file named:
- *
- * <pre>
- * META-INF/services/javax.imageio.spi.ImageReaderSpi
- * </pre>
- *
- * containing the line:
- *
- * <pre>
- * com.mycompany.imageio.MyFormatReaderSpi
- * </pre>
- *
- * <p> The service provider classes are intended to be lightweight
- * and quick to load. Implementations of these interfaces
- * should avoid complex dependencies on other classes and on
- * native code.
- *
- * <p> It is also possible to manually add service providers not found
- * automatically, as well as to remove those that are using the
- * interfaces of the <code>ServiceRegistry</code> class. Thus
- * the application may customize the contents of the registry as it
- * sees fit.
- *
- * <p> For more details on declaring service providers, and the JAR
- * format in general, see the <a
- * href="{@docRoot}/../guide/jar/jar.html">
- * JAR File Specification</a>.
- *
- * @version 0.5
- */
- public final class IIORegistry extends ServiceRegistry {
-
- /**
- * A <code>Vector</code> containing the valid IIO registry
- * categories (superinterfaces) to be used in the constructor.
- */
- private static final Vector initialCategories = new Vector(5);
-
- static {
- initialCategories.add(ImageReaderSpi.class);
- initialCategories.add(ImageWriterSpi.class);
- initialCategories.add(ImageTranscoderSpi.class);
- initialCategories.add(ImageInputStreamSpi.class);
- initialCategories.add(ImageOutputStreamSpi.class);
- }
-
- /**
- * Set up the valid service provider categories and automatically
- * register all available service providers.
- *
- * <p> The constructor is private in order to prevent creation of
- * additional instances.
- */
- private IIORegistry() {
- super(initialCategories.iterator());
- registerStandardSpis();
- registerApplicationClasspathSpis();
- }
-
- /**
- * Returns the default <code>IIORegistry</code> instance used by
- * the Image I/O API. This instance should be used for all
- * registry functions.
- *
- * <p> Each <code>ThreadGroup</code> will receive its own
- * instance; this allows different <code>Applet</code>s in the
- * same browser (for example) to each have their own registry.
- *
- * @return the default registry for the current
- * <code>ThreadGroup</code>.
- */
- public static IIORegistry getDefaultInstance() {
- AppContext context = AppContext.getAppContext();
- IIORegistry registry =
- (IIORegistry)context.get(IIORegistry.class);
- if (registry == null) {
- // Create an instance for this AppContext
- registry = new IIORegistry();
- context.put(IIORegistry.class, registry);
- }
- return registry;
- }
-
- private void registerStandardSpis() {
- // Hardwire standard SPIs
- registerServiceProvider(new GIFImageReaderSpi());
- registerServiceProvider(new BMPImageReaderSpi());
- registerServiceProvider(new BMPImageWriterSpi());
- registerServiceProvider(new WBMPImageReaderSpi());
- registerServiceProvider(new WBMPImageWriterSpi());
- registerServiceProvider(new PNGImageReaderSpi());
- registerServiceProvider(new PNGImageWriterSpi());
- registerServiceProvider(new JPEGImageReaderSpi());
- registerServiceProvider(new JPEGImageWriterSpi());
- registerServiceProvider(new FileImageInputStreamSpi());
- registerServiceProvider(new FileImageOutputStreamSpi());
- registerServiceProvider(new InputStreamImageInputStreamSpi());
- registerServiceProvider(new OutputStreamImageOutputStreamSpi());
- registerServiceProvider(new RAFImageInputStreamSpi());
- registerServiceProvider(new RAFImageOutputStreamSpi());
- }
-
- /**
- * Registers all available service providers found on the
- * application class path, using the default
- * <code>ClassLoader</code>. This method is typically invoked by
- * the <code>ImageIO.scanForPlugins</code> method.
- *
- * @see javax.imageio.ImageIO#scanForPlugins
- * @see ClassLoader#getResources
- */
- public void registerApplicationClasspathSpis() {
- // FIX: load only from application classpath
-
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
-
- Iterator categories = getCategories();
- while (categories.hasNext()) {
- Class c = (Class)categories.next();
- Iterator riter = Service.providers(c, loader);
- while (riter.hasNext()) {
- IIOServiceProvider r = (IIOServiceProvider)riter.next();
- registerServiceProvider(r);
- }
- }
- }
- }