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

Print this page




  47 import com.sun.imageio.plugins.png.PNGImageWriterSpi;
  48 import com.sun.imageio.plugins.bmp.BMPImageReaderSpi;
  49 import com.sun.imageio.plugins.bmp.BMPImageWriterSpi;
  50 import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
  51 import com.sun.imageio.plugins.wbmp.WBMPImageWriterSpi;
  52 import sun.awt.AppContext;
  53 import java.util.ServiceLoader;
  54 import java.util.ServiceConfigurationError;
  55 
  56 /**
  57  * A registry for service provider instances.  Service provider
  58  * classes may be detected at run time by means of meta-information in
  59  * the JAR files containing them.  The intent is that it be relatively
  60  * inexpensive to load and inspect all available service provider
  61  * classes.  These classes may them be used to locate and instantiate
  62  * more heavyweight classes that will perform actual work, in this
  63  * case instances of <code>ImageReader</code>,
  64  * <code>ImageWriter</code>, <code>ImageTranscoder</code>,
  65  * <code>ImageInputStream</code>, and <code>ImageOutputStream</code>.
  66  *
  67  * <p> Service providers found on the system classpath (typically
  68  * the <code>lib/ext</code> directory in the Java
  69  * installation directory) are automatically loaded as soon as this class is
  70  * instantiated.
  71  *
  72  * <p> When the <code>registerApplicationClasspathSpis</code> method
  73  * is called, service provider instances declared in the
  74  * meta-information section of JAR files on the application class path
  75  * are loaded.  To declare a service provider, a <code>services</code>
  76  * subdirectory is placed within the <code>META-INF</code> directory
  77  * that is present in every JAR file.  This directory contains a file
  78  * for each service provider interface that has one or more
  79  * implementation classes present in the JAR file.  For example, if
  80  * the JAR file contained a class named
  81  * <code>com.mycompany.imageio.MyFormatReaderSpi</code> which
  82  * implements the <code>ImageReaderSpi</code> interface, the JAR file
  83  * would contain a file named:
  84  *
  85  * <pre>
  86  * META-INF/services/javax.imageio.spi.ImageReaderSpi
  87  * </pre>
  88  *
  89  * containing the line:
  90  *


 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 


  47 import com.sun.imageio.plugins.png.PNGImageWriterSpi;
  48 import com.sun.imageio.plugins.bmp.BMPImageReaderSpi;
  49 import com.sun.imageio.plugins.bmp.BMPImageWriterSpi;
  50 import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
  51 import com.sun.imageio.plugins.wbmp.WBMPImageWriterSpi;
  52 import sun.awt.AppContext;
  53 import java.util.ServiceLoader;
  54 import java.util.ServiceConfigurationError;
  55 
  56 /**
  57  * A registry for service provider instances.  Service provider
  58  * classes may be detected at run time by means of meta-information in
  59  * the JAR files containing them.  The intent is that it be relatively
  60  * inexpensive to load and inspect all available service provider
  61  * classes.  These classes may them be used to locate and instantiate
  62  * more heavyweight classes that will perform actual work, in this
  63  * case instances of <code>ImageReader</code>,
  64  * <code>ImageWriter</code>, <code>ImageTranscoder</code>,
  65  * <code>ImageInputStream</code>, and <code>ImageOutputStream</code>.
  66  *
  67  * Service providers found from the Java platform are automatically
  68  * loaded as soon as this class is instantiated.


  69  *
  70  * <p> When the <code>registerApplicationClasspathSpis</code> method
  71  * is called, service provider instances declared in the
  72  * meta-information section of JAR files on the application class path
  73  * are loaded.  To declare a service provider, a <code>services</code>
  74  * subdirectory is placed within the <code>META-INF</code> directory
  75  * that is present in every JAR file.  This directory contains a file
  76  * for each service provider interface that has one or more
  77  * implementation classes present in the JAR file.  For example, if
  78  * the JAR file contained a class named
  79  * <code>com.mycompany.imageio.MyFormatReaderSpi</code> which
  80  * implements the <code>ImageReaderSpi</code> interface, the JAR file
  81  * would contain a file named:
  82  *
  83  * <pre>
  84  * META-INF/services/javax.imageio.spi.ImageReaderSpi
  85  * </pre>
  86  *
  87  * containing the line:
  88  *


 208                     // the try/catch block; see 6342404.
 209                     IIOServiceProvider r = riter.next();
 210                     registerServiceProvider(r);
 211                 } catch (ServiceConfigurationError err) {
 212                     if (System.getSecurityManager() != null) {
 213                         // In the applet case, we will catch the  error so
 214                         // registration of other plugins can  proceed
 215                         err.printStackTrace();
 216                     } else {
 217                         // In the application case, we will  throw the
 218                         // error to indicate app/system  misconfiguration
 219                         throw err;
 220                     }
 221                 }
 222             }
 223         }
 224     }
 225 
 226     private void registerInstalledProviders() {
 227         /*
 228           We need to load installed providers


 229           in the privileged mode in order to
 230           be able read corresponding jar files even if
 231           file read capability is restricted (like the
 232           applet context case).
 233          */
 234         PrivilegedAction<Object> doRegistration =
 235             new PrivilegedAction<Object>() {
 236                 public Object run() {
 237                     Iterator<Class<?>> categories = getCategories();
 238                     while (categories.hasNext()) {
 239                         @SuppressWarnings("unchecked")
 240                         Class<IIOServiceProvider> c = (Class<IIOServiceProvider>)categories.next();
 241                         for (IIOServiceProvider p : ServiceLoader.loadInstalled(c)) {
 242                             registerServiceProvider(p);
 243                         }
 244                     }
 245                     return this;
 246                 }
 247             };
 248