<commit_before>package io.sniffy.sql;

import io.sniffy.Constants;
import io.sniffy.Sniffy;
import io.sniffy.configuration.SniffyConfiguration;
import io.sniffy.registry.ConnectionsRegistry;
import io.sniffy.util.ExceptionUtil;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.*;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Logger;

import static io.sniffy.registry.ConnectionsRegistry.ConnectionStatus.CLOSED;

/**
 * Enable JDBC Sniffer by adding a {@code sniffy:} prefix to your JDBC URL.
 * For example:
 * {@code sniffy:jdbc:h2:mem:}
 *
 * After that you'll be able to verify the number of executed statements using the {@link Sniffy} class
 * @see Sniffy
 * @since 3.1
 */
public class SniffyDriver implements Driver, Constants {

    private static final SniffyDriver INSTANCE = new SniffyDriver();

    private final static Method CONNECT_METHOD;

    static {
        Method getConnectionMethod;
        try {
            getConnectionMethod = Driver.class.getMethod("connect", String.class, Properties.class);
        } catch (NoSuchMethodException e) {
            getConnectionMethod = null;
        }

        CONNECT_METHOD = getConnectionMethod;
    }

    static {
        load();
    }

    private static void load() {
        try {
            DriverManager.registerDriver(INSTANCE);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Sniffy.initialize();

    }

    protected static void checkConnectionAllowed(Connection connection, String url, String userName) throws SQLException {
        if (CLOSED == ConnectionsRegistry.INSTANCE.resolveDataSourceStatus(url, userName)) {
            try {
                connection.close();
            } catch (Exception e) {
                //
            }
            throw new SQLException(String.format("Connection to %s (%s) refused by Sniffy", url, userName));
        }
    }

    protected static void checkConnectionAllowed(String url, String userName) throws SQLException {
        if (CLOSED == ConnectionsRegistry.INSTANCE.resolveDataSourceStatus(url, userName)) {
            throw new SQLException(String.format("Connection to %s (%s) refused by Sniffy", url, userName));
        }
    }

    public Connection connect(String url, Properties info) throws SQLException {

        if (null == url || !acceptsURL(url)) return null;

        String originUrl = extractOriginUrl(url);
        String userName = info.getProperty("user");

        checkConnectionAllowed(originUrl, userName);

        Driver originDriver;
        try {
            originDriver = DriverManager.getDriver(originUrl);
        } catch (SQLException e) {
            try {
                reloadServiceProviders();
                originDriver = DriverManager.getDriver(originUrl);
            } catch (Exception e2) {
                throw e;
            }
        }

        if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info);

        long start = System.currentTimeMillis();
        try {
            Sniffy.enterJdbcMethod();
            Connection delegateConnection = originDriver.connect(originUrl, info);
            return Connection.class.cast(Proxy.newProxyInstance(
                    SniffyDriver.class.getClassLoader(),
                    new Class[]{Connection.class},
                    new ConnectionInvocationHandler(delegateConnection, originUrl, userName)
            ));
        } finally {
            Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start);
        }<commit_msg>Fix getting stacktrace from SniffyDriver.connect(String, Properties) call<commit_after>package io.sniffy.sql;

import io.sniffy.Constants;
import io.sniffy.Sniffy;
import io.sniffy.configuration.SniffyConfiguration;
import io.sniffy.registry.ConnectionsRegistry;
import io.sniffy.util.ExceptionUtil;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.*;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Logger;

import static io.sniffy.registry.ConnectionsRegistry.ConnectionStatus.CLOSED;

/**
 * Enable JDBC Sniffer by adding a {@code sniffy:} prefix to your JDBC URL.
 * For example:
 * {@code sniffy:jdbc:h2:mem:}
 *
 * After that you'll be able to verify the number of executed statements using the {@link Sniffy} class
 * @see Sniffy
 * @since 3.1
 */
public class SniffyDriver implements Driver, Constants {

    private static final SniffyDriver INSTANCE = new SniffyDriver();

    private final static Method CONNECT_METHOD;
    private final static Method CONNECT_METHOD_IMPL;

    static {
        Method getConnectionMethod;
        try {
            getConnectionMethod = Driver.class.getMethod("connect", String.class, Properties.class);
        } catch (NoSuchMethodException e) {
            getConnectionMethod = null;
        }

        CONNECT_METHOD = getConnectionMethod;

        Method getConnectionMethodImpl;
        try {
            getConnectionMethodImpl = SniffyDriver.class.getMethod("connect", String.class, Properties.class);
        } catch (NoSuchMethodException e) {
            getConnectionMethodImpl = null;
        }

        CONNECT_METHOD_IMPL = getConnectionMethodImpl;
    }

    static {
        load();
    }

    private static void load() {
        try {
            DriverManager.registerDriver(INSTANCE);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Sniffy.initialize();

    }

    protected static void checkConnectionAllowed(Connection connection, String url, String userName) throws SQLException {
        if (CLOSED == ConnectionsRegistry.INSTANCE.resolveDataSourceStatus(url, userName)) {
            try {
                connection.close();
            } catch (Exception e) {
                //
            }
            throw new SQLException(String.format("Connection to %s (%s) refused by Sniffy", url, userName));
        }
    }

    protected static void checkConnectionAllowed(String url, String userName) throws SQLException {
        if (CLOSED == ConnectionsRegistry.INSTANCE.resolveDataSourceStatus(url, userName)) {
            throw new SQLException(String.format("Connection to %s (%s) refused by Sniffy", url, userName));
        }
    }

    public Connection connect(String url, Properties info) throws SQLException {

        if (null == url || !acceptsURL(url)) return null;

        String originUrl = extractOriginUrl(url);
        String userName = info.getProperty("user");

        checkConnectionAllowed(originUrl, userName);

        Driver originDriver;
        try {
            originDriver = DriverManager.getDriver(originUrl);
        } catch (SQLException e) {
            try {
                reloadServiceProviders();
                originDriver = DriverManager.getDriver(originUrl);
            } catch (Exception e2) {
                throw e;
            }
        }

        if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return originDriver.connect(originUrl, info);

        long start = System.currentTimeMillis();
        try {
            Sniffy.enterJdbcMethod();
            Connection delegateConnection = originDriver.connect(originUrl, info);
            return Connection.class.cast(Proxy.newProxyInstance(
                    SniffyDriver.class.getClassLoader(),
                    new Class[]{Connection.class},
                    new ConnectionInvocationHandler(delegateConnection, originUrl, userName)
            ));
        } finally {
            Sniffy.exitJdbcMethod(CONNECT_METHOD, System.currentTimeMillis() - start, CONNECT_METHOD_IMPL);
        }
{'fraction_non_alphanumeric': 0.09046177138531415, 'fraction_numerical': 0.0010093363613424174, 'mean_word_length': 2.637907296925195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538163', 'n_tokens_mistral': 2076, 'n_tokens_neox': 2037, 'n_words': 502}