java load property

package common;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;


import org.apache.commons.logging.LogFactory;


public class MySqlLoader {


public static void main(String[] args) {
String sqlIdString = "aa";
System.out.println(getSql(sqlIdString));
String sqlIdString1 = "bb";
System.out.println(getSql(sqlIdString1));
// String sqlIdString2="cc";
// System.out.println(getSql(sqlIdString2));
String sqlIdString3 = "dd";
System.out.println(getSql(sqlIdString3));
}


private static final String SQLFILE = "common.sql";
private static org.apache.commons.logging.Log log = LogFactory
.getLog(MySqlLoader.class);


// save read property neirong
private static final Hashtable cache = new Hashtable();


public static String getSql(String name) {
final String methodName = "getSql";
log.debug(methodName + "sql read start: sqlId=" + name);


Properties properties = null;
synchronized (cache) {
properties = (Properties) cache.get(SQLFILE);
}
if (properties == null) {
synchronized (cache) {
properties = (Properties) cache.get(SQLFILE);
if (properties == null) {
properties = cacheSQL(SQLFILE);
}
}
}
String valueString = properties.getProperty(name);
// property qu de bu ke
if (valueString == null) {
String msg = "this sqlId" + name + "meiyoudingyi";
log.error(methodName + msg);
throw new IllegalArgumentException(msg);
}
// sqlid ding yi le danshi mei you value zhi
if ("".equals(valueString)) {
String msg = "this sqlId" + name
+ "ding yi le danshi meiyou ding yi Value zhi";
log.error(methodName + msg);
throw new IllegalArgumentException(msg);
}
log.debug(methodName + "sql read zhong liao");
return valueString;


}


private static Properties cacheSQL(String resource)
throws IllegalArgumentException {
final String method = "cacheSQL";
InputStream inputStream = null;
Properties properties = null;
try {
// E:\workspace\WebPj\src\common\sql.properties
String pathName = resource.replace(".", "/") + ".properties";
Object dummy = new MySqlLoader();
ClassLoader loader = dummy.getClass().getClassLoader();
inputStream = loader.getResourceAsStream(pathName);
// in =
// loader.getResourceAsStream("E:\\workspace\\WebPj\\src\\common\\sql.properties");


if (inputStream == null) {
String msg = "zhi ding de property file" + pathName
+ " bu cun zai";
log.error(method + msg);
throw new IllegalArgumentException(msg);
}


inputStream = new BufferedInputStream(inputStream);
properties = new Properties();
properties.load(inputStream);
cache.put(resource, properties);
return properties;
} catch (IOException e) {
String msg = "sql resource" + resource + "read out failed";
log.error(method + msg);
throw new IllegalArgumentException(msg);


} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {


}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值