Mercurial > dive4elements > river
changeset 9670:07d854c325d8 3.2.x
Tighten code a bit
PostgreSQL database names might also contain dots and since the extra
pattern matches a subset of the other, it will never match anyhow.
Thus remove the extra pattern, a lot of duplicate code and some
superfluous debug logging.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Wed, 20 May 2020 17:50:27 +0200 |
parents | aee871c60599 |
children | 9cfc495a9f40 |
files | artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java |
diffstat | 1 files changed, 12 insertions(+), 68 deletions(-) [+] |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java Tue May 19 20:07:44 2020 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java Wed May 20 17:50:27 2020 +0200 @@ -24,9 +24,6 @@ public static final Pattern DB_URL_PATTERN = Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([\\.a-zA-Z0-9_-]+)"); - public static final Pattern DB_PSQL_URL_PATTERN = - Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9_-]+)"); - /** * This method returns a connection string for databases used by * Mapserver's Mapfile. @@ -45,16 +42,12 @@ Matcher m = DB_URL_PATTERN.matcher(url); if (!m.matches()) { - log.warn("Could not parse Connection string." + - "Try to parse PostgreSQL string."); - // maybe this is a PostgreSQL connection... - return getPostgreSQLConnection(); + log.warn("Could not parse Connection string"); + return null; } - log.debug("Groups for connection string: " + m.groupCount()); int groups = m.groupCount(); - if (log.isDebugEnabled()) { for (int i = 0; i <= groups; i++) { log.debug("Group " + i + ": " + m.group(i)); @@ -63,28 +56,20 @@ String connection = null; + if (groups < 4) { + log.warn("Could only partially parse connection string."); + return null; + } + + String host = m.group(2); + String port = m.group(3); + String db = m.group(4); + if (RiverUtils.isUsingOracle()) { - if (groups < 4) { - log.warn("Could only partially parse connection string."); - return null; - } - - String host = m.group(2); - String port = m.group(3); - String backend = m.group(4); connection = user + "/" + pass - + "@" + host + ":" + port + "/" + backend; + + "@" + host + ":" + port + "/" + db; } else { - if (groups < 4) { - log.warn("Could only partially parse connection string."); - return null; - } - - String host = m.group(2); - String port = m.group(3); - String db = m.group(4); - connection = createConnectionString(user, pass, host, db, port); } @@ -109,47 +94,6 @@ return sb.toString(); } - protected static String getPostgreSQLConnection() { - SessionFactoryImpl sf = (SessionFactoryImpl) - SessionFactoryProvider.getSessionFactory(); - - String user = SessionFactoryProvider.getUser(sf); - String pass = SessionFactoryProvider.getPass(sf); - String url = SessionFactoryProvider.getURL(sf); - - Matcher m = DB_PSQL_URL_PATTERN.matcher(url); - if (!m.matches()) { - log.warn("Could not parse PostgreSQL Connection string."); - return null; - } - - int groups = m.groupCount(); - log.debug("Groups for PostgreSQL connection string: " + groups); - - if (log.isDebugEnabled()) { - for (int i = 0; i <= groups; i++) { - log.debug("Group " + i + ": " + m.group(i)); - } - } - - String connection = null; - - if (groups < 4) { - log.warn("Could only partially parse connection string."); - return null; - } - - String host = m.group(2); - String port = m.group(3); - String db = m.group(4); - - connection = createConnectionString(user, pass, host, db, port); - - log.debug("Created connection: '" + connection + "'"); - - return connection; - } - public static String getConnectionType() { return RiverUtils.isUsingOracle() ? "oraclespatial" : "postgis"; }