changeset 1044:461d4489705c

Improved directoy handling for incoming wms requests on product 'Layer' (issue269). gnv-artifacts/trunk@1116 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 21 May 2010 09:16:59 +0000
parents d955571cdf2a
children 3fe08ea3f2b6
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java
diffstat 2 files changed, 58 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Fri May 21 08:28:49 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Fri May 21 09:16:59 2010 +0000
@@ -1,3 +1,15 @@
+2010-05-21  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	  Issue269
+
+	* src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java:
+	  Improved the directory handling for wms requests (directories are created
+	  if there is no directory existing for the shapefiles; the content of
+	  directories is deleted if the directory is existing). The shapefiles are
+	  always generated again as well if there is an directory existing and it
+	  contains files - we cannot know if these files are shapefiles that have
+	  been created by our application.
+
 2010-05-21  Tim Englich  <tim.englich@intevation.de>
 
 	* doc/conf/queries.properties: 
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java	Fri May 21 08:28:49 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java	Fri May 21 09:16:59 2010 +0000
@@ -501,6 +501,16 @@
         }
     }
 
+
+    /**
+     * Set the shapefile path.
+     */
+    public void setShapeFilePath(String shapeFilePath) {
+        synchronized (shapeFileLock) {
+            this.shapeFilePath = shapeFilePath;
+        }
+    }
+
     /**
      * Returns the basic-directory where the Shapefiles should be placed in.
      * @param callContext the Context of this Call
@@ -576,26 +586,37 @@
      * type, time to live of the current artifact, etc).
      * @throws StateException if an error occured while shapefile writing.
      */
-    protected Document getWMS(String uuid,
-                              CallContext callContext,
-                              Collection<LayerMetaData> layerMetaData,
-                              Collection<InputData> inputData)
+    protected Document getWMS(
+        String                    uuid,
+        CallContext               callContext,
+        Collection<LayerMetaData> layerMetaData,
+        Collection<InputData>     inputData)
     throws StateException
     {
-        String path = getShapeFilePath();
-        if (path != null &&  new File(path).isDirectory()){
-            return this.refreshMetaFile(layerMetaData, inputData,
-                                        uuid, callContext);
-        }else{
-            Document document = XMLUtils.newDocument();
-            if (this.shapeFilePath == null){
-                File baseDir = shapefileDirectory(callContext);
-                if (!this.createShapeDir(baseDir, uuid)){
-                    // TODO Insert Error Report
-                    return document;
+        Document document = XMLUtils.newDocument();
+
+        File baseDir  = shapefileDirectory(callContext);
+        File shapeDir = new File(baseDir, uuid);
+
+        boolean success    = false;
+        boolean createdDir = false;
+
+        try {
+            // create shapefile directory or delete content if it already exists
+            synchronized (shapeFileLock) {
+                if (shapeDir.exists()) {
+                    FileUtils.deleteContent(shapeDir);
                 }
+                else if (!shapeDir.mkdirs()) {
+                    log.error("cannot create directory '"
+                        + shapeDir.getAbsolutePath() + "'");
+                    return null;
+                }
+                setShapeFilePath(shapeDir.getAbsolutePath());
+                createdDir = true;
             }
-            path = getShapeFilePath();
+
+            // process data
             Iterator<LayerMetaData> it = layerMetaData.iterator();
             Node meta = null;
             int layerNumber = 0;
@@ -626,8 +647,11 @@
                                                   createShapeFileName(layerNumber),
                                                   title);
                     }
+
+                    success = true;
+
                     if (meta != null && !it.hasNext()) {
-                        MetaWriter.writeMetaFile(path,document);
+                        MetaWriter.writeMetaFile(getShapeFilePath(),document);
                         MapfileGenerator.getInstance().update();
                         return document;
                     }
@@ -637,6 +661,11 @@
             }
             return document;
         }
+        finally {
+            if (!success && createdDir) {
+                FileUtils.deleteRecursive(shapeDir);
+            }
+        }
     }
 
     /**
@@ -648,57 +677,6 @@
         return SHAPEFILE_NAME+"_"+layerNumber+".shp";
     }
 
-    /**
-     * Method that refreshes the Metadatafile for publishing the WMS
-     * Without generating the Data ones again.
-     * @param layerMetaData the Metadata which is required to create the Layers
-     * @param inputData the Inputdata which was sent by the Client
-     * @param uuid the uuid of the Artifact
-     * @param callContext the context of this Call
-     * @return a refreshed Metadata-Document
-     */
-    private Document refreshMetaFile(Collection<LayerMetaData> layerMetaData,
-                                     Collection<InputData> inputData,
-                                     String uuid,
-                                     CallContext callContext){
-        Document document = XMLUtils.newDocument();
-        Node meta = null;
-        int layerNumber = 0;
-        Iterator<LayerMetaData> it = layerMetaData.iterator();
-        while (it.hasNext()){
-            LayerMetaData lmd = it.next();
-            layerNumber ++;
-            String geometryType = lmd.getGeometryType();
-            String templateId = lmd.getTemplateID();
-            String title = getLayerTitle(inputData);
-            if (title == null) {
-                title = uuid+"_"+layerNumber;
-            }else{
-                title = title+"_"+layerNumber;
-            }
-            callContext.putContextValue(
-                MetaWriter.CONTEXT_LAYER_TITLE, title);
-            String paramType = findParameterTitle(geometryType,templateId);
-            if (log.isDebugEnabled()) {
-                log.debug("Layer title: " + title);
-                log.debug("Layer type: " + paramType);
-            }
-            if (meta == null){
-                meta = MetaWriter.writeLayerMeta(callContext,document);
-            }
-            MetaWriter.writeLayerMeta(callContext, document,
-                                      meta, uuid, paramType,
-                                      this.determineGeometryType(geometryType),
-                                      createShapeFileName(layerNumber),
-                                      title);
-            if (meta != null && !it.hasNext()) {
-                MetaWriter.writeMetaFile(getShapeFilePath(),document);
-                MapfileGenerator.getInstance().update();
-                return document;
-            }
-        }
-        return document;
-    }
 
     /**
      * Returns the parameterType for the Layer.

http://dive4elements.wald.intevation.org