changeset 1854:e372934f0c42

Replaced the dbconnection.include used by Mapserver - these information are provided by LayerInfo objects now, which reduces config overhead. flys-artifacts/trunk@3199 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 09 Nov 2011 15:00:29 +0000
parents 5b2122a8bcbf
children b4798f15edc7
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/mapserver/db_layer.vm flys-artifacts/doc/conf/mapserver/dbconnection.include flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LayerInfo.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java
diffstat 7 files changed, 150 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Nov 09 14:02:15 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Nov 09 15:00:29 2011 +0000
@@ -1,3 +1,24 @@
+2011-11-09  Ingo Weinzierl <ingo@intevation.de>
+
+	* doc/conf/mapserver/dbconnection.include: Removed. DB connections are
+	  supported by LayerInfo objects now. So, we do not need to configure it any
+	  more.
+
+	* doc/conf/mapserver/db_layer.vm: The database connection is provided by
+	  LayerInfo objects. The "INCLUDE dbconnection.include" has been replaced.
+
+	* src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java: The inner
+	  State class got two new methods that provide information about db
+	  connection string and connection type.
+
+	* src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java,
+	  src/main/java/de/intevation/flys/artifacts/model/LayerInfo.java: Both
+	  classes support getter/setter for connection and connectionType.
+
+	* src/main/java/de/intevation/flys/utils/MapfileGenerator.java: Fill
+	  LayerInfo objects used to create DB layers with the connection and
+	  connection type provided by WMSDBLayerFacet.
+
 2011-11-09  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
 	Allow height marks with points style in w-differences diagrams.
--- a/flys-artifacts/doc/conf/mapserver/db_layer.vm	Wed Nov 09 14:02:15 2011 +0000
+++ b/flys-artifacts/doc/conf/mapserver/db_layer.vm	Wed Nov 09 15:00:29 2011 +0000
@@ -2,7 +2,9 @@
     NAME "$LAYER.getName()"
     TYPE $LAYER.getType()
 
-    INCLUDE "$CONFIGDIR/mapserver/dbconnection.include"
+    CONNECTIONTYPE $LAYER.getConnectionType()
+    CONNECTION "$LAYER.getConnection()"
+
     DATA    "$LAYER.getData()"
     FILTER  '$LAYER.getFilter()'
     EXTENT  $LAYER.getExtent()
--- a/flys-artifacts/doc/conf/mapserver/dbconnection.include	Wed Nov 09 14:02:15 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-CONNECTIONTYPE oraclespatial
-CONNECTION "flys26/flys26@czech-republic.atlas.intevation.de"
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java	Wed Nov 09 14:02:15 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java	Wed Nov 09 15:00:29 2011 +0000
@@ -3,10 +3,15 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
 import org.w3c.dom.Document;
 
 import org.apache.log4j.Logger;
 
+import org.hibernate.impl.SessionFactoryImpl;
+
 import com.vividsolutions.jts.geom.Envelope;
 
 import de.intevation.artifacts.Artifact;
@@ -20,8 +25,11 @@
 import de.intevation.artifacts.common.ArtifactNamespaceContext;
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.flys.backend.SessionFactoryProvider;
+
 import de.intevation.flys.artifacts.states.DefaultState;
 import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
