package chat.server; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import frame.variant.translator.Translator; public class Configer { private static Configer instance; private static Object lock = new Object(); private Map params; private String Path_Application; private String Path_WebInfo; private String Path_Config; private String Path_SQL; private Configer() { params = new HashMap(); } public static Configer getInstance() { if (instance == null) { synchronized (lock) { if (instance == null) { instance = new Configer(); } } } return instance; } public static void init(ServletContext servletContext) { try { getInstance(); instance.params = new HashMap(); instance.Path_Application = servletContext.getRealPath("").replace("\\", "/"); instance.Path_WebInfo = servletContext.getRealPath("/WEB-INF").replace("\\", "/"); instance.Path_Config = instance.Path_WebInfo + "/config/"; instance.Path_SQL = instance.Path_WebInfo + "/sql/"; instance.load(); } catch(Exception e) { e.printStackTrace(); } } private void load() throws Exception { File file = new File(Path_Config + "/wildfirechat.conf"); Reader reader = new FileReader(file); BufferedReader br = new BufferedReader(reader); try { String line; while ((line = br.readLine()) != null) { line = line.trim(); if (line.startsWith("#")) { continue; } loadOneLine(line); } } finally { reader.close(); } } private void loadOneLine(String line) { if (line == null) { return; } line = line.trim(); int delimiterIdx = line.indexOf(' '); if (delimiterIdx <= 0) { return; } String key = line.substring(0, delimiterIdx).trim(); String value = line.substring(delimiterIdx).trim(); params.put(key, value); } public static String getPath_WebInfo() { return instance.Path_WebInfo; } public static String getPath_Config() { return instance.Path_Config; } public static String getPath_TimerConfig() { return instance.Path_Config + "timer.properties"; } public static String getWebserviceURI() { return "http://www.jydatas.com/"; } public static String getPath_Application() { return instance.Path_Application; } public static String getPath_Application(String subpath) { if (subpath == null) { return instance.Path_Application; } subpath = subpath.replace("\\", "/"); if ('/' != subpath.charAt(0)) { subpath = "/" + subpath; } if ('/' != subpath.charAt(subpath.length() - 1)) { subpath = subpath + "/"; } return instance.Path_Application + subpath; } public static String getPath_LoggerConfig() { return instance.Path_Config + "log4j.properties"; } public static String getPath_SQLConfig() { return instance.Path_SQL; } public static String getPath_SQLDTD() { return instance.Path_Config + "sql.dtd"; } public static String getPath_MainConfig() { return instance.Path_Config + "config.xml"; } public static String getPath_Datasource() { return instance.Path_Config + "datasource.xml"; } public static String getPath_Upload(String username) { return instance.Path_Application + "/upload/" + username; } public static String getPath_Temp() { return instance.Path_Application + "/temp"; } public static String getPath_Temp(String username) { return instance.Path_Application + "/temp/" + username; } public static String getPath_Template() { return instance.Path_Application + "/template"; } public static String getPath_WXConfig() { return instance.Path_Config + "weixin.properties"; } public static Integer getInteger(String name, Integer defaultValue) { String value = getString(name); return Translator.toInteger(value, defaultValue); } public static boolean getBoolean(String name, boolean defaultValue) { String value = getString(name); return Translator.toBoolean(value, defaultValue); } public static String getString(String name) { if (name == null) { return null; } name = name.toLowerCase(); String result = instance.params.get(name); return result; } }