teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.utils; rrenkert@4841: rrenkert@4841: import java.util.regex.Matcher; rrenkert@4841: import java.util.regex.Pattern; rrenkert@4841: rrenkert@4841: import org.apache.log4j.Logger; rrenkert@4841: import org.hibernate.impl.SessionFactoryImpl; rrenkert@4841: teichmann@5831: import org.dive4elements.river.backend.SessionFactoryProvider; rrenkert@4841: rrenkert@4841: rrenkert@4841: public class MapUtils rrenkert@4841: { teichmann@8202: private static final Logger log = Logger.getLogger(MapUtils.class); rrenkert@4841: rrenkert@4841: public static final Pattern DB_URL_PATTERN = aheinecke@5211: Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([\\.a-zA-Z0-9_-]+)"); rrenkert@4841: rrenkert@4841: public static final Pattern DB_PSQL_URL_PATTERN = teichmann@5147: Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9_-]+)"); rrenkert@4841: rrenkert@4841: /** rrenkert@4841: * This method returns a connection string for databases used by rrenkert@4841: * Mapserver's Mapfile. rrenkert@4841: * rrenkert@4841: * @return A connection string for Mapserver. rrenkert@4841: */ rrenkert@4841: public static String getConnection() { rrenkert@4841: SessionFactoryImpl sf = (SessionFactoryImpl) rrenkert@4841: SessionFactoryProvider.getSessionFactory(); rrenkert@4841: rrenkert@4841: String user = SessionFactoryProvider.getUser(sf); rrenkert@4841: String pass = SessionFactoryProvider.getPass(sf); rrenkert@4841: String url = SessionFactoryProvider.getURL(sf); rrenkert@4841: teichmann@8202: log.debug("Parse connection url: " + url); rrenkert@4841: rrenkert@4841: Matcher m = DB_URL_PATTERN.matcher(url); rrenkert@4841: if (!m.matches()) { teichmann@8202: log.warn("Could not parse Connection string." + rrenkert@4841: "Try to parse PostgreSQL string."); rrenkert@4841: // maybe this is a PostgreSQL connection... rrenkert@4841: return getPostgreSQLConnection(); rrenkert@4841: } rrenkert@4841: teichmann@8202: log.debug("Groups for connection string: " + m.groupCount()); rrenkert@4841: int groups = m.groupCount(); rrenkert@4841: teichmann@5147: teichmann@8202: if (log.isDebugEnabled()) { teichmann@5147: for (int i = 0; i <= groups; i++) { teichmann@8202: log.debug("Group " + i + ": " + m.group(i)); teichmann@5147: } rrenkert@4841: } rrenkert@4841: rrenkert@4841: String connection = null; rrenkert@4841: teichmann@5865: if (RiverUtils.isUsingOracle()) { aheinecke@5270: if (groups < 4) { teichmann@8202: log.warn("Could only partially parse connection string."); rrenkert@4841: return null; rrenkert@4841: } rrenkert@4841: rrenkert@4841: String host = m.group(2); rrenkert@4841: String port = m.group(3); aheinecke@5270: String backend = m.group(4); aheinecke@5422: connection = user + "/" + pass + "@" + host + ":" + port + "/" + backend; rrenkert@4841: } rrenkert@4841: else { rrenkert@4841: if (groups < 4) { teichmann@8202: log.warn("Could only partially parse connection string."); rrenkert@4841: return null; rrenkert@4841: } rrenkert@4841: rrenkert@4841: String host = m.group(2); rrenkert@4841: String port = m.group(3); rrenkert@4841: String db = m.group(4); rrenkert@4841: teichmann@5147: connection = createConnectionString(user, pass, host, db, port); rrenkert@4841: } rrenkert@4841: rrenkert@4841: return connection; rrenkert@4841: } rrenkert@4841: teichmann@5147: public static String createConnectionString( teichmann@5147: String user, teichmann@5147: String pass, teichmann@5147: String host, teichmann@5147: String db, teichmann@5147: String port teichmann@5147: ) { teichmann@5147: StringBuilder sb = new StringBuilder(); teichmann@5147: sb.append("dbname=").append(db); teichmann@5147: sb.append(" host='").append(host).append("'"); teichmann@5147: sb.append(" user=").append(user); teichmann@5147: sb.append(" port=").append(port); teichmann@5147: // XXX: We need to escape this somehow. teichmann@5147: sb.append(" password='").append(pass).append("'"); teichmann@5147: sb.append(" sslmode=disable"); teichmann@5147: return sb.toString(); teichmann@5147: } teichmann@5147: rrenkert@4841: protected static String getPostgreSQLConnection() { rrenkert@4841: SessionFactoryImpl sf = (SessionFactoryImpl) rrenkert@4841: SessionFactoryProvider.getSessionFactory(); rrenkert@4841: rrenkert@4841: String user = SessionFactoryProvider.getUser(sf); rrenkert@4841: String pass = SessionFactoryProvider.getPass(sf); rrenkert@4841: String url = SessionFactoryProvider.getURL(sf); rrenkert@4841: rrenkert@4841: Matcher m = DB_PSQL_URL_PATTERN.matcher(url); rrenkert@4841: if (!m.matches()) { teichmann@8202: log.warn("Could not parse PostgreSQL Connection string."); rrenkert@4841: return null; rrenkert@4841: } rrenkert@4841: rrenkert@4841: int groups = m.groupCount(); teichmann@8202: log.debug("Groups for PostgreSQL connection string: " + groups); rrenkert@4841: teichmann@8202: if (log.isDebugEnabled()) { rrenkert@4841: for (int i = 0; i <= groups; i++) { teichmann@8202: log.debug("Group " + i + ": " + m.group(i)); rrenkert@4841: } rrenkert@4841: } rrenkert@4841: rrenkert@4841: String connection = null; rrenkert@4841: rrenkert@4841: if (groups < 4) { teichmann@8202: log.warn("Could only partially parse connection string."); rrenkert@4841: return null; rrenkert@4841: } rrenkert@4841: rrenkert@4841: String host = m.group(2); rrenkert@4841: String port = m.group(3); rrenkert@4841: String db = m.group(4); rrenkert@4841: teichmann@5147: connection = createConnectionString(user, pass, host, db, port); rrenkert@4841: teichmann@8202: log.debug("Created connection: '" + connection + "'"); rrenkert@4841: rrenkert@4841: return connection; rrenkert@4841: } rrenkert@4841: rrenkert@4841: public static String getConnectionType() { teichmann@5865: return RiverUtils.isUsingOracle() ? "oraclespatial" : "postgis"; rrenkert@4841: } rrenkert@4841: }