package common;
import java.util.Properties;
import java.util.Enumeration;
import java.util.HashMap;
public class PropertyLoader {
private static PropertyLoader instance;
private HashMap<String, Properties> propMap = new HashMap<String, Properties>();
private Properties prop = new Properties();
private String charSetBase = "KSC5601";
private String fileIdBase = "config";
private PropertyLoader() {}
public static synchronized PropertyLoader getInstance() {
if(instance == null) instance = new PropertyLoader();
return instance;
}
public Properties getProperties() {
return getProperties("");
}
public Properties getProperties(String fileId) {
return checkPropMap(fileId);
}
public Properties getProperties(String fileId, String charSet) {
return convertChar(checkPropMap(fileId), charSet);
}
private Properties checkPropMap(String fileId) {
if(fileId.equals("")) fileId = fileIdBase;
if(propMap.get(fileId) == null) {
try {
String propFile = "properties/" + fileId + ".properties";
prop.load(getClass().getClassLoader().getResourceAsStream(propFile));
} catch(Exception e) {
e.printStackTrace();
}
propMap.put(fileId, prop);
}
return prop;
}
private Properties convertChar(Properties propBefore, String charSet) {
if(charSet.equals("")) charSet = charSetBase;
Properties propAfter = new Properties();
Enumeration<Object> en = propBefore.keys();
while(en.hasMoreElements()) {
try {
String enKey = (String)en.nextElement();
String enVal = new String(propBefore.getProperty(enKey).getBytes("8859_1"), charSet);
propAfter.setProperty(enKey, enVal);
} catch(Exception e) {
e.printStackTrace();
}
}
return propAfter;
}
}
'Java' 카테고리의 다른 글
lotto 당첨번호 파싱 (4) | 2010.04.07 |
---|---|
openCsv sample function (2) | 2010.04.07 |
AES java 암호화 구현 function sample (1) | 2010.04.07 |
Unzip.java (3) | 2010.04.07 |
java 한글 인코딩 (swing) (1) | 2010.04.07 |