Hi,
I'm trying to find the right place to invoke the method 
ShapefileDataStore.dispose in my code. I know I must invoke it because, if I 
don't, several messages are logged
. SEVERE: Undisposed of shapefile, you should call dispose() on all shapefile 
stores
. SEVERE: The following locker still has a lock: read on file:/A:/Streets.shp 
by org.geotools.data.shapefile.shp.ShapefileReader
. SEVERE: The following locker still has a lock: read on file:/A:/Streets.shx 
by org.geotools.data.shapefile.shp.IndexFile
. SEVERE: The following locker still has a lock: read on file:/A:/Streets.dbf 
by org.geotools.data.shapefile.dbf.DbaseFileReader
And the program ends with an exception: java.lang.IllegalArgumentException: 
Expected requestor org.geotools.data.shapefile.dbf.DbaseFileReader@21955fe1 to 
have locked the url but it does not hold the lock for the URL

So I tried to invoke dispose right after ShapefileDataStore.getFeatureSource() 
or right after ShapefileDataStore.getFeatureSource().getFeatures(): in either 
case, when I try to get the SimpleFeatureIterator, I get a NullPointerException 
in ShapefileDataStore.getTypeName. 

Then I tried to invoke dispose after 
ShapefileDataStore.getFeatureSource().getFeatures().features(), that is, when I 
already had the SimpleFeatureIterator, and then it was too late: I get the 
messages above about lockers that still have a lock, and the program ended with 
the same IllegalArgumentException above.

And finally I decided to ask the user list.

The output of GeoTools.getAboutInfo()  is

GeoTools version 11.0
Java version: 1.7.0_45
Operating system: Windows 7 6.1
GeoTools jars on classpath:

The program I'm trying to make work is below. Thanks for any help.

-- Clovis

import java.io.IOException;
import java.nio.file.*;
import java.util.Locale;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.*;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.GeoTools;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.type.FeatureType;
import org.opengis.filter.*;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

public class SmallExample {
    private static final boolean USE_FILTER = true;
    private static final int DISPOSE_DATA_STORE = 3;

    public static void main(String[] args) throws Exception {
        System.setProperty("gt2.shapefile.trace",  "true");
        Locale.setDefault(Locale.US);
        System.out.println(GeoTools.getAboutInfo());

        try (SimpleFeatureIterator iterator = getFeatures(Paths.get(args[0]))) {
            System.gc(); // forces the invocation of 
ShapefileDataStore.finalize 
        }
        // IllegalArgumentException when closing the iterator if the 
ShapefileDataStore is still open
    }

    private static SimpleFeatureIterator getFeatures(Path file) throws 
IOException {
        ShapefileDataStore dataStore = new 
ShapefileDataStore(file.toUri().toURL());
        try {
            SimpleFeatureSource source = dataStore.getFeatureSource();
            
            if (DISPOSE_DATA_STORE == 1) {
                dataStore.dispose(); // fine only if no filter is used
            }
            SimpleFeatureCollection featureCollection;
            if (USE_FILTER) {
                FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
                FeatureType schema = source.getSchema();

                String geometryPropertyName = 
schema.getGeometryDescriptor().getLocalName();
                CoordinateReferenceSystem targetCRS = 
schema.getGeometryDescriptor().getCoordinateReferenceSystem();

                ReferencedEnvelope bbox = new ReferencedEnvelope(15.45, 16.55, 
48.02, 48.42, targetCRS);

                Filter filter = ff.bbox(ff.property(geometryPropertyName), 
bbox);
                featureCollection = source.getFeatures(filter);
            } else {
                featureCollection = source.getFeatures();
            }
            
            if (DISPOSE_DATA_STORE == 2) {
                dataStore.dispose(); // fine only if no filter is used
            }

            return featureCollection.features(); // NullPointerException if the 
ShapefileDataStore was disposed and the feature collection has a filter
        } finally {
            if (DISPOSE_DATA_STORE == 3) {
                dataStore.dispose();
            }
        }
    }
}

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
• 3 signs your SCM is hindering your productivity
• Requirements for releasing software faster
• Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to