/** * 初始化<br> * 设定文件中的host和端口有三种形式: --------------------- host = host:port --------------------- host = host * port = port --------------------- #此种形式使用MongoDB默认端口 host = host --------------------- */ public synchronized void initSingle() { if (setting == null) { try { setting = new Setting(MONGO_CONFIG_PATH, CharsetUtil.UTF_8, true); } catch (Exception e) { // 在single模式下,可以没有配置文件。 } } String group = StrUtil.EMPTY; if (serverAddress == null) { if (groups != null && groups.length == 1) { group = groups[0]; } serverAddress = createServerAddress(group); } try { mongo = new MongoClient(serverAddress, buildMongoClientOptions(group)); } catch (Exception e) { throw new UtilException( StrUtil.format("Init MongoDB pool with connection to [{}] error!", serverAddress), e); } log.info("Init MongoDB pool with connection to [{}]", serverAddress); }
/** * 根目录 * * @param root 根目录绝对路径 */ public static void setRoot(File root) { if (root.exists() == false) { root.mkdirs(); } else if (root.isDirectory() == false) { throw new ServerSettingException(StrUtil.format("{} is not a directory!", root.getPath())); } ServerSetting.root = root; }
/** * 创建ServerAddress对象,会读取配置文件中的相关信息 * * @param group 分组,如果为null默认为无分组 * @return ServerAddress */ private ServerAddress createServerAddress(String group) { if (setting == null) { throw new UtilException( StrUtil.format( "Please indicate setting file or create default [{}], and define group [{}]", MONGO_CONFIG_PATH, group)); } if (group == null) { group = StrUtil.EMPTY; } String tmpHost = setting.getString("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)); }
public StatefulException(String messageTemplate, Object... params) { super(StrUtil.format(messageTemplate, params)); }
public SettingException(Throwable throwable, String messageTemplate, Object... params) { super(StrUtil.format(messageTemplate, params), throwable); }