SDMDriver.java [src/csip/sdm] Revision:   Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip.sdm;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 *
 * Use the following for your datasource environment:
 *
 * @Resource(type = JDBC, file = "${<config key>}", id = <id string> , env = {
 * "driverClassName=csip.sdm.SDMDriver", "validationQuery=SELECT 1 FROM
 * mapunit;", "maxWait=300000", "testOnBorrow=false", "sdm.logging=INFO",
 * "sdm.timeout=60" })
 */
public class SDMDriver implements Driver {

    static final String URL_TYPE = "jdbc:sdm:rest://";

    /**
     *
     */
    static class DBProperties {

        static final String PROP_KEY_LOGGING = "sdm.logging";
        static final String PROP_DEF_LOGGING = "OFF";

        static final String PROP_KEY_TIMEOUT = "sdm.timeout";
        static final String PROP_DEF_TIMEOUT = "300";

        static final String PROP_KEY_SCHEME = "sdm.scheme";
        static final String PROP_DEF_SCHEME = "https";

        Properties p;


        DBProperties(Properties p) {
            this.p = p;
        }


        String getLogging() {
            return p.getProperty(PROP_KEY_LOGGING, PROP_DEF_LOGGING);
        }


        boolean isLoggingDefault() {
            return getLogging().equalsIgnoreCase(PROP_DEF_LOGGING);
        }


        String getScheme() {
            return p.getProperty(PROP_KEY_SCHEME, PROP_DEF_SCHEME);
        }


        int getTimeout() {
            String timeout = p.getProperty(PROP_KEY_TIMEOUT, PROP_DEF_TIMEOUT);
            int t = 0;
            try {
                t = Integer.parseInt(timeout);
            } catch (NumberFormatException E) {
                t = Integer.parseInt(PROP_DEF_TIMEOUT);
            }
            return Math.max(t, 0);
        }
    }


    static {
        try {
            SDMDriver driverInst = new SDMDriver();
            DriverManager.registerDriver(driverInst);
        } catch (SQLException ex) {
            Logger.getLogger(SDMDriver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public boolean acceptsURL(String url) throws SQLException {
        return url.startsWith(URL_TYPE);
    }


    @Override
    public Connection connect(String url, Properties info) throws SQLException {
        if (acceptsURL(url)) {
            DBProperties props = new DBProperties(info);
            return new SDMConnection(url, props);
        } else {
            throw new SQLException("Invalid connection url specified.  Requires connection of type '" + URL_TYPE + "'");
        }
    }


    @Override
    public int getMajorVersion() {
        return 1;
    }


    @Override
    public int getMinorVersion() {
        return 0;
    }


    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public boolean jdbcCompliant() {
        return true;
    }
}