tom@9672: /* Copyright (C) 2011, 2012, 2013, 2020 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: tom@9672: import java.net.URI; tom@9672: import java.net.URISyntaxException; 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: tom@9672: private static final String JDBC_SCHEME = "^jdbc:"; tom@9672: tom@9672: private static final String JDBC_DRV_PATTERN = tom@9672: JDBC_SCHEME + "(postgresql|oracle:(thin|oci)):.*"; 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) { tom@9672: log.info("Parse connection string: " + url); rrenkert@4841: tom@9672: if (!url.matches(JDBC_DRV_PATTERN)) { tom@9672: log.error("Could not parse connection string: " tom@9672: + "Not a JDBC URL with PostgreSQL or Oracle driver"); tom@9670: return null; rrenkert@4841: } rrenkert@4841: tom@9672: URI uri = null; tom@9672: try { tom@9672: // Strip JDBC_SCHEME to let the driver be parsed as scheme tom@9672: uri = new URI(url.replaceFirst(JDBC_SCHEME, "")); rrenkert@4841: } tom@9672: catch (URISyntaxException e) { tom@9672: log.error("Could not parse connection string: " + e.getMessage()); tom@9670: return null; tom@9670: } tom@9670: tom@9672: String drv = uri.getScheme(); tom@9672: log.debug("Driver: " + drv); tom@9670: tom@9672: String connection = null; tom@9672: if (drv.equals("oracle")) { tom@9672: try { tom@9672: // Work-around the extra colon in the driver part of the scheme tom@9672: String con = new URI(uri.getSchemeSpecificPart()) tom@9672: .getSchemeSpecificPart().replaceFirst("^@(//)?", ""); tom@9672: log.debug("Database specifier: " + con); tom@9672: connection = user + "/" + pass + "@" + con; tom@9672: } tom@9672: catch (URISyntaxException e) { tom@9672: log.error("Could not parse Oracle connection string: " tom@9672: + e.getMessage()); tom@9672: return null; tom@9672: } rrenkert@4841: } tom@9672: else { // assume PostgreSQL tom@9672: String host = uri.getHost(); tom@9672: if (host == null && uri.getSchemeSpecificPart().startsWith("//")) { tom@9672: // invalid hostnames (e.g. containing '_') are not parsed! tom@9672: log.error("Could not parse PostgreSQL connection string: " tom@9672: + "invalid host name"); tom@9672: return null; tom@9672: } tom@9672: String db = host == null tom@9672: ? uri.getSchemeSpecificPart() tom@9672: : uri.getPath(); tom@9672: int port = uri.getPort(); teichmann@5147: connection = createConnectionString(user, pass, host, db, port); rrenkert@4841: } rrenkert@4841: rrenkert@4841: return connection; rrenkert@4841: } rrenkert@4841: tom@9672: private static String createConnectionString( teichmann@5147: String user, teichmann@5147: String pass, teichmann@5147: String host, teichmann@5147: String db, tom@9672: int port teichmann@5147: ) { teichmann@5147: StringBuilder sb = new StringBuilder(); tom@9672: // Required parameters tom@9672: // defaults to user name in PostgreSQL JDBC: tom@9672: if (db != null) { tom@9672: db = db.replaceFirst("/", ""); tom@9672: } tom@9672: sb.append("dbname=").append(db == null || db.equals("") ? user : db); teichmann@5147: sb.append(" user=").append(user); tom@9672: tom@9672: // Optional parameters tom@9672: if (host != null) { tom@9672: sb.append(" host='").append(host).append("'"); tom@9672: } tom@9672: if (port != -1) { tom@9672: sb.append(" port=").append(port); tom@9672: } 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: }