comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.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 442fbb290fa8
children 88014b9852d0
comparison
equal deleted inserted replaced
4840:8cd2f20b1d8c 4841:ad0990a82ab8
12 import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet; 12 import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
13 import de.intevation.flys.artifacts.resources.Resources; 13 import de.intevation.flys.artifacts.resources.Resources;
14 import de.intevation.flys.artifacts.states.DefaultState; 14 import de.intevation.flys.artifacts.states.DefaultState;
15 import de.intevation.flys.backend.SessionFactoryProvider; 15 import de.intevation.flys.backend.SessionFactoryProvider;
16 import de.intevation.flys.utils.FLYSUtils; 16 import de.intevation.flys.utils.FLYSUtils;
17 import de.intevation.flys.utils.MapUtils;
17 18
18 import java.io.File; 19 import java.io.File;
19 import java.util.ArrayList; 20 import java.util.ArrayList;
20 import java.util.List; 21 import java.util.List;
21 import java.util.regex.Matcher; 22 import java.util.regex.Matcher;
144 facet.setOriginalExtent(getExtent(true)); 145 facet.setOriginalExtent(getExtent(true));
145 facet.setSrid(getSrid()); 146 facet.setSrid(getSrid());
146 facet.setData(getDataString()); 147 facet.setData(getDataString());
147 facet.setFilter(getFilter()); 148 facet.setFilter(getFilter());
148 facet.setGeometryType(getGeometryType()); 149 facet.setGeometryType(getGeometryType());
149 facet.setConnection(getConnection()); 150 facet.setConnection(MapUtils.getConnection());
150 facet.setConnectionType(getConnectionType()); 151 facet.setConnectionType(MapUtils.getConnectionType());
151 facet.setLabelItem(getLabelItem()); 152 facet.setLabelItem(getLabelItem());
152 153
153 facets.add(facet); 154 facets.add(facet);
154 155
155 return null; 156 return null;
156 }
157
158 /**
159 * This method returns a connection string for databases used by
160 * Mapserver's Mapfile.
161 *
162 * @return A connection string for Mapserver.
163 */
164 protected String getConnection() {
165 SessionFactoryImpl sf = (SessionFactoryImpl)
166 SessionFactoryProvider.getSessionFactory();
167
168 String user = SessionFactoryProvider.getUser(sf);
169 String pass = SessionFactoryProvider.getPass(sf);
170 String url = SessionFactoryProvider.getURL(sf);
171
172 logger.debug("Parse connection url: " + url);
173
174 Matcher m = DB_URL_PATTERN.matcher(url);
175 if (!m.matches()) {
176 logger.warn("Could not parse Connection string." +
177 "Try to parse PostgreSQL string.");
178 // maybe this is a PostgreSQL connection...
179 return getPostgreSQLConnection();
180 }
181
182 logger.debug("Groups for connection string: " + m.groupCount());
183 int groups = m.groupCount();
184
185 for (int i = 0; i <= groups; i++) {
186 logger.debug("Group " + i + ": " + m.group(i));
187 }
188
189 String connection = null;
190
191 if (FLYSUtils.isUsingOracle()) {
192 if (groups < 3) {
193 logger.warn("Could only partially parse connection string.");
194 return null;
195 }
196
197 String host = m.group(2);
198 String port = m.group(3);
199
200 connection = user + "/" + pass + "@" + host;
201 }
202 else {
203 if (groups < 4) {
204 logger.warn("Could only partially parse connection string.");
205 return null;
206 }
207
208 String host = m.group(2);
209 String port = m.group(3);
210 String db = m.group(4);
211
212 StringBuilder sb = new StringBuilder();
213 sb.append("dbname=" + db);
214 sb.append("host='" + host + "'");
215 sb.append("port=" + port);
216 sb.append("password='" + pass + "'");
217 sb.append("sslmode=disable");
218
219 connection = sb.toString();
220 }
221
222 logger.debug("Created connection: '" + connection + "'");
223
224 return connection;
225 }
226
227 protected String getPostgreSQLConnection() {
228 SessionFactoryImpl sf = (SessionFactoryImpl)
229 SessionFactoryProvider.getSessionFactory();
230
231 String user = SessionFactoryProvider.getUser(sf);
232 String pass = SessionFactoryProvider.getPass(sf);
233 String url = SessionFactoryProvider.getURL(sf);
234
235 Matcher m = DB_PSQL_URL_PATTERN.matcher(url);
236 if (!m.matches()) {
237 logger.warn("Could not parse PostgreSQL Connection string.");
238 return null;
239 }
240
241 int groups = m.groupCount();
242 logger.debug("Groups for PostgreSQL connection string: " + groups);
243
244 if (logger.isDebugEnabled()) {
245 for (int i = 0; i <= groups; i++) {
246 logger.debug("Group " + i + ": " + m.group(i));
247 }
248 }
249
250 String connection = null;
251
252 if (groups < 4) {
253 logger.warn("Could only partially parse connection string.");
254 return null;
255 }
256
257 String host = m.group(2);
258 String port = m.group(3);
259 String db = m.group(4);
260
261 StringBuilder sb = new StringBuilder();
262 sb.append("dbname=" + db);
263 sb.append(" host='" + host + "'");
264 sb.append(" port=" + port);
265 sb.append(" user=" + user);
266 sb.append(" password='" + pass + "'");
267 sb.append(" sslmode=disable");
268
269 connection = sb.toString();
270
271 logger.debug("Created connection: '" + connection + "'");
272
273 return connection;
274 }
275
276 protected String getConnectionType() {
277 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
278 } 157 }
279 158
280 protected String getLabelItem() { 159 protected String getLabelItem() {
281 return null; 160 return null;
282 } 161 }

http://dive4elements.wald.intevation.org