view artifacts/src/main/java/org/dive4elements/river/utils/MapUtils.java @ 8755:30b1ddadf275

(issue1801) Unify reference gauge finding code The basic way as described in the method comment of the determineRefGauge method is now used in the WINFOArtifact, MainValuesService and RiverUtils.getGauge method. RiverUtils.getGauge previously just returned the first gauge found. While this is now a behavior change I believe that it is always more correct then the undeterministic behavior of the previous implmenentation.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 24 Jun 2015 14:07:26 +0200
parents e4606eae8ea5
children 5e38e2924c07
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.hibernate.impl.SessionFactoryImpl;

import org.dive4elements.river.backend.SessionFactoryProvider;


public class MapUtils
{
    private static final Logger log = Logger.getLogger(MapUtils.class);

    public static final Pattern DB_URL_PATTERN =
        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([\\.a-zA-Z0-9_-]+)");

    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);

        log.debug("Parse connection url: " + url);

        Matcher m = DB_URL_PATTERN.matcher(url);
        if (!m.matches()) {
            log.warn("Could not parse Connection string." +
                "Try to parse PostgreSQL string.");
            // maybe this is a PostgreSQL connection...
            return getPostgreSQLConnection();
        }

        log.debug("Groups for connection string: " + m.groupCount());
        int groups = m.groupCount();


        if (log.isDebugEnabled()) {
            for (int i = 0; i <= groups; i++) {
                log.debug("Group " + i + ": " + m.group(i));
            }
        }

        String connection = null;

        if (RiverUtils.isUsingOracle()) {
            if (groups < 4) {
                log.warn("Could only partially parse connection string.");
                return null;
            }

            String host = m.group(2);
            String port = m.group(3);
            String backend = m.group(4);
            connection = user + "/" + pass + "@" + host + ":" + port + "/" + backend;
        }
        else {
            if (groups < 4) {
                log.warn("Could only partially parse connection string.");
                return null;
            }

            String host = m.group(2);
            String port = m.group(3);
            String db   = m.group(4);

            connection = createConnectionString(user, pass, host, db, port);
        }

        return connection;
    }

    public static String createConnectionString(
        String user,
        String pass,
        String host,
        String db,
        String port
    ) {
        StringBuilder sb = new StringBuilder();
        sb.append("dbname=").append(db);
        sb.append(" host='").append(host).append("'");
        sb.append(" user=").append(user);
        sb.append(" port=").append(port);
        // XXX: We need to escape this somehow.
        sb.append(" password='").append(pass).append("'");
        sb.append(" sslmode=disable");
        return sb.toString();
    }

    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()) {
            log.warn("Could not parse PostgreSQL Connection string.");
            return null;
        }

        int groups = m.groupCount();
        log.debug("Groups for PostgreSQL connection string: " + groups);

        if (log.isDebugEnabled()) {
            for (int i = 0; i <= groups; i++) {
                log.debug("Group " + i + ": " + m.group(i));
            }
        }

        String connection = null;

        if (groups < 4) {
            log.warn("Could only partially parse connection string.");
            return null;
        }

        String host = m.group(2);
        String port = m.group(3);
        String db   = m.group(4);

        connection = createConnectionString(user, pass, host, db, port);

        log.debug("Created connection: '" + connection + "'");

        return connection;
    }

    public static String getConnectionType() {
        return RiverUtils.isUsingOracle() ? "oraclespatial" : "postgis";
    }
}

http://dive4elements.wald.intevation.org