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: /** 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: tom@9671: return getConnection(user, pass, url); tom@9671: } tom@9671: tom@9671: public static String getConnection(String user, String pass, String url) { teichmann@8202: log.debug("Parse connection url: " + url); rrenkert@4841: rrenkert@4841: Matcher m = DB_URL_PATTERN.matcher(url); rrenkert@4841: if (!m.matches()) { tom@9670: log.warn("Could not parse Connection string"); tom@9670: return null; rrenkert@4841: } rrenkert@4841: rrenkert@4841: int groups = m.groupCount(); rrenkert@4841: 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: tom@9670: if (groups < 4) { tom@9670: log.warn("Could only partially parse connection string."); tom@9670: return null; tom@9670: } tom@9670: tom@9670: String host = m.group(2); tom@9670: String port = m.group(3); tom@9670: String db = m.group(4); tom@9670: tom@9671: if (url.startsWith("jdbc:oracle:")) { tom@8856: connection = user + "/" + pass tom@9670: + "@" + host + ":" + port + "/" + db; rrenkert@4841: } rrenkert@4841: else { 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: public static String getConnectionType() { teichmann@5865: return RiverUtils.isUsingOracle() ? "oraclespatial" : "postgis"; rrenkert@4841: } rrenkert@4841: }