Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java @ 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 | 5e38e2924c07 |
children | 9cfc495a9f40 |
comparison
equal
deleted
inserted
replaced
9669:aee871c60599 | 9670:07d854c325d8 |
---|---|
22 private static final Logger log = Logger.getLogger(MapUtils.class); | 22 private static final Logger log = Logger.getLogger(MapUtils.class); |
23 | 23 |
24 public static final Pattern DB_URL_PATTERN = | 24 public static final Pattern DB_URL_PATTERN = |
25 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([\\.a-zA-Z0-9_-]+)"); | 25 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([\\.a-zA-Z0-9_-]+)"); |
26 | 26 |
27 public static final Pattern DB_PSQL_URL_PATTERN = | |
28 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9_-]+)"); | |
29 | |
30 /** | 27 /** |
31 * This method returns a connection string for databases used by | 28 * This method returns a connection string for databases used by |
32 * Mapserver's Mapfile. | 29 * Mapserver's Mapfile. |
33 * | 30 * |
34 * @return A connection string for Mapserver. | 31 * @return A connection string for Mapserver. |
43 | 40 |
44 log.debug("Parse connection url: " + url); | 41 log.debug("Parse connection url: " + url); |
45 | 42 |
46 Matcher m = DB_URL_PATTERN.matcher(url); | 43 Matcher m = DB_URL_PATTERN.matcher(url); |
47 if (!m.matches()) { | 44 if (!m.matches()) { |
48 log.warn("Could not parse Connection string." + | 45 log.warn("Could not parse Connection string"); |
49 "Try to parse PostgreSQL string."); | 46 return null; |
50 // maybe this is a PostgreSQL connection... | |
51 return getPostgreSQLConnection(); | |
52 } | 47 } |
53 | 48 |
54 log.debug("Groups for connection string: " + m.groupCount()); | |
55 int groups = m.groupCount(); | 49 int groups = m.groupCount(); |
56 | |
57 | 50 |
58 if (log.isDebugEnabled()) { | 51 if (log.isDebugEnabled()) { |
59 for (int i = 0; i <= groups; i++) { | 52 for (int i = 0; i <= groups; i++) { |
60 log.debug("Group " + i + ": " + m.group(i)); | 53 log.debug("Group " + i + ": " + m.group(i)); |
61 } | 54 } |
62 } | 55 } |
63 | 56 |
64 String connection = null; | 57 String connection = null; |
65 | 58 |
59 if (groups < 4) { | |
60 log.warn("Could only partially parse connection string."); | |
61 return null; | |
62 } | |
63 | |
64 String host = m.group(2); | |
65 String port = m.group(3); | |
66 String db = m.group(4); | |
67 | |
66 if (RiverUtils.isUsingOracle()) { | 68 if (RiverUtils.isUsingOracle()) { |
67 if (groups < 4) { | |
68 log.warn("Could only partially parse connection string."); | |
69 return null; | |
70 } | |
71 | |
72 String host = m.group(2); | |
73 String port = m.group(3); | |
74 String backend = m.group(4); | |
75 connection = user + "/" + pass | 69 connection = user + "/" + pass |
76 + "@" + host + ":" + port + "/" + backend; | 70 + "@" + host + ":" + port + "/" + db; |
77 } | 71 } |
78 else { | 72 else { |
79 if (groups < 4) { | |
80 log.warn("Could only partially parse connection string."); | |
81 return null; | |
82 } | |
83 | |
84 String host = m.group(2); | |
85 String port = m.group(3); | |
86 String db = m.group(4); | |
87 | |
88 connection = createConnectionString(user, pass, host, db, port); | 73 connection = createConnectionString(user, pass, host, db, port); |
89 } | 74 } |
90 | 75 |
91 return connection; | 76 return connection; |
92 } | 77 } |
107 sb.append(" password='").append(pass).append("'"); | 92 sb.append(" password='").append(pass).append("'"); |
108 sb.append(" sslmode=disable"); | 93 sb.append(" sslmode=disable"); |
109 return sb.toString(); | 94 return sb.toString(); |
110 } | 95 } |
111 | 96 |
112 protected static String getPostgreSQLConnection() { | |
113 SessionFactoryImpl sf = (SessionFactoryImpl) | |
114 SessionFactoryProvider.getSessionFactory(); | |
115 | |
116 String user = SessionFactoryProvider.getUser(sf); | |
117 String pass = SessionFactoryProvider.getPass(sf); | |
118 String url = SessionFactoryProvider.getURL(sf); | |
119 | |
120 Matcher m = DB_PSQL_URL_PATTERN.matcher(url); | |
121 if (!m.matches()) { | |
122 log.warn("Could not parse PostgreSQL Connection string."); | |
123 return null; | |
124 } | |
125 | |
126 int groups = m.groupCount(); | |
127 log.debug("Groups for PostgreSQL connection string: " + groups); | |
128 | |
129 if (log.isDebugEnabled()) { | |
130 for (int i = 0; i <= groups; i++) { | |
131 log.debug("Group " + i + ": " + m.group(i)); | |
132 } | |
133 } | |
134 | |
135 String connection = null; | |
136 | |
137 if (groups < 4) { | |
138 log.warn("Could only partially parse connection string."); | |
139 return null; | |
140 } | |
141 | |
142 String host = m.group(2); | |
143 String port = m.group(3); | |
144 String db = m.group(4); | |
145 | |
146 connection = createConnectionString(user, pass, host, db, port); | |
147 | |
148 log.debug("Created connection: '" + connection + "'"); | |
149 | |
150 return connection; | |
151 } | |
152 | |
153 public static String getConnectionType() { | 97 public static String getConnectionType() { |
154 return RiverUtils.isUsingOracle() ? "oraclespatial" : "postgis"; | 98 return RiverUtils.isUsingOracle() ? "oraclespatial" : "postgis"; |
155 } | 99 } |
156 } | 100 } |