+import de.intevation.flys.utils.FLYSUtils;
 
 
 public abstract class WMSDBArtifact extends StaticFLYSArtifact {
@@ -30,6 +38,9 @@
 
     public static final String XPATH_IDS = "/art:action/art:ids/@value";
 
+    public static final Pattern DB_URL_PATTERN =
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
+
 
     @Override
     public void setup(
@@ -126,12 +137,85 @@
             facet.setData(getDataString());
             facet.setFilter(getFilter());
             facet.setGeometryType(getGeometryType());
+            facet.setConnection(getConnection());
+            facet.setConnectionType(getConnectionType());
 
             facets.add(facet);
 
             return null;
         }
 
+        /**
+         * This method returns a connection string for databases used by
+         * Mapserver's Mapfile.
+         *
+         * @return A connection string for Mapserver.
+         */
+        protected String getConnection() {
+            SessionFactoryImpl sf = (SessionFactoryImpl)
+                SessionFactoryProvider.getSessionFactory();
+
+            String user = SessionFactoryProvider.getUser(sf);
+            String pass = SessionFactoryProvider.getPass(sf);
+            String url  = SessionFactoryProvider.getURL(sf);
+
+            logger.debug("Parse connection url: " + url);
+
+            Matcher m = DB_URL_PATTERN.matcher(url);
+            if (!m.matches()) {
+                logger.warn("Could not parse Connection string.");
+                return null;
+            }
+
+            logger.debug("Groups for connection string: " + m.groupCount());
+            int groups = m.groupCount();
+
+            for (int i = 0; i <= m.groupCount(); i++) {
+                logger.debug("Group " + i + ": " + m.group(i));
+            }
+
+            String connection = null;
+
+            if (FLYSUtils.isUsingOracle()) {
+                if (groups < 3) {
+                    logger.warn("Could only partially parse connection string.");
+                    return null;
+                }
+
+                String host = m.group(2);
+                String port = m.group(3);
+
+                connection = user + "/" + pass + "@" + host;
+            }
+            else {
+                if (groups < 4) {
+                    logger.warn("Could only partially parse connection string.");
+                    return null;
+                }
+
+                String host = m.group(2);
+                String port = m.group(3);
+                String db   = m.group(4);
+
+                StringBuilder sb = new StringBuilder();
+                sb.append("dbname=" + db);
+                sb.append("host='" + host + "'");
+                sb.append("port=" + port);
+                sb.append("password='" + pass + "'");
+                sb.append("sslmode=disable");
+
+                connection = sb.toString();
+            }
+
+            logger.debug("Created connection: '" + connection + "'");
+
+            return connection;
+        }
+
+        protected String getConnectionType() {
+            return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
+        }
+
         protected abstract String getFacetType();
 
         protected abstract String getTitle(CallMeta meta);
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LayerInfo.java	Wed Nov 09 14:02:15 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LayerInfo.java	Wed Nov 09 15:00:29 2011 +0000
@@ -7,6 +7,8 @@
     protected String type;
     protected String directory;
     protected String data;
+    protected String connection;
+    protected String connectionType;
     protected String extent;
     protected String group;
     protected String groupTitle;
@@ -59,6 +61,26 @@
     }
 
 
+    public void setConnection(String connection) {
+        this.connection = connection;
+    }
+
+
+    public String getConnection() {
+        return connection;
+    }
+
+
+    public void setConnectionType(String connectionType) {
+        this.connectionType = connectionType;
+    }
+
+
+    public String getConnectionType() {
+        return connectionType;
+    }
+
+
     public void setGroup(String group) {
         this.group = group;
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java	Wed Nov 09 14:02:15 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WMSDBLayerFacet.java	Wed Nov 09 15:00:29 2011 +0000
@@ -8,6 +8,8 @@
     protected String data;
     protected String filter;
     protected String geometryType;
+    protected String connection;
+    protected String connectionType;
 
 
     public WMSDBLayerFacet() {
@@ -69,5 +71,21 @@
     public String getGeometryType() {
         return geometryType;
     }
+
+    public void setConnection(String connection) {
+        this.connection = connection;
+    }
+
+    public String getConnection() {
+        return connection;
+    }
+
+    public void setConnectionType(String connectionType) {
+        this.connectionType = connectionType;
+    }
+
+    public String getConnectionType() {
+        return connectionType;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Wed Nov 09 14:02:15 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java	Wed Nov 09 15:00:29 2011 +0000
@@ -529,6 +529,8 @@
         layerinfo.setTitle(wms.getDescription());
         layerinfo.setStyle(style);
         layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent()));
+        layerinfo.setConnection(wms.getConnection());
+        layerinfo.setConnectionType(wms.getConnectionType());
 
         String name = MS_LAYER_PREFIX + wms.getName();
 

http://dive4elements.wald.intevation.org