依靠下面的工具类来完成:
]
package cn.com.mx.gome.suggest.util.envload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 加载tomcat目录下的文件 实现每台机器的个性化参数配置
* @author songqinghu
*/
public class EnvConfLoaderUtils {
private static Logger logger = LoggerFactory.getLogger(EnvConfLoaderUtils.class);
//设置此工具类为单例模式
private static EnvConfLoaderUtils envconf =null ;
private EnvConfLoaderUtils(){
this.EnvConfLoaderUtils(null,null);//调用可配置构造
}
public static EnvConfLoaderUtils getInstance(){
if(envconf ==null){
envconf = new EnvConfLoaderUtils();
}
return envconf;
}
//加载tomcat的配置文件部分
private final static String ENV_CONF_DIR ="envpath";//tomcat根目录下的目录名称
private final static String ENV_CONF_FILE = "env.properties";//个性化机器参数的文件
//根据不同系统 tomcat位置来决定配置目录的位置
private static String ENV_FILE_DIR = System.getProperty("catalina.base")+File.separator+ENV_CONF_DIR;
private Map<String, String> envProps = Collections.emptyMap();//读取配置文件产生的键值对列表
private File envFile = null;//具体的配置文件
//指定的构造 用于通过系统变量动态设置环境变量的参数---system.getproperties
private void EnvConfLoaderUtils(String path, String fileName) {
if(path==null)
path = ENV_FILE_DIR;
if(fileName ==null)
fileName = ENV_CONF_FILE;
if(System.getProperty("com.gomeplus.envpath")!=null){
//如果需要进行环境变量的变更可以通过设置环境变来指定一个目录
path = System.getProperty("com.gomeplus.envpath").trim();
}
//设置新文件
envFile = new File(path+File.separator+fileName);
reload();
}
//加载配置文件到集合中
private void reload() {
//判断文件是否存在
if(!envFile.exists()){
logger.info("conf {} dosen't exist,trying to create one", envFile);
//判断父目录存在吗
if(!envFile.getParentFile().exists()){
//不存在先创建一个
envFile.getParentFile().mkdir();
try {
envFile.createNewFile();//创建一个新的文件
} catch (IOException e) {
logger.info("new file is error :{}",e);
}
}
}
//判断envFile是不是文件 不是就抛出异常
if(!envFile.isFile()){
throw new RuntimeException("the env.properties is not file");
}
//进行文件的读取 propertis
logger.info("start loading env properties :"+envFile);
Properties p ;
try(InputStream is = new FileInputStream(envFile)){
p = new Properties();
p.load(is);
HashMap<String, String> envProps = new HashMap<String,String>();
Enumeration<String> names = (Enumeration<String>) p.propertyNames();
while(names.hasMoreElements()){
String key = names.nextElement();
String value = p.getProperty(key);
envProps.put(key, value);
}
this.envProps = envProps;
}catch (Exception e) {
logger.error("",e);
}
}
/**
*
* @描述:获取指定的配置信息 指定是否重新加载文件
* @param key
* @param reload
* @return
* @return String
* @exception
* @createTime:2016年5月10日
* @author: songqinghu
*/
public String getValue(String key,boolean reload){
if(reload){
reload();
}
return envProps.get(key);
}
/**
*
* @描述:获取配置文件读取的信息集合
* @return
* @return Map<String,String>
* @exception
* @createTime:2016年5月10日
* @author: songqinghu
*/
public Map<String,String> getEnvProps(){
return this.envProps;
}
}