[PATCH] Tighten code a bit
Wald Commits
scm-commit at wald.intevation.org
Wed May 20 21:47:39 CEST 2020
# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1589989827 -7200
# Wed May 20 17:50:27 2020 +0200
# Branch 3.2.x
# Node ID 07d854c325d8bf776e95bb83b20a02ca69446081
# Parent aee871c6059987606aad2defc3d03f8b32434d06
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.
diff -r aee871c60599 -r 07d854c325d8 artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java
--- 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";
}
More information about the Dive4Elements-commits
mailing list