<commit_before>/**
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.vibur.dbcp.integration;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.connection.ConnectionProvider;
import org.vibur.dbcp.ViburDBCPDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

/**
 * <p>A connection provider for Hibernate org.vibur.dbcp.integration.
 *
 * <p>To use this connection provider set:<br>
 * <code>hibernate.connection.provider_class&nbsp;org.vibur.dbcp.org.vibur.dbcp.integration.ViburDBCPConnectionProvider</code>
 *
 * <pre>
 * Supported Hibernate properties:<br>
 *   hibernate.connection.driver_class
 *   hibernate.connection.url
 *   hibernate.connection.username
 *   hibernate.connection.password
 *   hibernate.connection.isolation
 *   hibernate.connection.autocommit
 * </pre>
 *
 * All {@link org.vibur.dbcp.ViburDBCPConfig} properties are also supported via using the
 * {@code hibernate.vibur} prefix.
 *
 * @see ConnectionProvider
 *
 * @author <NAME>
 */
public class ViburDBCPConnectionProvider implements ConnectionProvider {

    private static final String VIBUR_PREFIX = "hibernate.vibur.";

    private ViburDBCPDataSource dataSource = null;

    /** {@inheritDoc} */
    @Override
    public void configure(Properties props) throws HibernateException {
        dataSource = new ViburDBCPDataSource(transform(props));
        dataSource.start();
    }

    /** {@inheritDoc} */
    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /** {@inheritDoc} */
    @Override
    public void closeConnection(Connection conn) throws SQLException {
        conn.close();
    }

    /** {@inheritDoc} */
    @Override
    public void close() throws HibernateException {
        if (dataSource != null) {
            dataSource.terminate();
            dataSource = null;
        }
    }

    /** {@inheritDoc} */
    @Override
    public boolean supportsAggressiveRelease() {
        return false;
    }

    private Properties transform(Properties props) {
        Properties result  = new Properties();

        String driverClassName = props.getProperty(Environment.DRIVER);
        if (driverClassName != null)
            result.setProperty("driverClassName", driverClassName);
        String jdbcUrl = props.getProperty(Environment.URL);
        if (jdbcUrl != null)
            result.setProperty("jdbcUrl", jdbcUrl);

        String username = props.getProperty(Environment.USER);
        if (username != null)
            result.setProperty("username", username);
        String password = props.getProperty(Environment.PASS);
        if (password != null)
            result.setProperty("password", password);

        String defaultTransactionIsolationValue = props.getProperty(Environment.ISOLATION);
        if (defaultTransactionIsolationValue != null)
            result.setProperty("defaultTransactionIsolationValue", defaultTransactionIsolationValue);
        String defaultAutoCommit = props.getProperty(Environment.AUTOCOMMIT);
        if (defaultAutoCommit != null)<commit_msg>removing redundant inheritDoc<commit_after>/**
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.vibur.dbcp.integration;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.connection.ConnectionProvider;
import org.vibur.dbcp.ViburDBCPDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

/**
 * <p>A connection provider for Hibernate org.vibur.dbcp.integration.
 *
 * <p>To use this connection provider set:<br>
 * <code>hibernate.connection.provider_class&nbsp;org.vibur.dbcp.org.vibur.dbcp.integration.ViburDBCPConnectionProvider</code>
 *
 * <pre>
 * Supported Hibernate properties:<br>
 *   hibernate.connection.driver_class
 *   hibernate.connection.url
 *   hibernate.connection.username
 *   hibernate.connection.password
 *   hibernate.connection.isolation
 *   hibernate.connection.autocommit
 * </pre>
 *
 * All {@link org.vibur.dbcp.ViburDBCPConfig} properties are also supported via using the
 * {@code hibernate.vibur} prefix.
 *
 * @see ConnectionProvider
 *
 * @author <NAME>
 */
public class ViburDBCPConnectionProvider implements ConnectionProvider {

    private static final String VIBUR_PREFIX = "hibernate.vibur.";

    private ViburDBCPDataSource dataSource = null;

    @Override
    public void configure(Properties props) throws HibernateException {
        dataSource = new ViburDBCPDataSource(transform(props));
        dataSource.start();
    }

    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void closeConnection(Connection conn) throws SQLException {
        conn.close();
    }

    @Override
    public void close() throws HibernateException {
        if (dataSource != null) {
            dataSource.terminate();
            dataSource = null;
        }
    }

    @Override
    public boolean supportsAggressiveRelease() {
        return false;
    }

    private Properties transform(Properties props) {
        Properties result  = new Properties();

        String driverClassName = props.getProperty(Environment.DRIVER);
        if (driverClassName != null)
            result.setProperty("driverClassName", driverClassName);
        String jdbcUrl = props.getProperty(Environment.URL);
        if (jdbcUrl != null)
            result.setProperty("jdbcUrl", jdbcUrl);

        String username = props.getProperty(Environment.USER);
        if (username != null)
            result.setProperty("username", username);
        String password = props.getProperty(Environment.PASS);
        if (password != null)
            result.setProperty("password", password);

        String defaultTransactionIsolationValue = props.getProperty(Environment.ISOLATION);
        if (defaultTransactionIsolationValue != null)
            result.setProperty("defaultTransactionIsolationValue", defaultTransactionIsolationValue);
        String defaultAutoCommit = props.getProperty(Environment.AUTOCOMMIT);
        if (defaultAutoCommit != null)
{'fraction_non_alphanumeric': 0.09562508465393471, 'fraction_numerical': 0.00216714072870107, 'mean_word_length': 3.4805825242718447, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5109015', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1915, 'n_words': 516}