org.logicalcobwebs.proxool.configuration.PropertyConfigurator.java

Hibernate框架读取连接池配置信息,并且对连接池配置文件有一些要求

如:配置键值必须是jdbc为前缀..........


package org.logicalcobwebs.proxool.configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolConstants;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * Uses a standard Java properties file to configure Proxool. For example:
 *
 * <pre>
 * jdbc-0.proxool.alias=property-test
 * jdbc-0.proxool.driver-url=jdbc:hsqldb:.
 * jdbc-0.proxool.driver-class=org.hsqldb.jdbcDriver
 * jdbc-0.user=foo
 * jdbc-0.password=bar
 * jdbc-0.proxool.house-keeping-sleep-time=40000
 * jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
 * jdbc-0.proxool.maximum-connection-count=10
 * jdbc-0.proxool.minimum-connection-count=3
 * jdbc-0.proxool.maximum-connection-lifetime=18000000
 * jdbc-0.proxool.simultaneous-build-throttle=5
 * jdbc-0.proxool.recently-started-threshold=40000
 * jdbc-0.proxool.overload-without-refusal-lifetime=50000
 * jdbc-0.proxool.maximum-active-time=60000
 * jdbc-0.proxool.verbose=true
 * jdbc-0.proxool.trace=true
 * jdbc-0.proxool.fatal-sql-exception=Fatal error
 * jdbc-0.proxool.prototype-count=2
 *
 * jdbc-1.proxool.alias=property-test-2
 * jdbc-1.proxool.driver-url=jdbc:hsqldb:.
 * jdbc-1.proxool.driver-class=org.hsqldb.jdbcDriver
 * jdbc-1.user=scott
 * jdbc-1.password=tiger
 * jdbc-1.proxool.house-keeping-sleep-time=40000
 * jdbc-1.proxool.house-keeping-test-sql=select CURRENT_DATE
 * jdbc-1.proxool.maximum-connection-count=10
 * jdbc-1.proxool.minimum-connection-count=3
 * jdbc-1.proxool.maximum-connection-lifetime=18000000
 * jdbc-1.proxool.simultaneous-build-throttle=5
 * jdbc-1.proxool.recently-started-threshold=40000
 * jdbc-1.proxool.overload-without-refusal-lifetime=50000
 * jdbc-1.proxool.maximum-active-time=60000
 * jdbc-1.proxool.verbose=true
 * jdbc-1.proxool.trace=true
 * jdbc-1.proxool.fatal-sql-exception=Fatal error
 * jdbc-1.proxool.prototype-count=2
 * </pre>
 *
 * <p>The first word (up to the first dot) must start with "jdbc", but it can
 * be anything you like. Use unique names to identify each pool. Any property
 * not starting with "jdbc" will be ignored.</p>
 * <p>
 * The properties prefixed with "proxool."  will be used by Proxool while
 * the properties that are not prefixed will be passed on to the
 * delegate JDBC driver.
 * </p>
 *
 * @version $Revision: 1.11 $, $Date: 2006/01/18 14:39:58 $
 * @author Bill Horsman (bill@logicalcobwebs.co.uk)
 * @author $Author: billhorsman $ (current maintainer)
 * @since Proxool 0.5
 */
public class PropertyConfigurator {
    private static final Log LOG = LogFactory.getLog(PropertyConfigurator.class);

    protected static final String PREFIX = "jdbc";

    private static final String DOT = ".";

    private static final String EXAMPLE_FORMAT = PREFIX + "*" + DOT + "*";

    /**
     * Configure proxool with the given properties file.
     * @param filename the filename of the properties file.
     * @throws ProxoolException if the configuration fails.
     */
    public static void configure(String filename) throws ProxoolException {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filename));
        } catch (IOException e) {
            throw new ProxoolException("Couldn't load property file " + filename);
        }
        configure(properties);
    }

    /**
     * Configure proxool with the given properties.
     * @param properties the properties instance to use.
     * @throws ProxoolException if the configuration fails.
     */
    public static void configure(Properties properties) throws ProxoolException {
        final Map propertiesMap = new HashMap();
        final Iterator allPropertyKeysIterator = properties.keySet().iterator();
        Properties proxoolProperties = null;

        while (allPropertyKeysIterator.hasNext()) {
            String key = (String) allPropertyKeysIterator.next();
            String value = properties.getProperty(key);

            if (key.startsWith(PREFIX)) {
                int a = key.indexOf(DOT);
                if (a == -1) {
                    throw new ProxoolException("Property " + key + " must be of the format " + EXAMPLE_FORMAT);
                }
                final String tag = key.substring(0, a);
                final String name = key.substring(a + 1);
                proxoolProperties = (Properties) propertiesMap.get(tag);
                if (proxoolProperties == null) {
                    proxoolProperties = new Properties();
                    propertiesMap.put(tag, proxoolProperties);
                }
                proxoolProperties.put(name, value);
            }
        }

        final Iterator tags = propertiesMap.keySet().iterator();
        while (tags.hasNext()) {
            proxoolProperties = (Properties) propertiesMap.get(tags.next());
            // make sure that required propeties are defined
            // and build the url
            // Check that we have defined the minimum information
            final String driverClass = proxoolProperties.getProperty(ProxoolConstants.DRIVER_CLASS_PROPERTY);
            final String driverUrl = proxoolProperties.getProperty(ProxoolConstants.DRIVER_URL_PROPERTY);
            if (driverClass == null || driverUrl == null) {
                throw new ProxoolException("You must define the " + ProxoolConstants.DRIVER_CLASS_PROPERTY + " and the "
                    + ProxoolConstants.DRIVER_URL_PROPERTY + ".");
            }
            final String alias = proxoolProperties.getProperty(ProxoolConstants.ALIAS_PROPERTY);

            // Build the URL; optionally defining a name
            StringBuffer url = new StringBuffer();
            url.append("proxool");
            if (alias != null) {
                url.append(ProxoolConstants.ALIAS_DELIMITER);
                url.append(alias);
                proxoolProperties.remove(ProxoolConstants.ALIAS_PROPERTY);
            }
            url.append(ProxoolConstants.URL_DELIMITER);
            url.append(driverClass);
            proxoolProperties.remove(ProxoolConstants.DRIVER_CLASS_PROPERTY);
            url.append(ProxoolConstants.URL_DELIMITER);
            url.append(driverUrl);
            proxoolProperties.remove(ProxoolConstants.DRIVER_URL_PROPERTY);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Created url: " + url);
            }

            ProxoolFacade.registerConnectionPool(url.toString(), proxoolProperties);
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值