Search in sources :

Example 11 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class RedisDS method init.

/**
 * 初始化Jedis客户端
 *
 * @param group Redis服务器信息分组
 * @return this
 */
public RedisDS init(String group) {
    if (null == setting) {
        setting = new Setting(REDIS_CONFIG_PATH, true);
    }
    final JedisPoolConfig config = new JedisPoolConfig();
    // 共用配置
    setting.toBean(config);
    if (StrUtil.isNotBlank(group)) {
        // 特有配置
        setting.toBean(group, config);
    }
    this.pool = new JedisPool(config, // 地址
    setting.getStr("host", group, Protocol.DEFAULT_HOST), // 端口
    setting.getInt("port", group, Protocol.DEFAULT_PORT), // 连接超时
    setting.getInt("connectionTimeout", group, setting.getInt("timeout", group, Protocol.DEFAULT_TIMEOUT)), // 读取数据超时
    setting.getInt("soTimeout", group, setting.getInt("timeout", group, Protocol.DEFAULT_TIMEOUT)), // 密码
    setting.getStr("password", group, null), // 数据库序号
    setting.getInt("database", group, Protocol.DEFAULT_DATABASE), // 客户端名
    setting.getStr("clientName", group, "Hutool"), // 是否使用SSL
    setting.getBool("ssl", group, false), // SSL相关,使用默认
    null, null, null);
    return this;
}
Also used : Setting(cn.hutool.setting.Setting) JedisPool(redis.clients.jedis.JedisPool) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Example 12 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class DbSetting method getDbConfig.

/**
 * 获得数据库连接信息
 *
 * @param group 分组
 * @return 分组
 */
public DbConfig getDbConfig(String group) {
    final Setting config = setting.getSetting(group);
    if (MapUtil.isEmpty(config)) {
        throw new DbRuntimeException("No Hutool pool config for group: [{}]", group);
    }
    final DbConfig dbConfig = new DbConfig();
    // 基本信息
    final String url = config.getAndRemoveStr(DSFactory.KEY_ALIAS_URL);
    if (StrUtil.isBlank(url)) {
        throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
    }
    dbConfig.setUrl(url);
    // 自动识别Driver
    final String driver = config.getAndRemoveStr(DSFactory.KEY_ALIAS_DRIVER);
    dbConfig.setDriver(StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url));
    dbConfig.setUser(config.getAndRemoveStr(DSFactory.KEY_ALIAS_USER));
    dbConfig.setPass(config.getAndRemoveStr(DSFactory.KEY_ALIAS_PASSWORD));
    // 连接池相关信息
    dbConfig.setInitialSize(setting.getInt("initialSize", group, 0));
    dbConfig.setMinIdle(setting.getInt("minIdle", group, 0));
    dbConfig.setMaxActive(setting.getInt("maxActive", group, 8));
    dbConfig.setMaxWait(setting.getLong("maxWait", group, 6000L));
    // remarks等特殊配置,since 5.3.8
    String connValue;
    for (String key : DSFactory.KEY_CONN_PROPS) {
        connValue = config.get(key);
        if (StrUtil.isNotBlank(connValue)) {
            dbConfig.addConnProps(key, connValue);
        }
    }
    return dbConfig;
}
Also used : Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 13 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class DbcpDSFactory method createDataSource.

/**
 * 创建数据源
 * @param group 分组
 * @return Dbcp数据源 {@link BasicDataSource}
 */
private BasicDataSource createDataSource(String group) {
    final Setting config = setting.getSetting(group);
    if (CollectionUtil.isEmpty(config)) {
        throw new DbRuntimeException("No DBCP config for group: [{}]", group);
    }
    final BasicDataSource ds = new BasicDataSource();
    // 基本信息
    String url = config.getAndRemoveStr(KEY_ALIAS_URL);
    if (StrUtil.isBlank(url)) {
        throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
    }
    ds.setUrl(url);
    ds.setUsername(config.getAndRemoveStr(KEY_ALIAS_USER));
    ds.setPassword(config.getAndRemoveStr(KEY_ALIAS_PASSWORD));
    final String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
    if (StrUtil.isNotBlank(driver)) {
        ds.setDriverClassName(driver);
    } else {
        ds.setDriverClassName(DbUtil.identifyDriver(ds.getUrl()));
    }
    // 注入属性
    config.toBean(ds);
    return ds;
}
Also used : Setting(cn.hutool.setting.Setting) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 14 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class UploadSetting method load.

/**
 * 加载全局设定
 *
 * @param settingPath 设定文件路径,相对Classpath
 */
public synchronized void load(String settingPath) {
    URL url = URLUtil.getURL(settingPath);
    if (url == null) {
        log.info("Can not find Upload setting file [{}], use default setting.", settingPath);
        return;
    }
    Setting setting = new Setting(url, Setting.DEFAULT_CHARSET, true);
    maxFileSize = setting.getInt("file.size.max");
    memoryThreshold = setting.getInt("memory.threshold");
    tmpUploadPath = setting.getStr("tmp.upload.path");
    fileExts = setting.getStrings("file.exts");
    isAllowFileExts = setting.getBool("file.exts.allow");
}
Also used : Setting(cn.hutool.setting.Setting) URL(java.net.URL)

Example 15 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class AbstractDSFactory method createDataSource.

/**
 * 创建数据源
 *
 * @param group 分组
 * @return {@link DataSourceWrapper} 数据源包装
 */
private DataSourceWrapper createDataSource(String group) {
    if (group == null) {
        group = StrUtil.EMPTY;
    }
    final Setting config = setting.getSetting(group);
    if (MapUtil.isEmpty(config)) {
        throw new DbRuntimeException("No config for group: [{}]", group);
    }
    // 基本信息
    final String url = config.getAndRemoveStr(KEY_ALIAS_URL);
    if (StrUtil.isBlank(url)) {
        throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
    }
    // 移除用户可能误加入的show sql配置项
    // issue#I3VW0R@Gitee
    DbUtil.removeShowSqlParams(config);
    // 自动识别Driver
    String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
    if (StrUtil.isBlank(driver)) {
        driver = DriverUtil.identifyDriver(url);
    }
    final String user = config.getAndRemoveStr(KEY_ALIAS_USER);
    final String pass = config.getAndRemoveStr(KEY_ALIAS_PASSWORD);
    return DataSourceWrapper.wrap(createDataSource(url, driver, user, pass, config), driver);
}
Also used : Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Aggregations

Setting (cn.hutool.setting.Setting)16 DbRuntimeException (cn.hutool.db.DbRuntimeException)8 NotInitedException (cn.hutool.core.exceptions.NotInitedException)3 Test (org.junit.Test)3 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 ServerAddress (com.mongodb.ServerAddress)2 SimpleDataSource (cn.hutool.db.ds.simple.SimpleDataSource)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)1 Ignore (org.junit.Ignore)1 JedisPool (redis.clients.jedis.JedisPool)1 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1