rrenkert@4841: package de.intevation.flys.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: 
rrenkert@4841: import de.intevation.flys.backend.SessionFactoryProvider;
rrenkert@4841: 
rrenkert@4841: 
rrenkert@4841: public class MapUtils
rrenkert@4841: {
rrenkert@4841:     private static final Logger logger = 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: 
rrenkert@4841:         logger.debug("Parse connection url: " + url);
rrenkert@4841: 
rrenkert@4841:         Matcher m = DB_URL_PATTERN.matcher(url);
rrenkert@4841:         if (!m.matches()) {
rrenkert@4841:             logger.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: 
rrenkert@4841:         logger.debug("Groups for connection string: " + m.groupCount());
rrenkert@4841:         int groups = m.groupCount();
rrenkert@4841: 
teichmann@5147: 
teichmann@5147:         if (logger.isDebugEnabled()) {
teichmann@5147:             for (int i = 0; i <= groups; i++) {
teichmann@5147:                 logger.debug("Group " + i + ": " + m.group(i));
teichmann@5147:             }
rrenkert@4841:         }
rrenkert@4841: 
rrenkert@4841:         String connection = null;
rrenkert@4841: 
rrenkert@4841:         if (FLYSUtils.isUsingOracle()) {
aheinecke@5270:             if (groups < 4) {
rrenkert@4841:                 logger.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@5270:             connection = user + "/" + pass + "@" + host + "/" + backend;
rrenkert@4841:         }
rrenkert@4841:         else {
rrenkert@4841:             if (groups < 4) {
rrenkert@4841:                 logger.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()) {
rrenkert@4841:             logger.warn("Could not parse PostgreSQL Connection string.");
rrenkert@4841:             return null;
rrenkert@4841:         }
rrenkert@4841: 
rrenkert@4841:         int groups = m.groupCount();
rrenkert@4841:         logger.debug("Groups for PostgreSQL connection string: " + groups);
rrenkert@4841: 
rrenkert@4841:         if (logger.isDebugEnabled()) {
rrenkert@4841:             for (int i = 0; i <= groups; i++) {
rrenkert@4841:                 logger.debug("Group " + i + ": " + m.group(i));
rrenkert@4841:             }
rrenkert@4841:         }
rrenkert@4841: 
rrenkert@4841:         String connection = null;
rrenkert@4841: 
rrenkert@4841:         if (groups < 4) {
rrenkert@4841:             logger.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:         logger.debug("Created connection: '" + connection + "'");
rrenkert@4841: 
rrenkert@4841:         return connection;
rrenkert@4841:     }
rrenkert@4841: 
rrenkert@4841:     public static String getConnectionType() {
rrenkert@4841:         return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
rrenkert@4841:     }
rrenkert@4841: }