1

I'm writing a small Java EE 6 Web Service application that is for converting ESRI Shape Files and passing the data into another application.

I have the application working fine as a command line app. However whilst porting the code, it has started to throw an exception whilst running on the Glassfish server.

The code is checking to see if the coordinate projection type is a valid projection for our legacy app and should be returning a boolean value:

/**
     * Validates that the shape file is using one of the valid coordinate systems. Returns true if
     * the coordinate system is valid.
     * 
     * @param shpFilePath The path to the shape files.
     * @return
     */
    public boolean validCoordinateSystem(String shpFilePath) {
        try {
            ShpFiles shapeFiles = new ShpFiles(shpFilePath);

            PrjFileReader fileReader = new PrjFileReader(shapeFiles);

            CoordinateReferenceSystem coordSystem = fileReader.getCoodinateSystem();

            for (int i = 0; i < this.acceptedCoordinateSystems.length; i++) {

                if (this.acceptedCoordinateSystems[i].equalsIgnoreCase(coordSystem.getName().toString())) {
                    return true;
                }

            }
            fileReader.close();

        } catch (MalformedURLException ex) {
            Logger.getLogger(GeoTools.class.getName()).log(Level.SEVERE, null, ex);

            return false;
        } catch (IOException ioe) {

            Logger.getLogger(GeoTools.class.getName()).log(Level.SEVERE, null, ioe);

            return false;

        } catch (Exception nex){

            Logger.getLogger(GeoTools.class.getName()).log(Level.SEVERE, null, nex);

            return false;
        }

        return false;
    }

This code runs fine normally as a command line app. However when running this method in glassfish I am getting the following stacktrace:

org.geotools.factory.FactoryNotFoundException: No factory of kind "MathTransformFactory" found.
        at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:374)
        at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:143)
        at org.geotools.referencing.ReferencingFactoryFinder.getFactory(ReferencingFactoryFinder.java:193)
        at org.geotools.referencing.ReferencingFactoryFinder.getMathTransformFactory(ReferencingFactoryFinder.java:503)
        at org.geotools.referencing.factory.ReferencingObjectFactory.getMathTransformFactory(ReferencingObjectFactory.java:152)
        at org.geotools.referencing.factory.ReferencingObjectFactory.createFromWKT(ReferencingObjectFactory.java:1084)
        at org.geotools.data.shapefile.prj.PrjFileReader.<init>(PrjFileReader.java:78)
        at com.spectrum.demeter.utils.GeoTools.validCoordinateSystem(GeoTools.java:61)
        at com.spectrum.demeter.ejb.Converter.addSurveyToDatabase(Converter.java:117)
        at com.spectrum.demeter.ws.SurveyManager.convertShpToDb(SurveyManager.java:76)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.glassfish.webservices.InstanceResolverImpl$1.invoke(InstanceResolverImpl.java:137)

I've check to ensure that I have all the geotools.jar files added to the project that I need however I am not sure what is causing this error and (although I think I might of missed it) can't find anything in the docs or api.

Any help in fixing this problem would be fantastic.

Thanks Grant.

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
Grant Collins
  • 1,781
  • 5
  • 31
  • 47
  • Just a quick update the code runs fine as a unit test? I think this has something to do with the Glassfish server but can't see why. – Grant Collins Jan 14 '11 at 15:24
  • We hit this same error while running Geotools in Hadoop. I don't know if this is the ideal fix, but we have resolved it by using the maven-shade-plugin to package our dependencies in with our hadoop-job.jar rather than using geotools as a shared library through the distributed cache. – arcdrag Jun 19 '13 at 19:16

2 Answers2

2

I had this problem with my JUnit tests using GeoTools. It turned out I needed to include dependencies for: vecmath and jsr-275.

cs94njw
  • 535
  • 5
  • 12
  • Confirm this works for me when using geo-tools in a pig script and encountering the exact same error – piggybox Sep 22 '14 at 22:01
2

GeoTools uses the Java Service Provide Interface (SPI) plugin system; this depends on a magic directory in each jar file (META_INF/servies/). This directory lists all the plugin classes available in the jar; in this case the MathTransformFactory implementation).

The trouble is that in some environments (usually OSGi) access to files (rather than classes) in a Jar is considered a security risk.

Can you ensure that your glassfish environment allows these files to be accessed? Java itself is wired up this way so it is not uncommon to see workarounds proposed for Java Advanced Imaging (where new image formats are registered using this mechanism).

Jody Garnett
  • 503
  • 3
  • 10