<commit_before>/*
 * The Spring Framework is published under the terms
 * of the Apache Software License.
 */

package org.springframework.jdbc.support;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.ClassLoaderUtils;

/**
 * Factory for creating SQLErrorCodes based on the
 * DatabaseProductName taken from the DatabaseMetaData.
 *
 * <p>Returns SQLErrorCodes populated with vendor codes
 * defined in a configuration file named "sql-error-codes.xml".
 * Reads the default file in this package if not overridden by a file
 * in the root of the classpath (e.g. in the WEB-INF/classes directory).
 *
 * @author <NAME>
 * @author <NAME>
   @version $Id: SQLErrorCodesFactory.java,v 1.2 2003-12-10 02:15:17 trisberg Exp $
 */
public class SQLErrorCodesFactory {

	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Name of custom SQL error codes file, loading from the root
	 * of the class path (e.g. in the WEB-INF/classes directory).
	 */
	public static final String SQL_ERROR_CODE_OVERRIDE_PATH = "sql-error-codes.xml";

	/**
	 * Name of default SQL error code files, loading from the class path.
	 */
	public static final String SQL_ERROR_CODE_DEFAULT_PATH = "org/springframework/jdbc/support/sql-error-codes.xml";

	/**
	* Keep track of this instance so we can return it to classes that request it.
	*/
	private static final SQLErrorCodesFactory instance;

	static {
		instance = new SQLErrorCodesFactory();
	}

	/**
	 * Return singleton instance.
	 */
	public static SQLErrorCodesFactory getInstance() {
		return instance;
	}


	/**
	* Create a Map to hold error codes for all databases defined in the config file.
	*/
	private Map rdbmsErrorCodes;

	/**
	 * Not public to enforce Singleton design pattern.
	 * Would be private except to allow testing via overriding the
	 * loadInputStream() method.
	 * <b>Do not subclass in application code.</b>
	 */
	protected SQLErrorCodesFactory() {
		try {
			InputStream is = loadInputStream(SQL_ERROR_CODE_OVERRIDE_PATH);
			if (is == null) {
				is = loadInputStream(SQL_ERROR_CODE_DEFAULT_PATH);
				if (is == null) {
					throw new BeanDefinitionStoreException("Unable to locate file [" + SQL_ERROR_CODE_DEFAULT_PATH +"]",null);
				}
			}
			ListableBeanFactory bf = new XmlBeanFactory(is);
			String[] rdbmsNames = bf.getBeanDefinitionNames(org.springframework.jdbc.support.SQLErrorCodes.class);
			rdbmsErrorCodes = new HashMap(rdbmsNames.length);

			for (int i = 0; i < rdbmsNames.length; i++) {
				SQLErrorCodes ec = (SQLErrorCodes) bf.getBean(rdbmsNames[i]);
				if (ec.getBadSqlGrammarCodes() == null) {
					ec.setBadSqlGrammarCodes(new String[0]);
				}
				else {
					Arrays.sort(ec.getBadSqlGrammarCodes());
				}
				if (ec.getDataIntegrityViolationCodes() == null) {
					ec.setDataIntegrityViolationCodes(new String[0]);
				}
				else {
					Arrays.sort(ec.getDataIntegrityViolationCodes());
				}
				if (ec.getDatabaseProductName() == null)
					rdbmsErrorCodes.put(rdbmsNames[i], ec);
				else
				rdbmsErrorCodes.put(ec.getDatabaseProductName(), ec);
			}
			logger.warn("SQLErrorCodes loaded " + rdbmsErrorCodes.keySet());
		}
		catch (BeanDefinitionStoreException be) {
			logger.warn("Error loading error codes from config file.  Message = " + be.getMessage());
			rdbmsErrorCodes = new HashMap(0);
		}
	}
	
