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) {
}
}
}
}