java load property
来源:互联网 发布:淘宝商品如何推广 编辑:程序博客网 时间:2024/06/03 23:47
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) {
}
}
}
}
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
- java load property
- 使用codeigniter的时候出现 Undefined property: Index::$load问题
- Failed to load property source from location 'classpath:/application.yml'
- Java读写Property
- java 读取property文件
- java System Property
- java 读取property文件
- java操作Property
- java property配置文件
- java读取配置文件property
- java property有乱码
- java property 配置文件用法
- Java读取Property配置文件
- java读取property文件
- java property 配置文件
- java读写property文件
- Java读取Property配置文件
- Java解析Property文件
- 【数据结构】单链表上的基本运算
- 文件
- Sum Problem
- 浅析互斥锁与条件变量的使用
- 打开转发IP功能(IP forwarding)
- java load property
- 传纸条 优化
- linux文件学习9
- RMQ模板题 POJ 3264
- Windows7/10 64Bit安装Theano并实现GPU加速
- 【Android 进阶:翻译】Get Started With Firebase for Android
- composer autoload原理浅析
- 基础知识
- java网络编程之TCP