changeset 5147:48b231a02d3a

Generate valid DB connection strings.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 28 Feb 2013 18:55:42 +0100
parents d4fdd98a04f7
children 3d01658d9c9c
files flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java
diffstat 1 files changed, 27 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java	Thu Feb 28 18:04:19 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java	Thu Feb 28 18:55:42 2013 +0100
@@ -14,10 +14,10 @@
     private static final Logger logger = Logger.getLogger(MapUtils.class);
 
     public static final Pattern DB_URL_PATTERN =
-        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z_-]+)");
 
     public static final Pattern DB_PSQL_URL_PATTERN =
-        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)");
+        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9_-]+)");
 
     /**
      * This method returns a connection string for databases used by
@@ -46,8 +46,11 @@
         logger.debug("Groups for connection string: " + m.groupCount());
         int groups = m.groupCount();
 
-        for (int i = 0; i <= groups; i++) {
-            logger.debug("Group " + i + ": " + m.group(i));
+
+        if (logger.isDebugEnabled()) {
+            for (int i = 0; i <= groups; i++) {
+                logger.debug("Group " + i + ": " + m.group(i));
+            }
         }
 
         String connection = null;
@@ -73,19 +76,30 @@
             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();
+            connection = createConnectionString(user, pass, host, db, port);
         }
 
         return connection;
     }
 
+    public static String createConnectionString(
+        String user,
+        String pass,
+        String host,
+        String db,
+        String port
+    ) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("dbname=").append(db);
+        sb.append(" host='").append(host).append("'");
+        sb.append(" user=").append(user);
+        sb.append(" port=").append(port);
+        // XXX: We need to escape this somehow.
+        sb.append(" password='").append(pass).append("'");
+        sb.append(" sslmode=disable");
+        return sb.toString();
+    }
+
     protected static String getPostgreSQLConnection() {
         SessionFactoryImpl sf = (SessionFactoryImpl)
         SessionFactoryProvider.getSessionFactory();
@@ -120,15 +134,7 @@
         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(" user=" + user);
-        sb.append(" password='" + pass + "'");
-        sb.append(" sslmode=disable");
-
-        connection = sb.toString();
+        connection = createConnectionString(user, pass, host, db, port);
 
         logger.debug("Created connection: '" + connection + "'");
 

http://dive4elements.wald.intevation.org