/** * create by sanghoon lee * * */ package util; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; public class ConfigInjector { private static ConfigInjector config = new ConfigInjector(); final static String CRLF = "\n\r"; Properties props; private ConfigInjector() { init(); } private void init() { loadConfig(); } /** * properties load.. * */ public void loadConfig() { props = new Properties(); try { if(isWindow()) { // 윈도우인 경우.. props.load( new BufferedReader( new FileReader( new File( "properties/batch.properties" ) ))); } else { // 윈도우가 아닌경우.. props.load(new BufferedReader(new FileReader(System .getProperty("batch.properties")))); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public boolean isWindow() { String os = System.getProperty("os.name"); if(os.indexOf("Window") != -1) return true; // 윈도우 일경우 return false; // 윈도우가 아닐경우 } /** * load 한 프로퍼티 파싱 * */ public String parseConfig(String key) { String value = ""; try { value = props.getProperty(key); } catch (Exception e) { // ignore } return value; } /** * propertis 를 읽어와 Object 으로 리턴 getObject 에서는 * */ public Object getObject(String key) { String value = ""; try { value = parseConfig(key); } catch (Exception e) { // ignore } return value.trim(); } /** * properties 를 읽어와 String 으로 리턴 * */ public String getString(String key) { String value = ""; try { value = (String) getObject(key); } catch (ClassCastException e) { // ignore } return value; } /** * properties 를 읽어와 boolean 으로 리턴 * */ public boolean getBoolean(String key) { boolean flag = false; try { flag = Boolean.parseBoolean((String) getObject(key)); } catch (ClassCastException e) { // ignore } return flag; } /** * properties 를 읽어와 int 으로 리턴 * */ public int getInt(String key) { int result = 0; try { result = Integer.parseInt((String) getObject(key)); } catch (ClassCastException e) { // ignore } return result; } /** * properties 를 읽어와 Long 으로 리턴 * */ public long getLong(String key) { long result = 0; try { result = Long.parseLong((String) getObject(key)); } catch (ClassCastException e) { // ignore } return result; } /** * ConfigInjector 인스턴스를 리턴 * */ public static ConfigInjector getInstance() { return config; } /* * public static void main( String[] args ) { ConfigInjector config = * ConfigInjector.getInstance(); String pw = * config.getString("copydb.userpw"); String dir = * config.getString("batchjob.log.dir"); System.out.println( pw ); * System.out.println( dir ); ConfigEntity entity = * config.getConfigEntity(); System.out.println(entity); } */ } |