All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.h2gis.utilities.JDBCUrlParser Maven / Gradle / Ivy

Go to download

Collection of methods to fetch spatial metadata in SFS database like PostGIS or H2Spatial. Theses functions can be commonly used either in PostGIS or in H2. Spatial utilities publish also a DataSourceFactory wrapper that provide JDBC Wrapper for spatial functionality.

There is a newer version: 1.2.4
Show newest version
/*
 * h2spatial is a library that brings spatial support to the H2 Java database.
 *
 * h2spatial is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute  CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * h2patial is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * h2spatial is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * h2spatial. If not, see .
 *
 * For more information, please consult: 
 * or contact directly:
 * info_at_ orbisgis.org
 */
package org.h2gis.utilities;

import org.osgi.service.jdbc.DataSourceFactory;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Map;
import java.util.Properties;

/**
 * Convert JDBC URL into JDBC Connection properties.
 * Currently work with H2 and PostGreSQL DataSourceFactory
 * @author Nicolas Fortin
 */
public class JDBCUrlParser {
    private JDBCUrlParser() {}
    private static final String URL_STARTS = "jdbc:";
    /** If the property value of {@link DataSourceFactory#OSGI_JDBC_DRIVER_NAME} ends with SPATIAL_DATASOURCE_ENDSWITH
     *  then it is the wrapped spatial version of a DataSourceFactory  */
    public static final String SPATIAL_DATASOURCE_ENDSWITH = "_spatial";

    /**
     * Convert JDBC URL into JDBC Connection properties
     * @param jdbcUrl JDBC connection path
     * @return Properties to be used in OSGi DataSourceFactory that does not handle {@link DataSourceFactory#JDBC_URL}
     * @throws IllegalArgumentException Argument is not a valid JDBC connection URL or cannot be parsed by this class
     */
    public static Properties parse(String jdbcUrl) throws IllegalArgumentException {
        if(!jdbcUrl.startsWith(URL_STARTS)) {
            throw new IllegalArgumentException("JDBC Url must start with "+URL_STARTS);
        }
        String driverAndURI = jdbcUrl.substring(URL_STARTS.length());
        String driver = driverAndURI.substring(0,driverAndURI.indexOf(':'));
        Properties properties = new Properties();
        URI uri = URI.create(driverAndURI.substring(driverAndURI.indexOf(':')+1));
        if(uri.getHost()!=null) {
            properties.setProperty(DataSourceFactory.JDBC_SERVER_NAME,uri.getHost());
        }
        // Read DataBase name/path and options
        String path = uri.getPath();
        if(path!=null) {
            String[] paths = path.split(";");
            if(uri.getHost()!=null && paths[0].startsWith("/")) {
                paths[0] = paths[0].substring(1);
            }
            properties.setProperty(DataSourceFactory.JDBC_DATABASE_NAME,paths[0]);
            for(int id=1;id entry : URIUtility.getQueryKeyValuePairs(uri).entrySet()) {
                    properties.setProperty(entry.getKey(),entry.getValue());
                }
            } catch (UnsupportedEncodingException ex) {
                throw new IllegalArgumentException("JDBC Url encoding error",ex);

            }
        }
        if(uri.getPort()!=-1) {
            properties.setProperty(DataSourceFactory.JDBC_PORT_NUMBER,String.valueOf(uri.getPort()));
        }
        return properties;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy