comparison flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java @ 4841:ad0990a82ab8

Insert db connection into riveraxis map files. * Added new MapUtils. * Moved connection specific strings to maputils. * Updated mapfile template.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 23 Jan 2013 17:32:30 +0100
parents
children 48b231a02d3a
comparison
equal deleted inserted replaced
4840:8cd2f20b1d8c 4841:ad0990a82ab8
1 package de.intevation.flys.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 de.intevation.flys.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-Z]+)");
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 for (int i = 0; i <= groups; i++) {
50 logger.debug("Group " + i + ": " + m.group(i));
51 }
52
53 String connection = null;
54
55 if (FLYSUtils.isUsingOracle()) {
56 if (groups < 3) {
57 logger.warn("Could only partially parse connection string.");
58 return null;
59 }
60
61 String host = m.group(2);
62 String port = m.group(3);
63
64 connection = user + "/" + pass + "@" + host;
65 }
66 else {
67 if (groups < 4) {
68 logger.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 db = m.group(4);
75
76 StringBuilder sb = new StringBuilder();
77 sb.append("dbname=" + db);
78 sb.append("host='" + host + "'");
79 sb.append("port=" + port);
80 sb.append("password='" + pass + "'");
81 sb.append("sslmode=disable");
82
83 connection = sb.toString();
84 }
85
86 return connection;
87 }
88
89 protected static String getPostgreSQLConnection() {
90 SessionFactoryImpl sf = (SessionFactoryImpl)
91 SessionFactoryProvider.getSessionFactory();
92
93 String user = SessionFactoryProvider.getUser(sf);
94 String pass = SessionFactoryProvider.getPass(sf);
95 String url = SessionFactoryProvider.getURL(sf);
96
97 Matcher m = DB_PSQL_URL_PATTERN.matcher(url);
98 if (!m.matches()) {
99 logger.warn("Could not parse PostgreSQL Connection string.");
100 return null;
101 }
102
103 int groups = m.groupCount();
104 logger.debug("Groups for PostgreSQL connection string: " + groups);
105
106 if (logger.isDebugEnabled()) {
107 for (int i = 0; i <= groups; i++) {
108 logger.debug("Group " + i + ": " + m.group(i));
109 }
110 }
111
112 String connection = null;
113
114 if (groups < 4) {
115 logger.warn("Could only partially parse connection string.");
116 return null;
117 }
118
119 String host = m.group(2);
120 String port = m.group(3);
121 String db = m.group(4);
122
123 StringBuilder sb = new StringBuilder();
124 sb.append("dbname=" + db);
125 sb.append(" host='" + host + "'");
126 sb.append(" port=" + port);
127 sb.append(" user=" + user);
128 sb.append(" password='" + pass + "'");
129 sb.append(" sslmode=disable");
130
131 connection = sb.toString();
132
133 logger.debug("Created connection: '" + connection + "'");
134
135 return connection;
136 }
137
138 public static String getConnectionType() {
139 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
140 }
141 }

http://dive4elements.wald.intevation.org