KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > spi > IIORegistry


1 /*
2  * @(#)IIORegistry.java 1.65 04/06/17
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.imageio.spi;
9
10 import java.security.PrivilegedAction JavaDoc;
11 import java.security.AccessController JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.NoSuchElementException JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.Vector JavaDoc;
18 import com.sun.imageio.spi.FileImageInputStreamSpi;
19 import com.sun.imageio.spi.FileImageOutputStreamSpi;
20 import com.sun.imageio.spi.InputStreamImageInputStreamSpi;
21 import com.sun.imageio.spi.OutputStreamImageOutputStreamSpi;
22 import com.sun.imageio.spi.RAFImageInputStreamSpi;
23 import com.sun.imageio.spi.RAFImageOutputStreamSpi;
24 import com.sun.imageio.plugins.gif.GIFImageReaderSpi;
25 import com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi;
26 import com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi;
27 import com.sun.imageio.plugins.png.PNGImageReaderSpi;
28 import com.sun.imageio.plugins.png.PNGImageWriterSpi;
29 import com.sun.imageio.plugins.bmp.BMPImageReaderSpi;
30 import com.sun.imageio.plugins.bmp.BMPImageWriterSpi;
31 import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
32 import com.sun.imageio.plugins.wbmp.WBMPImageWriterSpi;
33 import sun.awt.AppContext;
34 import sun.misc.Service;
35 import sun.misc.ServiceConfigurationError;
36
37 /**
38  * A registry for service provider instances. Service provider
39  * classes may be detected at run time by means of meta-information in
40  * the JAR files containing them. The intent is that it be relatively
41  * inexpensive to load and inspect all available service provider
42  * classes. These classes may them be used to locate and instantiate
43  * more heavyweight classes that will perform actual work, in this
44  * case instances of <code>ImageReader</code>,
45  * <code>ImageWriter</code>, <code>ImageTranscoder</code>,
46  * <code>ImageInputStream</code>, and <code>ImageOutputStream</code>.
47  *
48  * <p> Service providers found on the system classpath (<i>e.g.</i>,
49  * the <code>jre/lib/ext</code> directory in Sun's implementation of
50  * JDK) are automatically loaded as soon as this class is
51  * instantiated.
52  *
53  * <p> When the <code>registerApplicationClasspathSpis</code> method
54  * is called, service provider instances declared in the
55  * meta-information section of JAR files on the application class path
56  * are loaded. To declare a service provider, a <code>services</code>
57  * subdirectory is placed within the <code>META-INF</code> directory
58  * that is present in every JAR file. This directory contains a file
59  * for each service provider interface that has one or more
60  * implementation classes present in the JAR file. For example, if
61  * the JAR file contained a class named
62  * <code>com.mycompany.imageio.MyFormatReaderSpi</code> which
63  * implements the <code>ImageReaderSpi</code> interface, the JAR file
64  * would contain a file named:
65  *
66  * <pre>
67  * META-INF/services/javax.imageio.spi.ImageReaderSpi
68  * </pre>
69  *
70  * containing the line:
71  *
72  * <pre>
73  * com.mycompany.imageio.MyFormatReaderSpi
74  * </pre>
75  *
76  * <p> The service provider classes are intended to be lightweight
77  * and quick to load. Implementations of these interfaces
78  * should avoid complex dependencies on other classes and on
79  * native code.
80  *
81  * <p> It is also possible to manually add service providers not found
82  * automatically, as well as to remove those that are using the
83  * interfaces of the <code>ServiceRegistry</code> class. Thus
84  * the application may customize the contents of the registry as it
85  * sees fit.
86  *
87  * <p> For more details on declaring service providers, and the JAR
88  * format in general, see the <a
89  * HREF="{@docRoot}/../guide/jar/jar.html">
90  * JAR File Specification</a>.
91  *
92  * @version 0.5
93  */

