package chat.server; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Properties; import org.apache.log4j.Logger; public class ConfigerLoader { protected static Logger logger; private static ConfigerLoader instance; private Properties properties; static { logger = Logger.getLogger(ConfigerLoader.class); } private ConfigerLoader() { properties = new Properties(); } public synchronized static ConfigerLoader getInstance() { if (instance == null) { instance = new ConfigerLoader(); } return instance; } public static void staticLoad() throws Exception { getInstance(); instance.load(); } public void load() throws Exception { String path = Configer.getPath_MainConfig(); File file = new File(path); loadOneFile(file); } private void loadOneFile(File file) throws Exception { try { logger.debug("load config file:" + file); BufferedReader reader = new BufferedReader(new FileReader(file)); try { String line; while ((line = reader.readLine()) != null) { if (line.startsWith("#")) { continue; } if (line.isEmpty() || line.matches("^\\s*$")) { continue; } int delimiterIdx = line.indexOf(' '); String key; String value = ""; if (delimiterIdx > 0) { key = line.substring(0, delimiterIdx).trim(); value = line.substring(delimiterIdx).trim(); } else { key = line.trim(); } properties.put(key, value); } } finally { reader.close(); } } catch (Exception e) { logger.error("can not load dispatch file: " + file); logger.error(e); throw e; } } }