Search in sources :

Example 6 with Setting

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

the class SettingTest method settingTestForAbsPath.

@Test
@Ignore
public void settingTestForAbsPath() {
    Setting setting = new Setting("d:/test.setting", true);
    String driver = setting.getByGroup("driver", "demo");
    Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
    String user = setting.getByGroup("user", "demo");
    Assert.assertEquals(user, "rootcom.mysql.jdbc.Driver");
}
Also used : Setting(cn.hutool.setting.Setting) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with Setting

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

the class Profile method getSetting.

// -------------------------------------------------------------------------------- Constructor end
/**
 * 获取当前环境下的配置文件
 *
 * @param name 文件名,如果没有扩展名,默认为.setting
 * @return 当前环境下配置文件
 */
public Setting getSetting(String name) {
    String nameForProfile = fixNameForProfile(name);
    Setting setting = settingMap.get(nameForProfile);
    if (null == setting) {
        setting = new Setting(nameForProfile, this.charset, this.useVar);
        settingMap.put(nameForProfile, setting);
    }
    return setting;
}
Also used : Setting(cn.hutool.setting.Setting)

Example 8 with Setting

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

the class MongoDS method initCloud.

/**
 * 初始化集群<br>
 * 集群的其它客户端设定参数使用全局设定<br>
 * 集群中每一个实例成员用一个group表示,例如:
 *
 * <pre>
 * user = test1
 * pass = 123456
 * database = test
 * [db0]
 * host = 192.168.1.1:27117
 * [db1]
 * host = 192.168.1.1:27118
 * [db2]
 * host = 192.168.1.1:27119
 * </pre>
 */
public synchronized void initCloud() {
    if (groups == null || groups.length == 0) {
        throw new DbRuntimeException("Please give replication set groups!");
    }
    if (setting == null) {
        // 若未指定配置文件,则使用默认配置文件
        setting = new Setting(MONGO_CONFIG_PATH, true);
    }
    final List<ServerAddress> addrList = new ArrayList<>();
    for (String group : groups) {
        addrList.add(createServerAddress(group));
    }
    final MongoCredential credentail = createCredentail(StrUtil.EMPTY);
    try {
        if (null == credentail) {
            mongo = new MongoClient(addrList, buildMongoClientOptions(StrUtil.EMPTY));
        } else {
            mongo = new MongoClient(addrList, credentail, buildMongoClientOptions(StrUtil.EMPTY));
        }
    } catch (Exception e) {
        log.error(e, "Init MongoDB connection error!");
        return;
    }
    log.info("Init MongoDB cloud Set pool with connection to {}", addrList);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) Setting(cn.hutool.setting.Setting) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) DbRuntimeException(cn.hutool.db.DbRuntimeException) DbRuntimeException(cn.hutool.db.DbRuntimeException) NotInitedException(cn.hutool.core.exceptions.NotInitedException)

Example 9 with Setting

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

the class MongoDS method createServerAddress.

// --------------------------------------------------------------------------- Private method start
/**
 * 创建ServerAddress对象,会读取配置文件中的相关信息
 *
 * @param group 分组,如果为{@code null}或者""默认为无分组
 * @return ServerAddress
 */
private ServerAddress createServerAddress(String group) {
    final Setting setting = checkSetting();
    if (group == null) {
        group = StrUtil.EMPTY;
    }
    final String tmpHost = setting.getByGroup("host", group);
    if (StrUtil.isBlank(tmpHost)) {
        throw new NotInitedException("Host name is empy of group: {}", group);
    }
    final int defaultPort = setting.getInt("port", group, 27017);
    return new ServerAddress(NetUtil.buildInetSocketAddress(tmpHost, defaultPort));
}
Also used : NotInitedException(cn.hutool.core.exceptions.NotInitedException) Setting(cn.hutool.setting.Setting) ServerAddress(com.mongodb.ServerAddress)

Example 10 with Setting

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

the class MongoDS method createCredentail.

/**
 * 创建{@link MongoCredential},用于服务端验证<br>
 * 此方法会首先读取指定分组下的属性,用户没有定义则读取空分组下的属性
 *
 * @param group 分组
 * @return {@link MongoCredential},如果用户未指定用户名密码返回null
 * @since 4.1.20
 */
private MongoCredential createCredentail(String group) {
    final Setting setting = this.setting;
    if (null == setting) {
        return null;
    }
    final String user = setting.getStr("user", group, setting.getStr("user"));
    final String pass = setting.getStr("pass", group, setting.getStr("pass"));
    final String database = setting.getStr("database", group, setting.getStr("database"));
    return createCredentail(user, database, pass);
}
Also used : Setting(cn.hutool.setting.Setting)

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