package foundation.server.config; import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.servlet.ServletContext; import foundation.translator.Translator; import foundation.util.Util; import foundation.variant.provider.DataEvent; import foundation.variant.provider.IVariantsProvider; import foundation.variant.provider.VariantProviderType; public class Configer implements IVariantsProvider { private static Configer instance; private static Object lock = new Object(); private static String Path_Application; private static String Path_WebInfo; private static String Path_Config; private static String Path_SQL; private Map params; 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) { getInstance(); instance.params = new HashMap(); Path_Application = servletContext.getRealPath("").replace("\\", "/"); Path_WebInfo = servletContext.getRealPath("/WEB-INF").replace("\\", "/"); Path_Config = Path_WebInfo + "/config/"; Path_SQL = Path_WebInfo + "/sql/"; } public static Configer newInstance() { return new Configer(); } public static void activeInstance(Configer configer) { instance = configer; } public static void clear() { instance.params.clear(); } public void addParam(String name, String value) throws Exception { params.put(name.toLowerCase(), value); } public static String getParam(String name) { if (name == null) { return null; } return instance.params.get(name.toLowerCase()); } public static String getString(String name) { if (name == null) { return null; } name = name.toLowerCase(); return instance.params.get(name); } public static String getString(String name, String defaultValue) { String value = getString(name); if (Util.isEmpty(value)) { return defaultValue; } return value; } public static int getInt(String name) throws Exception { String value = getString(name); if (Util.isEmpty(value)) { return 0; } return Translator.toInteger(value); } public static int getInt(String name, int defaultValue) { int result = defaultValue; try { result = getInt(name); } catch (Exception e) { result = defaultValue; } return result; } public static boolean getBoolean(String name, boolean defaultValue) { String value = getString(name); return Translator.toBoolean(value, defaultValue); } public static String getPath_WebInfo() { return Path_WebInfo; } public static String getPath_Config() { return Path_Config; } public static String getPath_TimerConfig() { return Path_Config + "timer.properties"; } public static String getPath_Application() { return Path_Application; } public static String getPath_Application(String subpath) { if (subpath == null) { return Path_Application; } subpath = subpath.replace("\\", "/"); if ('/' != subpath.charAt(0)) { subpath = "/" + subpath; } if ('/' != subpath.charAt(subpath.length() - 1)) { subpath = subpath + "/"; } return Path_Application + subpath; } public static String getPath_LoggerConfig() { return Path_Config + "log4j.xml"; } public static String getPath_ActivePeriodConfig() { return Path_Config + "activeperiod.properties"; } public static String getPath_SQLConfig() { return Path_SQL; } public static String getPath_SQLDTD() { return Path_Config + "sql.dtd"; } public static String getPath_ServerConfig() { return Path_Config + "server.xml"; } public static String getPath_MainConfig() { return Path_Config + "config.xml"; } public static String getPath_WXConfig() { return Path_Config + "weixin.properties"; } //he mail.xml 20230829 public static String getPath_MailConfig() { return Path_Config + "mail.xml"; } //he message.xml 20230807 public static String getPath_SmsConfig() { return Path_Config + "message.xml"; } public static String getPath(String name, String defaultValue) { String path = getString(name); if (path == null) { path = defaultValue; } return path; } @Override public VariantProviderType getProviderType() { return VariantProviderType.Persist; } @Override public String getProviderName() { return "configer"; } @Override public boolean containsVariant(String variantName) { if (variantName == null) { return false; } variantName = variantName.toLowerCase(); return params.containsKey(variantName); } @Override public Object getVariantValue(DataEvent dataEvent, String variantName) { if (variantName == null) { return null; } if ("path_application".equalsIgnoreCase(variantName)) { return Path_Application; } else if ("path_config".equalsIgnoreCase(variantName)) { return Path_Config; } else if ("path_sql".equalsIgnoreCase(variantName)) { return Path_SQL; } else if ("path_webinfo".equalsIgnoreCase(variantName)) { return Path_WebInfo; } else { return params.get(variantName.toLowerCase()); } } @Override public Set getVariantNames() { return params.keySet(); } }