Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/utils/MapUtils.java @ 4952:5a0225ddae2b dami
Importer: Add sectie and strover to boundaries and select more
files for import
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 01 Feb 2013 10:38:26 +0100 |
parents | ad0990a82ab8 |
children | 48b231a02d3a |
line wrap: on
line source
package de.intevation.flys.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.hibernate.impl.SessionFactoryImpl; import de.intevation.flys.backend.SessionFactoryProvider; public class MapUtils { private static final Logger logger = Logger.getLogger(MapUtils.class); public static final Pattern DB_URL_PATTERN = Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)"); public static final Pattern DB_PSQL_URL_PATTERN = Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)"); /** * This method returns a connection string for databases used by * Mapserver's Mapfile. * * @return A connection string for Mapserver. */ public static String getConnection() { SessionFactoryImpl sf = (SessionFactoryImpl) SessionFactoryProvider.getSessionFactory(); String user = SessionFactoryProvider.getUser(sf); String pass = SessionFactoryProvider.getPass(sf); String url = SessionFactoryProvider.getURL(sf); logger.debug("Parse connection url: " + url); Matcher m = DB_URL_PATTERN.matcher(url); if (!m.matches()) { logger.warn("Could not parse Connection string." + "Try to parse PostgreSQL string."); // maybe this is a PostgreSQL connection... return getPostgreSQLConnection(); } logger.debug("Groups for connection string: " + m.groupCount()); int groups = m.groupCount(); for (int i = 0; i <= groups; i++) { logger.debug("Group " + i + ": " + m.group(i)); } String connection = null; if (FLYSUtils.isUsingOracle()) { if (groups < 3) { logger.warn("Could only partially parse connection string."); return null; } String host = m.group(2); String port = m.group(3); connection = user + "/" + pass + "@" + host; } else { if (groups < 4) { logger.warn("Could only partially parse connection string."); return null; } String host = m.group(2); String port = m.group(3); String db = m.group(4); StringBuilder sb = new StringBuilder(); sb.append("dbname=" + db); sb.append("host='" + host + "'"); sb.append("port=" + port); sb.append("password='" + pass + "'"); sb.append("sslmode=disable"); connection = sb.toString(); } return connection; } protected static String getPostgreSQLConnection() { SessionFactoryImpl sf = (SessionFactoryImpl) SessionFactoryProvider.getSessionFactory(); String user = SessionFactoryProvider.getUser(sf); String pass = SessionFactoryProvider.getPass(sf); String url = SessionFactoryProvider.getURL(sf); Matcher m = DB_PSQL_URL_PATTERN.matcher(url); if (!m.matches()) { logger.warn("Could not parse PostgreSQL Connection string."); return null; } int groups = m.groupCount(); logger.debug("Groups for PostgreSQL connection string: " + groups); if (logger.isDebugEnabled()) { for (int i = 0; i <= groups; i++) { logger.debug("Group " + i + ": " + m.group(i)); } } String connection = null; if (groups < 4) { logger.warn("Could only partially parse connection string."); return null; } String host = m.group(2); String port = m.group(3); String db = m.group(4); StringBuilder sb = new StringBuilder(); sb.append("dbname=" + db); sb.append(" host='" + host + "'"); sb.append(" port=" + port); sb.append(" user=" + user); sb.append(" password='" + pass + "'"); sb.append(" sslmode=disable"); connection = sb.toString(); logger.debug("Created connection: '" + connection + "'"); return connection; } public static String getConnectionType() { return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis"; } }