package egovframework.com.cmm.service; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; import egovframework.com.cmm.EgovWebUtil; import egovframework.com.cmm.util.EgovResourceCloseHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EgovProperties { private static final Logger LOGGER = LoggerFactory.getLogger(EgovProperties.class); //파일구분자 final static String FILE_SEPARATOR = System.getProperty("file.separator"); //프로퍼티 파일의 물리적 위치 //public static final String GLOBALS_PROPERTIES_FILE = System.getProperty("user.home") + FILE_SEPARATOR + "egovProps" +FILE_SEPARATOR + "globals.properties"; //public static final String RELATIVE_PATH_PREFIX = EgovProperties.class.getResource("").getPath() + FILE_SEPARATOR+ ".." + FILE_SEPARATOR + ".." + FILE_SEPARATOR; public static final String RELATIVE_PATH_PREFIX = EgovProperties.class.getResource("").getPath().substring(0, EgovProperties.class.getResource("").getPath().lastIndexOf("com")); public static final String GLOBALS_PROPERTIES_FILE = RELATIVE_PATH_PREFIX + "egovProps" + FILE_SEPARATOR + "globals.properties"; public static String getPathProperty(String keyName) { String value = ""; System.out.println("GLOBALS_PROPERTIES_FILE ==>" + GLOBALS_PROPERTIES_FILE); LOGGER.debug("getPathProperty : {} = {}", GLOBALS_PROPERTIES_FILE, keyName); FileInputStream fis = null; try { Properties props = new Properties(); fis = new FileInputStream(EgovWebUtil.filePathBlackList(GLOBALS_PROPERTIES_FILE)); props.load(new BufferedInputStream(fis)); value = props.getProperty(keyName).trim(); value = RELATIVE_PATH_PREFIX + "egovProps" + System.getProperty("file.separator") + value; } catch (FileNotFoundException fne) { LOGGER.debug("Property file not found.", fne); throw new RuntimeException("Property file not found", fne); } catch (IOException ioe) { LOGGER.debug("Property file IO exception", ioe); throw new RuntimeException("Property file IO exception", ioe); } finally { EgovResourceCloseHelper.close(fis); } return value; } public static String getProperty(String keyName) { String value = ""; LOGGER.debug("getProperty : {} = {}", GLOBALS_PROPERTIES_FILE, keyName); FileInputStream fis = null; try { Properties props = new Properties(); fis = new FileInputStream(EgovWebUtil.filePathBlackList(GLOBALS_PROPERTIES_FILE)); props.load(new BufferedInputStream(fis)); if (props.getProperty(keyName) == null) { return ""; } value = props.getProperty(keyName).trim(); } catch (FileNotFoundException fne) { LOGGER.debug("Property file not found.", fne); throw new RuntimeException("Property file not found", fne); } catch (IOException ioe) { LOGGER.debug("Property file IO exception", ioe); throw new RuntimeException("Property file IO exception", ioe); } finally { EgovResourceCloseHelper.close(fis); } return value; } public static String getPathProperty(String fileName, String key) { FileInputStream fis = null; try { Properties props = new Properties(); fis = new FileInputStream(EgovWebUtil.filePathBlackList(fileName)); props.load(new BufferedInputStream(fis)); fis.close(); String value = props.getProperty(key); value = RELATIVE_PATH_PREFIX + "egovProps" + System.getProperty("file.separator") + value; return value; } catch (FileNotFoundException fne) { LOGGER.debug("Property file not found.", fne); throw new RuntimeException("Property file not found", fne); } catch (IOException ioe) { LOGGER.debug("Property file IO exception", ioe); throw new RuntimeException("Property file IO exception", ioe); } finally { EgovResourceCloseHelper.close(fis); } } public static String getProperty(String fileName, String key) { FileInputStream fis = null; try { Properties props = new Properties(); fis = new FileInputStream(EgovWebUtil.filePathBlackList(fileName)); props.load(new BufferedInputStream(fis)); fis.close(); String value = props.getProperty(key); return value; } catch (FileNotFoundException fne) { LOGGER.debug("Property file not found.", fne); throw new RuntimeException("Property file not found", fne); } catch (IOException ioe) { LOGGER.debug("Property file IO exception", ioe); throw new RuntimeException("Property file IO exception", ioe); } finally { EgovResourceCloseHelper.close(fis); } } public static ArrayList> loadPropertyFile(String property) { // key - value 형태로 된 배열 결과 ArrayList> keyList = new ArrayList>(); String src = property.replace('\\', File.separatorChar).replace('/', File.separatorChar); FileInputStream fis = null; try { File srcFile = new File(EgovWebUtil.filePathBlackList(src)); if (srcFile.exists()) { Properties props = new Properties(); fis = new FileInputStream(src); props.load(new BufferedInputStream(fis)); fis.close(); Enumeration plist = props.propertyNames(); if (plist != null) { while (plist.hasMoreElements()) { Map map = new HashMap(); String key = (String) plist.nextElement(); map.put(key, props.getProperty(key)); keyList.add(map); } } } } catch (IOException ex) { LOGGER.debug("IO Exception", ex); throw new RuntimeException(ex); } finally { EgovResourceCloseHelper.close(fis); } return keyList; } }