annotate flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java @ 5133:8e52b4829cd1

Fix flys/issue1228: Units in tabular calculation output.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 28 Feb 2013 12:37:07 +0100
parents ad0990a82ab8
children 48b231a02d3a
rev   line source
4841
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.utils;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
2
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
3 import java.util.regex.Matcher;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
4 import java.util.regex.Pattern;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
5
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
6 import org.apache.log4j.Logger;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
7 import org.hibernate.impl.SessionFactoryImpl;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
8
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
9 import de.intevation.flys.backend.SessionFactoryProvider;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
12 public class MapUtils
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
14 private static final Logger logger = Logger.getLogger(MapUtils.class);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
15
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
16 public static final Pattern DB_URL_PATTERN =
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
17 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
18
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
19 public static final Pattern DB_PSQL_URL_PATTERN =
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
20 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
21
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
22 /**
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 * This method returns a connection string for databases used by
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
24 * Mapserver's Mapfile.
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
25 *
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
26 * @return A connection string for Mapserver.
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
27 */
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
28 public static String getConnection() {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
29 SessionFactoryImpl sf = (SessionFactoryImpl)
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
30 SessionFactoryProvider.getSessionFactory();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
31
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
32 String user = SessionFactoryProvider.getUser(sf);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 String pass = SessionFactoryProvider.getPass(sf);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
34 String url = SessionFactoryProvider.getURL(sf);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
35
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
36 logger.debug("Parse connection url: " + url);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
38 Matcher m = DB_URL_PATTERN.matcher(url);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 if (!m.matches()) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40 logger.warn("Could not parse Connection string." +
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 "Try to parse PostgreSQL string.");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42 // maybe this is a PostgreSQL connection...
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
43 return getPostgreSQLConnection();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
44 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
45
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
46 logger.debug("Groups for connection string: " + m.groupCount());
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 int groups = m.groupCount();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49 for (int i = 0; i <= groups; i++) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
50 logger.debug("Group " + i + ": " + m.group(i));
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
51 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
52
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
53 String connection = null;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
54
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
55 if (FLYSUtils.isUsingOracle()) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
56 if (groups < 3) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
57 logger.warn("Could only partially parse connection string.");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
58 return null;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
59 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
60
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
61 String host = m.group(2);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
62 String port = m.group(3);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
64 connection = user + "/" + pass + "@" + host;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
66 else {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
67 if (groups < 4) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
68 logger.warn("Could only partially parse connection string.");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
69 return null;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
70 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
71
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
72 String host = m.group(2);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
73 String port = m.group(3);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
74 String db = m.group(4);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
75
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
76 StringBuilder sb = new StringBuilder();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
77 sb.append("dbname=" + db);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
78 sb.append("host='" + host + "'");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
79 sb.append("port=" + port);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
80 sb.append("password='" + pass + "'");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
81 sb.append("sslmode=disable");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
82
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
83 connection = sb.toString();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
84 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
85
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
86 return connection;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
87 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
88
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
89 protected static String getPostgreSQLConnection() {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
90 SessionFactoryImpl sf = (SessionFactoryImpl)
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
91 SessionFactoryProvider.getSessionFactory();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
92
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
93 String user = SessionFactoryProvider.getUser(sf);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
94 String pass = SessionFactoryProvider.getPass(sf);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
95 String url = SessionFactoryProvider.getURL(sf);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
96
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
97 Matcher m = DB_PSQL_URL_PATTERN.matcher(url);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
98 if (!m.matches()) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
99 logger.warn("Could not parse PostgreSQL Connection string.");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
100 return null;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
101 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
102
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
103 int groups = m.groupCount();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
104 logger.debug("Groups for PostgreSQL connection string: " + groups);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
105
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
106 if (logger.isDebugEnabled()) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
107 for (int i = 0; i <= groups; i++) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
108 logger.debug("Group " + i + ": " + m.group(i));
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
109 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
110 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
111
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
112 String connection = null;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
113
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
114 if (groups < 4) {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
115 logger.warn("Could only partially parse connection string.");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
116 return null;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
117 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
118
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
119 String host = m.group(2);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
120 String port = m.group(3);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
121 String db = m.group(4);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
122
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
123 StringBuilder sb = new StringBuilder();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
124 sb.append("dbname=" + db);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
125 sb.append(" host='" + host + "'");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
126 sb.append(" port=" + port);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
127 sb.append(" user=" + user);
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
128 sb.append(" password='" + pass + "'");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
129 sb.append(" sslmode=disable");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
130
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
131 connection = sb.toString();
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
132
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
133 logger.debug("Created connection: '" + connection + "'");
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
134
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
135 return connection;
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
136 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
137
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
138 public static String getConnectionType() {
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
139 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
140 }
ad0990a82ab8 Insert db connection into riveraxis map files.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
141 }

http://dive4elements.wald.intevation.org