0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.imageio.spi;
0N/A
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.AccessController;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/Aimport java.util.NoSuchElementException;
0N/Aimport java.util.Set;
0N/Aimport java.util.Vector;
0N/Aimport com.sun.imageio.spi.FileImageInputStreamSpi;
0N/Aimport com.sun.imageio.spi.FileImageOutputStreamSpi;
0N/Aimport com.sun.imageio.spi.InputStreamImageInputStreamSpi;
0N/Aimport com.sun.imageio.spi.OutputStreamImageOutputStreamSpi;
0N/Aimport com.sun.imageio.spi.RAFImageInputStreamSpi;
0N/Aimport com.sun.imageio.spi.RAFImageOutputStreamSpi;
0N/Aimport com.sun.imageio.plugins.gif.GIFImageReaderSpi;
0N/Aimport com.sun.imageio.plugins.gif.GIFImageWriterSpi;
0N/Aimport com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi;
0N/Aimport com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi;
0N/Aimport com.sun.imageio.plugins.png.PNGImageReaderSpi;
0N/Aimport com.sun.imageio.plugins.png.PNGImageWriterSpi;
0N/Aimport com.sun.imageio.plugins.bmp.BMPImageReaderSpi;
0N/Aimport com.sun.imageio.plugins.bmp.BMPImageWriterSpi;
0N/Aimport com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
0N/Aimport com.sun.imageio.plugins.wbmp.WBMPImageWriterSpi;
0N/Aimport sun.awt.AppContext;
0N/Aimport java.util.ServiceLoader;
0N/Aimport java.util.ServiceConfigurationError;
0N/A
0N/A/**
0N/A * A registry for service provider instances. Service provider
0N/A * classes may be detected at run time by means of meta-information in
0N/A * the JAR files containing them. The intent is that it be relatively
0N/A * inexpensive to load and inspect all available service provider
0N/A * classes. These classes may them be used to locate and instantiate
0N/A * more heavyweight classes that will perform actual work, in this
0N/A * case instances of <code>ImageReader</code>,
0N/A * <code>ImageWriter</code>, <code>ImageTranscoder</code>,
0N/A * <code>ImageInputStream</code>, and <code>ImageOutputStream</code>.
0N/A *
0N/A * <p> Service providers found on the system classpath (<i>e.g.</i>,
0N/A * the <code>jre/lib/ext</code> directory in Sun's implementation of
0N/A * JDK) are automatically loaded as soon as this class is
0N/A * instantiated.
0N/A *
0N/A * <p> When the <code>registerApplicationClasspathSpis</code> method
0N/A * is called, service provider instances declared in the
0N/A * meta-information section of JAR files on the application class path
0N/A * are loaded. To declare a service provider, a <code>services</code>
0N/A * subdirectory is placed within the <code>META-INF</code> directory
0N/A * that is present in every JAR file. This directory contains a file
0N/A * for each service provider interface that has one or more
0N/A * implementation classes present in the JAR file. For example, if
0N/A * the JAR file contained a class named
0N/A * <code>com.mycompany.imageio.MyFormatReaderSpi</code> which
0N/A * implements the <code>ImageReaderSpi</code> interface, the JAR file
0N/A * would contain a file named:
0N/A *
0N/A * <pre>
0N/A * META-INF/services/javax.imageio.spi.ImageReaderSpi
0N/A * </pre>
0N/A *
0N/A * containing the line:
0N/A *
0N/A * <pre>
0N/A * com.mycompany.imageio.MyFormatReaderSpi
0N/A * </pre>
0N/A *
0N/A * <p> The service provider classes are intended to be lightweight
0N/A * and quick to load. Implementations of these interfaces
0N/A * should avoid complex dependencies on other classes and on
0N/A * native code.
0N/A *
0N/A * <p> It is also possible to manually add service providers not found
0N/A * automatically, as well as to remove those that are using the
0N/A * interfaces of the <code>ServiceRegistry</code> class. Thus
0N/A * the application may customize the contents of the registry as it
0N/A * sees fit.
0N/A *
0N/A * <p> For more details on declaring service providers, and the JAR
0N/A * format in general, see the <a
0N/A * href="{@docRoot}/../technotes/guides/jar/jar.html">
0N/A * JAR File Specification</a>.
0N/A *
0N/A */
0N/Apublic final class IIORegistry extends ServiceRegistry {
0N/A
0N/A /**
0N/A * A <code>Vector</code> containing the valid IIO registry
0N/A * categories (superinterfaces) to be used in the constructor.
0N/A */
0N/A private static final Vector initialCategories = new Vector(5);
0N/A
0N/A static {
0N/A initialCategories.add(ImageReaderSpi.class);
0N/A initialCategories.add(ImageWriterSpi.class);
0N/A initialCategories.add(ImageTranscoderSpi.class);
0N/A initialCategories.add(ImageInputStreamSpi.class);
0N/A initialCategories.add(ImageOutputStreamSpi.class);
0N/A }
0N/A
0N/A /**
0N/A * Set up the valid service provider categories and automatically
0N/A * register all available service providers.
0N/A *
0N/A * <p> The constructor is private in order to prevent creation of
0N/A * additional instances.
0N/A */
0N/A private IIORegistry() {
0N/A super(initialCategories.iterator());
0N/A registerStandardSpis();
0N/A registerApplicationClasspathSpis();
0N/A }
0N/A
0N/A /**
0N/A * Returns the default <code>IIORegistry</code> instance used by
0N/A * the Image I/O API. This instance should be used for all
0N/A * registry functions.
0N/A *
0N/A * <p> Each <code>ThreadGroup</code> will receive its own
0N/A * instance; this allows different <code>Applet</code>s in the
0N/A * same browser (for example) to each have their own registry.
0N/A *
0N/A * @return the default registry for the current
0N/A * <code>ThreadGroup</code>.
0N/A */
0N/A public static IIORegistry getDefaultInstance() {
0N/A AppContext context = AppContext.getAppContext();
0N/A IIORegistry registry =
0N/A (IIORegistry)context.get(IIORegistry.class);
0N/A if (registry == null) {
0N/A // Create an instance for this AppContext
0N/A registry = new IIORegistry();
0N/A context.put(IIORegistry.class, registry);
0N/A }
0N/A return registry;
0N/A }
0N/A
0N/A private void registerStandardSpis() {
0N/A // Hardwire standard SPIs
0N/A registerServiceProvider(new GIFImageReaderSpi());
0N/A registerServiceProvider(new GIFImageWriterSpi());
0N/A registerServiceProvider(new BMPImageReaderSpi());
0N/A registerServiceProvider(new BMPImageWriterSpi());
0N/A registerServiceProvider(new WBMPImageReaderSpi());
0N/A registerServiceProvider(new WBMPImageWriterSpi());
0N/A registerServiceProvider(new PNGImageReaderSpi());
0N/A registerServiceProvider(new PNGImageWriterSpi());
0N/A registerServiceProvider(new JPEGImageReaderSpi());
0N/A registerServiceProvider(new JPEGImageWriterSpi());
0N/A registerServiceProvider(new FileImageInputStreamSpi());
0N/A registerServiceProvider(new FileImageOutputStreamSpi());
0N/A registerServiceProvider(new InputStreamImageInputStreamSpi());
0N/A registerServiceProvider(new OutputStreamImageOutputStreamSpi());
0N/A registerServiceProvider(new RAFImageInputStreamSpi());
0N/A registerServiceProvider(new RAFImageOutputStreamSpi());
0N/A
0N/A registerInstalledProviders();
0N/A }
0N/A
0N/A /**
0N/A * Registers all available service providers found on the
0N/A * application class path, using the default
0N/A * <code>ClassLoader</code>. This method is typically invoked by
0N/A * the <code>ImageIO.scanForPlugins</code> method.
0N/A *
0N/A * @see javax.imageio.ImageIO#scanForPlugins
0N/A * @see ClassLoader#getResources
0N/A */
0N/A public void registerApplicationClasspathSpis() {
0N/A // FIX: load only from application classpath
0N/A
0N/A ClassLoader loader = Thread.currentThread().getContextClassLoader();
0N/A
0N/A Iterator categories = getCategories();
0N/A while (categories.hasNext()) {
0N/A Class<IIOServiceProvider> c = (Class)categories.next();
0N/A Iterator<IIOServiceProvider> riter =
0N/A ServiceLoader.load(c, loader).iterator();
0N/A while (riter.hasNext()) {
0N/A try {
0N/A // Note that the next() call is required to be inside
0N/A // the try/catch block; see 6342404.
0N/A IIOServiceProvider r = riter.next();
0N/A registerServiceProvider(r);
0N/A } catch (ServiceConfigurationError err) {
0N/A if (System.getSecurityManager() != null) {
0N/A // In the applet case, we will catch the error so
0N/A // registration of other plugins can proceed
0N/A err.printStackTrace();
0N/A } else {
0N/A // In the application case, we will throw the
0N/A // error to indicate app/system misconfiguration
0N/A throw err;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void registerInstalledProviders() {
0N/A /*
0N/A We need load installed providers from lib/ext
0N/A directory in the privileged mode in order to
0N/A be able read corresponding jar files even if
0N/A file read capability is restricted (like the
0N/A applet context case).
0N/A */
0N/A PrivilegedAction doRegistration =
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A Iterator categories = getCategories();
0N/A while (categories.hasNext()) {
0N/A Class<IIOServiceProvider> c = (Class)categories.next();
0N/A for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
0N/A registerServiceProvider(p);
0N/A }
0N/A }
0N/A return this;
0N/A }
0N/A };
0N/A
0N/A AccessController.doPrivileged(doRegistration);
0N/A }
0N/A}