94 public final class IIORegistry extends ServiceRegistry JavaDoc {
95
96     /**
97      * A <code>Vector</code> containing the valid IIO registry
98      * categories (superinterfaces) to be used in the constructor.
99      */

100     private static final Vector JavaDoc initialCategories = new Vector JavaDoc(5);
101
102     static {
103         initialCategories.add(ImageReaderSpi JavaDoc.class);
104         initialCategories.add(ImageWriterSpi JavaDoc.class);
105         initialCategories.add(ImageTranscoderSpi JavaDoc.class);
106         initialCategories.add(ImageInputStreamSpi JavaDoc.class);
107         initialCategories.add(ImageOutputStreamSpi JavaDoc.class);
108     }
109
110     /**
111      * Set up the valid service provider categories and automatically
112      * register all available service providers.
113      *
114      * <p> The constructor is private in order to prevent creation of
115      * additional instances.
116      */

117     private IIORegistry() {
118         super(initialCategories.iterator());
119         registerStandardSpis();
120         registerApplicationClasspathSpis();
121     }
122
123     /**
124      * Returns the default <code>IIORegistry</code> instance used by
125      * the Image I/O API. This instance should be used for all
126      * registry functions.
127      *
128      * <p> Each <code>ThreadGroup</code> will receive its own
129      * instance; this allows different <code>Applet</code>s in the
130      * same browser (for example) to each have their own registry.
131      *
132      * @return the default registry for the current
133      * <code>ThreadGroup</code>.
134      */

135     public static IIORegistry JavaDoc getDefaultInstance() {
136         AppContext context = AppContext.getAppContext();
137         IIORegistry JavaDoc registry =
138             (IIORegistry JavaDoc)context.get(IIORegistry JavaDoc.class);
139         if (registry == null) {
140             // Create an instance for this AppContext
141
registry = new IIORegistry JavaDoc();
142             context.put(IIORegistry JavaDoc.class, registry);
143         }
144         return registry;
145     }
146
147     private void registerStandardSpis() {
148         // Hardwire standard SPIs
149
registerServiceProvider(new GIFImageReaderSpi());
150         registerServiceProvider(new BMPImageReaderSpi());
151         registerServiceProvider(new BMPImageWriterSpi());
152         registerServiceProvider(new WBMPImageReaderSpi());
153         registerServiceProvider(new WBMPImageWriterSpi());
154         registerServiceProvider(new PNGImageReaderSpi());
155         registerServiceProvider(new PNGImageWriterSpi());
156         registerServiceProvider(new JPEGImageReaderSpi());
157         registerServiceProvider(new JPEGImageWriterSpi());
158         registerServiceProvider(new FileImageInputStreamSpi());
159         registerServiceProvider(new FileImageOutputStreamSpi());
160         registerServiceProvider(new InputStreamImageInputStreamSpi());
161         registerServiceProvider(new OutputStreamImageOutputStreamSpi());
162         registerServiceProvider(new RAFImageInputStreamSpi());
163         registerServiceProvider(new RAFImageOutputStreamSpi());
164
165         registerInstalledProviders();
166     }
167
168     /**
169      * Registers all available service providers found on the
170      * application class path, using the default
171      * <code>ClassLoader</code>. This method is typically invoked by
172      * the <code>ImageIO.scanForPlugins</code> method.
173      *
174      * @see javax.imageio.ImageIO#scanForPlugins
175      * @see ClassLoader#getResources
176      */

177     public void registerApplicationClasspathSpis() {
178         // FIX: load only from application classpath
179

180     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
181
182         Iterator JavaDoc categories = getCategories();
183         while (categories.hasNext()) {
184             Class JavaDoc c = (Class JavaDoc)categories.next();
185             Iterator JavaDoc<IIOServiceProvider JavaDoc> riter = Service.providers(c, loader);
186             while (riter.hasNext()) {
187                 try {
188                     IIOServiceProvider JavaDoc r = riter.next();
189                     registerServiceProvider(r);
190                 } catch (ServiceConfigurationError err) {
191                     if (System.getSecurityManager() != null) {
192                         // In the applet case, we will catch the error so
193
// registration of other plugins can proceed
194
err.printStackTrace();
195                     } else {
196                         // In the application case, we will throw the
197
// error to indicate app/system misconfiguration
198
throw err;
199                     }
200                 }
201             }
202         }
203     }
204
205     private void registerInstalledProviders() {
206         /*
207           We need load installed providers from lib/ext
208           directory in the privileged mode in order to
209           be able read corresponding jar files even if
210           file read capability is restricted (like the
211           applet context case).
212          */

213         PrivilegedAction JavaDoc doRegistration =
214             new PrivilegedAction JavaDoc() {
215                 public Object JavaDoc run() {
216                     Iterator JavaDoc categories = getCategories();
217                     while (categories.hasNext()) {
218                         Class JavaDoc c = (Class JavaDoc)categories.next();
219                         Iterator JavaDoc<IIOServiceProvider JavaDoc> providers =
220                             Service.installedProviders(c);
221                         while (providers.hasNext()) {
222                             IIOServiceProvider JavaDoc p = providers.next();
223                             registerServiceProvider(p);
224                         }
225                     }
226                     return this;
227                 }
228             };
229         
230         AccessController.doPrivileged(doRegistration);
231     }
232 }
233
Popular Tags