Ignore:
Timestamp:
2012-11-18T21:37:23+01:00 (12 years ago)
Author:
donvip
Message:

[josm_geotools] Fix JAI initialization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/geotools/src/org/openstreetmap/josm/plugins/geotools/GeoToolsPlugin.java

    r28592 r28950  
    22package org.openstreetmap.josm.plugins.geotools;
    33
     4import java.io.IOException;
     5import java.io.InputStream;
     6
     7import javax.media.jai.JAI;
     8import javax.media.jai.OperationRegistry;
     9
     10import org.geotools.referencing.CRS;
     11import org.opengis.referencing.FactoryException;
     12import org.opengis.referencing.NoSuchAuthorityCodeException;
    413import org.openstreetmap.josm.plugins.Plugin;
    514import org.openstreetmap.josm.plugins.PluginInformation;
     15
     16import com.sun.media.jai.imageioimpl.ImageReadWriteSpi;
    617
    718public class GeoToolsPlugin extends Plugin {
    819    public GeoToolsPlugin(PluginInformation info) {
    920        super(info);
     21        initJAI();
     22        checkEPSG();
     23    }
     24
     25    private void initJAI() {
     26        // As the JAI jars are bundled in the geotools plugin, JAI initialization does not work,
     27        // so we need to perform the tasks described here ("Initialization and automatic loading of registry objects"):
     28        // http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/OperationRegistry.html
     29        OperationRegistry registry = JAI.getDefaultInstance().getOperationRegistry();
     30        if (registry == null) {
     31            System.err.println("geotools: error in JAI initialization. Cannot access default operation registry");
     32        } else {
     33            // Update registry with com.sun.media.jai.imageioimpl.ImageReadWriteSpi (only class listed javax.media.jai.OperationRegistrySpi)
     34            // it would be safer to parse this file instead, but a JAI update is very unlikely as it has not been modified since 2005
     35            new ImageReadWriteSpi().updateRegistry(registry);
     36
     37            // Update registry with GeoTools registry file
     38            InputStream in = GeoToolsPlugin.class.getResourceAsStream("/META-INF/registryFile.jai");
     39            if (in == null) {
     40                System.err.println("geotools: error in JAI initialization. Cannot access META-INF/registryFile.jai");
     41            } else {
     42                try {
     43                    registry.updateFromStream(in);
     44                } catch (IOException e) {
     45                    System.err.println("geotools: error in JAI initialization. Cannot update default operation registry");
     46                }
     47                try {
     48                    in.close();
     49                } catch (IOException e) {
     50                    System.err.println("geotools: error in JAI initialization. Cannot close input stream");
     51                }
     52            }
     53           
     54            // Print JAI registry contents
     55            //for (String mode : RegistryMode.getModeNames()) {
     56            //    System.out.println("geotools: JAI mode "+mode+": "+Arrays.toString(registry.getDescriptorNames(mode)));
     57            //}
     58        }
     59    }
     60
     61    private void checkEPSG() {
     62        try {
     63            CRS.decode("EPSG:4326");
     64        } catch (NoSuchAuthorityCodeException e) {
     65            System.err.println("geotools: error in EPSG database initialization. NoSuchAuthorityCodeException: "+e.getMessage());
     66        } catch (FactoryException e) {
     67            System.err.println("geotools: error in EPSG database initialization. FactoryException: "+e.getMessage());
     68        }
    1069    }
    1170}
Note: See TracChangeset for help on using the changeset viewer.