comparison artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.utils;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import org.apache.log4j.Logger;
7 import org.hibernate.impl.SessionFactoryImpl;
8
9 import org.dive4elements.river.backend.SessionFactoryProvider;
10
11
12 public class MapUtils
13 {
14 private static final Logger logger = Logger.getLogger(MapUtils.class);
15
16 public static final Pattern DB_URL_PATTERN =
17 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([\\.a-zA-Z0-9_-]+)");
18
19 public static final Pattern DB_PSQL_URL_PATTERN =
20 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9_-]+)");
21
22 /**
23 * This method returns a connection string for databases used by
24 * Mapserver's Mapfile.
25 *
26 * @return A connection string for Mapserver.
27 */
28 public static String getConnection() {
29 SessionFactoryImpl sf = (SessionFactoryImpl)
30 SessionFactoryProvider.getSessionFactory();
31
32 String user = SessionFactoryProvider.getUser(sf);
33 String pass = SessionFactoryProvider.getPass(sf);
34 String url = SessionFactoryProvider.getURL(sf);
35
36 logger.debug("Parse connection url: " + url);
37
38 Matcher m = DB_URL_PATTERN.matcher(url);
39 if (!m.matches()) {
40 logger.warn("Could not parse Connection string." +
41 "Try to parse PostgreSQL string.");
42 // maybe this is a PostgreSQL connection...
43 return getPostgreSQLConnection();
44 }
45
46 logger.debug("Groups for connection string: " + m.groupCount());
47 int groups = m.groupCount();
48
49
50 if (logger.isDebugEnabled()) {
51 for (int i = 0; i <= groups; i++) {
52 logger.debug("Group " + i + ": " + m.group(i));
53 }
54 }
55
56 String connection = null;
57
58 if (FLYSUtils.isUsingOracle()) {
59 if (groups < 4) {
60 logger.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 backend = m.group(4);
67 connection = user + "/" + pass + "@" + host + ":" + port + "/" + backend;
68 }
69 else {
70 if (groups < 4) {
71 logger.warn("Could only partially parse connection string.");
72 return null;
73 }
74
75 String host = m.group(2);
76 String port = m.group(3);
77 String db = m.group(4);
78
79 connection = createConnectionString(user, pass, host, db, port);
80 }
81
82 return connection;
83 }
84
85 public static String createConnectionString(
86 String user,
87 String pass,
88 String host,
89 String db,
90 String port
91 ) {
92 StringBuilder sb = new StringBuilder();
93 sb.append("dbname=").append(db);
94 sb.append(" host='").append(host).append("'");
95 sb.append(" user=").append(user);
96 sb.append(" port=").append(port);
97 // XXX: We need to escape this somehow.
98 sb.append(" password='").append(pass).append("'");
99 sb.append(" sslmode=disable");
100 return sb.toString();
101 }
102
103 protected static String getPostgreSQLConnection() {
104 SessionFactoryImpl sf = (SessionFactoryImpl)
105 SessionFactoryProvider.getSessionFactory();
106
107 String user = SessionFactoryProvider.getUser(sf);
108 String pass = SessionFactoryProvider.getPass(sf);
109 String url = SessionFactoryProvider.getURL(sf);
110
111 Matcher m = DB_PSQL_URL_PATTERN.matcher(url);
112 if (!m.matches()) {
113 logger.warn("Could not parse PostgreSQL Connection string.");
114 return null;
115 }
116
117 int groups = m.groupCount();
118 logger.debug("Groups for PostgreSQL connection string: " + groups);
119
120 if (logger.isDebugEnabled()) {
121 for (int i = 0; i <= groups; i++) {
122 logger.debug("Group " + i + ": " + m.group(i));
123 }
124 }
125
126 String connection = null;
127
128 if (groups < 4) {
129 logger.warn("Could only partially parse connection string.");
130 return null;
131 }
132
133 String host = m.group(2);
134 String port = m.group(3);
135 String db = m.group(4);
136
137 connection = createConnectionString(user, pass, host, db, port);
138
139 logger.debug("Created connection: '" + connection + "'");
140
141 return connection;
142 }
143
144 public static String getConnectionType() {
145 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
146 }
147 }

http://dive4elements.wald.intevation.org