	/**<commit_msg>Changed some logging statements<commit_after>/*
 * The Spring Framework is published under the terms
 * of the Apache Software License.
 */

package org.springframework.jdbc.support;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.ClassLoaderUtils;

/**
 * Factory for creating SQLErrorCodes based on the
 * DatabaseProductName taken from the DatabaseMetaData.
 *
 * <p>Returns SQLErrorCodes populated with vendor codes
 * defined in a configuration file named "sql-error-codes.xml".
 * Reads the default file in this package if not overridden by a file
 * in the root of the classpath (e.g. in the WEB-INF/classes directory).
 *
 * @author <NAME>
 * @author <NAME>
   @version $Id: SQLErrorCodesFactory.java,v 1.3 2003-12-10 02:19:13 trisberg Exp $
 */
public class SQLErrorCodesFactory {

	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Name of custom SQL error codes file, loading from the root
	 * of the class path (e.g. in the WEB-INF/classes directory).
	 */
	public static final String SQL_ERROR_CODE_OVERRIDE_PATH = "sql-error-codes.xml";

	/**
	 * Name of default SQL error code files, loading from the class path.
	 */
	public static final String SQL_ERROR_CODE_DEFAULT_PATH = "org/springframework/jdbc/support/sql-error-codes.xml";

	/**
	* Keep track of this instance so we can return it to classes that request it.
	*/
	private static final SQLErrorCodesFactory instance;

	static {
		instance = new SQLErrorCodesFactory();
	}

	/**
	 * Return singleton instance.
	 */
	public static SQLErrorCodesFactory getInstance() {
		return instance;
	}


	/**
	* Create a Map to hold error codes for all databases defined in the config file.
	*/
	private Map rdbmsErrorCodes;

	/**
	 * Not public to enforce Singleton design pattern.
	 * Would be private except to allow testing via overriding the
	 * loadInputStream() method.
	 * <b>Do not subclass in application code.</b>
	 */
	protected SQLErrorCodesFactory() {
		try {
			InputStream is = loadInputStream(SQL_ERROR_CODE_OVERRIDE_PATH);
			if (is == null) {
				is = loadInputStream(SQL_ERROR_CODE_DEFAULT_PATH);
				if (is == null) {
					throw new BeanDefinitionStoreException("Unable to locate file [" + SQL_ERROR_CODE_DEFAULT_PATH +"]",null);
				}
			}
			ListableBeanFactory bf = new XmlBeanFactory(is);
			String[] rdbmsNames = bf.getBeanDefinitionNames(org.springframework.jdbc.support.SQLErrorCodes.class);
			rdbmsErrorCodes = new HashMap(rdbmsNames.length);

			for (int i = 0; i < rdbmsNames.length; i++) {
				SQLErrorCodes ec = (SQLErrorCodes) bf.getBean(rdbmsNames[i]);
				if (ec.getBadSqlGrammarCodes() == null) {
					ec.setBadSqlGrammarCodes(new String[0]);
				}
				else {
					Arrays.sort(ec.getBadSqlGrammarCodes());
				}
				if (ec.getDataIntegrityViolationCodes() == null) {
					ec.setDataIntegrityViolationCodes(new String[0]);
				}
				else {
					Arrays.sort(ec.getDataIntegrityViolationCodes());
				}
				if (ec.getDatabaseProductName() == null)
					rdbmsErrorCodes.put(rdbmsNames[i], ec);
				else
				rdbmsErrorCodes.put(ec.getDatabaseProductName(), ec);
			}
			logger.info("SQLErrorCodes loaded " + rdbmsErrorCodes.keySet());
		}
		catch (BeanDefinitionStoreException be) {
			logger.warn("Error loading error codes from config file.  Message = " + be.getMessage());
			rdbmsErrorCodes = new HashMap(0);
		}
	}
	
	/**
{'fraction_non_alphanumeric': 0.10409773479256808, 'fraction_numerical': 0.0050903537795876815, 'mean_word_length': 4.1772068511198945, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '4822774', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2423, 'n_words': 643}