com.jflyfox.util.Config Maven / Gradle / Ivy
/**
* Copyright 2015-2025 FLY的狐狸(email:[email protected] qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
public class Config {
private final static URL classPathUrl = Config.class.getResource("/");
private final static String classPath = new File(classPathUrl.getFile()).getPath();
private static String configPath = "/conf/";
private static Map configMap = new HashMap();
static {
setConfigMap();
}
public static String getStr(String key) {
if (configMap.size() < 0) {
return null;
}
return configMap.get(key);
}
public static int getToInt(String key) {
String val = getStr(key);
return NumberUtils.parseInt(val);
}
public static long getToLong(String key) {
String val = getStr(key);
return NumberUtils.parseLong(val);
}
public static double getToDbl(String key) {
String val = getStr(key);
return NumberUtils.parseDbl(val);
}
public static boolean getToBoolean(String key) {
String val = getStr(key);
try {
return Boolean.valueOf(val);
} catch (Exception e) {
return false;
}
}
/**
* 修改目录,配置重构
*
* 2016年6月2日 下午3:27:29
* flyfox [email protected]
* @param configPath
*/
public static void rebuild(String configPath){
Config.configPath = configPath;
rebuild();
}
/**
* 配置重构
*
* 2016年6月2日 下午3:27:13
* flyfox [email protected]
*/
public static void rebuild(){
setConfigMap();
}
public static void test() {
for (String key : configMap.keySet()) {
System.out.println(key + "=" + configMap.get(key));
}
}
private static void setConfigMap() {
String filePath = classPath + configPath;
filePath = PathUtils.rebuild(filePath);
List list = findFiles(filePath);
Map tmpConfigMap = new HashMap();
for (String configName : list) {
Properties props = getProperties(filePath + configName);
Iterator> it = props.entrySet().iterator();
while (it.hasNext()) {
Entry