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

com.ajjpj.asysmon.measure.jdbc.ASysMonJdbcDriver Maven / Gradle / Ivy

There is a newer version: 1.0-pre28
Show newest version
package com.ajjpj.asysmon.measure.jdbc;


import com.ajjpj.asysmon.ASysMon;
import com.ajjpj.asysmon.ASysMonApi;
import com.ajjpj.asysmon.measure.ASysMonSource;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;


/**
 * @author arno
 */
public class ASysMonJdbcDriver implements Driver {
    public static final String URL_PREFIX = "asysmon:";

    public static final String PARAM_CONNECTIONPOOL_IDENTIFIER = "qualifier";
    public static final String PARAM_SYSMON_SOURCE = "sysmon-source";

    public static final ASysMonJdbcDriver INSTANCE = new ASysMonJdbcDriver();

    static {
        try {
            DriverManager.registerDriver(INSTANCE);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void deregister() throws SQLException {
        DriverManager.deregisterDriver(INSTANCE);
    }

    @Override public Connection connect(String url, Properties info) throws SQLException {
        if(! acceptsURL(url)) {
            return null;
        }

        final String withoutPrefix = url.substring(URL_PREFIX.length());
        final int idxColon = withoutPrefix.indexOf(':');
        if(idxColon == -1) {
            return null;
        }
        final String paramString = withoutPrefix.substring(0, idxColon);
        final Map params = parseParams(paramString);

        final String innerUrl = withoutPrefix.substring(idxColon+1);
        final Connection inner = DriverManager.getConnection(innerUrl, info);

        final ASysMonApi sysMon = getSysMon(params);

        if(sysMon.getConfig().isGloballyDisabled()) {
            return inner;
        }

        return new ASysMonConnection(inner, sysMon, getPoolIdentifier(params), AConnectionCounter.INSTANCE); //TODO make instance management configurable
    }

    private String getPoolIdentifier(Map params) {
        return params.get(PARAM_CONNECTIONPOOL_IDENTIFIER);
    }

    private ASysMonApi getSysMon(Map params) throws SQLException {
        final String sysmonSourceName = params.get(PARAM_SYSMON_SOURCE);
        if(sysmonSourceName == null) {
            return ASysMon.get();
        }

        try {
            final ASysMonSource sysMonSource = (ASysMonSource) Class.forName(sysmonSourceName).newInstance();
            return sysMonSource.getSysMon();
        } catch (Exception exc) {
            throw new SQLException("error retrieving ASysMon instance", exc);
        }
    }

    private Map parseParams(String paramString) {
        final Map result = new HashMap();

        if(paramString.trim().isEmpty()) {
            return result;
        }

        for(String part: paramString.split(";")) {
            final String[] keyValue = part.split("=");
            if(keyValue.length != 2) {
                throw new IllegalArgumentException("key/value pairs with '=', ';' between pairs");
            }
            result.put(keyValue[0].toLowerCase().trim(), keyValue[1].trim());
        }
        return result;
    }

    @Override public boolean acceptsURL(String url) throws SQLException {
        return url != null && url.startsWith(URL_PREFIX);
    }

    @Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        return new DriverPropertyInfo[0];  //To change body of implemented methods use File | Settings | File Templates.
    }

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

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

    @Override public boolean jdbcCompliant() {
        return true; //TODO what to return here?!
    }

    // introduced with JDK 1.7
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy