diff flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1121:66783d957201

Close open shapefile transactions properly. flys-artifacts/trunk@2628 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 01 Sep 2011 07:27:44 +0000
parents faca1825818e
children c07e9e9c7482
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java	Thu Sep 01 07:06:14 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java	Thu Sep 01 07:27:44 2011 +0000
@@ -9,6 +9,8 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
+
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Geometry;
 
@@ -33,6 +35,9 @@
 
 public class GeometryUtils {
 
+    private static final Logger logger = Logger.getLogger(GeometryUtils.class);
+
+
     private GeometryUtils() {
     }
 
@@ -133,33 +138,56 @@
     public static boolean writeShapefile(
         File              shape,
         SimpleFeatureType featureType,
-        FeatureCollection collection)
-    throws MalformedURLException, IOException
-    {
-        Map<String, Serializable> params = new HashMap<String, Serializable>();
-        params.put("url", shape.toURI().toURL());
-        params.put("create spatial index", Boolean.TRUE);
-
-        DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
-
-        ShapefileDataStore newDataStore =
-            (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
-        newDataStore.createSchema(featureType);
+        FeatureCollection collection
+    ) {
+        Transaction transaction = null;
 
-        Transaction transaction = new DefaultTransaction("create");
-
-        String typeName = newDataStore.getTypeNames()[0];
+        try {
+            Map<String, Serializable> params =
+                new HashMap<String, Serializable>();
 
-        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
-            (FeatureStore<SimpleFeatureType, SimpleFeature>)
-                newDataStore.getFeatureSource(typeName);
+            params.put("url", shape.toURI().toURL());
+            params.put("create spatial index", Boolean.TRUE);
 
-        featureStore.setTransaction(transaction);
+            DataStoreFactorySpi dataStoreFactory =
+                new ShapefileDataStoreFactory();
 
-        featureStore.addFeatures(collection);
-        transaction.commit();
+            ShapefileDataStore newDataStore =
+                (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
+            newDataStore.createSchema(featureType);
 
-        return true;
+            transaction = new DefaultTransaction("create");
+
+            String typeName = newDataStore.getTypeNames()[0];
+
+            FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
+                (FeatureStore<SimpleFeatureType, SimpleFeature>)
+                    newDataStore.getFeatureSource(typeName);
+
+            featureStore.setTransaction(transaction);
+
+            featureStore.addFeatures(collection);
+            transaction.commit();
+
+            return true;
+        }
+        catch (MalformedURLException mue) {
+            logger.error("Unable to prepare shapefile: " + mue.getMessage());
+        }
+        catch (IOException ioe) {
+            logger.error("Unable to write shapefile: " + ioe.getMessage());
+        }
+        finally {
+            if (transaction != null) {
+                try {
+                    logger.debug("XXX CLOSE TRANSACTION!");
+                    transaction.close();
+                }
+                catch (IOException ioe) { /* do nothing */ }
+            }
+        }
+
+        return false;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org