📄 rtconfig.java
字号:
/* return the Servlet context path */ // return null if not in a servlet, or if the context path has not been defined public static File getServletContextPath() { RTProperties servletProps = getServletContextProperties(); if (servletProps != null) { String ctxPath = servletProps.getString(RTKey.WEBAPP_CONTEXT_PATH, null); if (ctxPath != null) { File ctxPathFile = new File(ctxPath); if (ctxPathFile.isDirectory()) { return ctxPathFile; } } } return null; } // ------------------------------------------------------------------------ public static Properties loadResourceProperties(String name) { try { ClassLoader cl = ClassLoader.getSystemClassLoader(); InputStream inpStream = cl.getResourceAsStream(name); if (inpStream != null) { Properties props = new Properties(); props.load(inpStream); return props; } else { return null; } } catch (Throwable t) { Print.logException("Loading properties: " + name, t); return null; } } // ------------------------------------------------------------------------ public static Properties loadManifestProperties(Class clzz) { // NOTE: Experimental! This currently does not work!!! DO NOT USE String manifestResource = "/META-INF/MANIFEST.MF"; try { ClassLoader cl = clzz.getClassLoader(); InputStream input = cl.getResourceAsStream(manifestResource); if (input == null) { throw new FileNotFoundException("MANIFEST not found"); } BufferedReader br = new BufferedReader(new InputStreamReader(input)); Properties props = new Properties(); for (;;) { String line = br.readLine(); if (line == null) { break; } int p = line.indexOf(':'); if (p > 0) { String key = line.substring(0,p).trim(); String val = line.substring(p+1).trim(); props.setProperty(key, val); } } return props; } catch (Throwable t) { Print.logException("Loading MANIFEST properties", t); return null; } } // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ /* replace ${key} strings in the specified text with values from the runtime properties */ // default key delimiters will be used public static String insertKeyValues(String text) { return RTConfig._insertKeyValues(null, text, RTProperties.KEY_START_DELIMITER, RTProperties.KEY_END_DELIMITER); } /* replace ${key} strings in the specified text with values from the runtime properties */ // specified key delimiters will be used public static String insertKeyValues(String text, String startDelim, String endDelim) { return RTConfig._insertKeyValues(null, text, startDelim, endDelim); } public static String _insertKeyValues(Object mainKey, String text) { return RTConfig._insertKeyValues(mainKey, text, RTProperties.KEY_START_DELIMITER, RTProperties.KEY_END_DELIMITER); } public static String _insertKeyValues(final Object mainKey, String text, String startDelim, String endDelim) { if (text != null) { //Print.logInfo("Inserting global keyvalues: " + text); // replacment call-back StringTools.ReplacementMap rm = new StringTools.ReplacementMap() { private Set<Object> thisKeySet = new HashSet<Object>(); private Set<Object> fullKeySet = new HashSet<Object>(); public String get(String k) { if (k == null) { // a bit of a hack here to tell this map to reset the cached keys fullKeySet.addAll(thisKeySet); if (mainKey != null) { fullKeySet.add(mainKey); } thisKeySet.clear(); return null; } else if (fullKeySet.contains(k)) { //Print.logInfo("Key already processed: " + k); return null; } else { //Print.logInfo("Processing key: " + k); thisKeySet.add(k); return RTConfig._getString(k, null); } } }; // iterate until the string doesn't change String s_old = text; for (int i = 0; i < RTProperties.KEY_MAX_RECURSION; i++) { rm.get(null); // hack to reset the cached keys String s_new = StringTools.insertKeyValues(s_old, startDelim, endDelim, rm, false); //Print.logInfo("New String: " + s_new); if (s_new.equals(s_old)) { return s_new; } s_old = s_new; } return s_old; } else { return text; } } // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ public static boolean hasProperty(String key) { return RTConfig.hasProperty(key, true); } public static boolean hasProperty(String key, boolean dftOk) { RTProperties rtp = getPropertiesForKey(key, dftOk); return (rtp != null); } public static boolean hasProperty(String key[]) { return RTConfig.hasProperty(key, true); } public static boolean hasProperty(String key[], boolean dftOk) { if (key != null) { for (int i = 0; i < key.length; i++) { RTProperties rtp = getPropertiesForKey(key[i], dftOk); if (rtp != null) { return true; } } } return false; } // ------------------------------------------------------------------------ public static Object getProperty(String key) { return getProperty(key, null); } public static Object getProperty(String key, Object dft) { RTProperties rtp = getPropertiesForKey(key); return (rtp != null)? rtp.getProperty(key, dft) : dft; } public static void setProperty(String key, Object value) { RTProperties cfgProps = getConfigFileProperties(); cfgProps.setProperty(key, value); if ((key != null) && (value == null)) { getSystemProperties().removeProperty(key); } } public static void setProperties(Properties props) { RTProperties cfgProps = getConfigFileProperties(); cfgProps.setProperties(props); } // ------------------------------------------------------------------------ // Extract a Map containing a group of key/values from the config file properties /* public static Map<String,String> extractMap(String keyEnd, String valEnd) { // TODO: should include keyEnd/valEnd from all properties RTProperties cfgProps = getConfigFileProperties(); return cfgProps.extractMap(keyEnd, valEnd); } */ // ------------------------------------------------------------------------ public static String _getString(String key, String dft) { RTProperties rtp = getPropertiesForKey(key); if (rtp != null) { Object obj = rtp._getProperty(key, dft); return (obj != null)? obj.toString() : dft; } else { return dft; } } public static String _getString(String key[], String dft) { if (key != null) { for (int i = 0; i < key.length; i++) { RTProperties rtp = getPropertiesForKey(key[i]); if (rtp != null) { Object obj = rtp._getProperty(key[i], dft); return (obj != null)? obj.toString() : dft; } } } return dft; } public static String getString(String key) { return getString(key, null); } public static String getString(String key, String dft) { RTProperties rtp = getPropertiesForKey(key); return (rtp != null)? rtp.getString(key, dft) : dft; } public static String getString(String key[], String dft) { if (key != null) { for (int i = 0; i < key.length; i++) { RTProperties rtp = getPropertiesForKey(key[i]); if (rtp != null) { return rtp.getString(key[i], dft); } } } return dft; } public static void setString(String key, String value) { RTProperties cfgFileProps = getConfigFileProperties(); cfgFileProps.setString(key, value); } // ------------------------------------------------------------------------ public static String[] getStringArray(String key) { return getStringArray(key, null); } public static String[] getStringArray(String key, String dft[]) { RTProperties rtp = getPropertiesForKey(key); return (rtp != null)? rtp.getStringArray(key, dft) : dft; } public static String[] getStringArray(String key[], String dft[]) { if (key != null) { for (int i = 0; i < key.length; i++) { RTProperties rtp = getPropertiesForKey(key[i]); if (rtp != null) { return rtp.getStringArray(key[i], dft); } } } return dft; } public static void setStringArray(String key, String val[]) { RTProperties cfgProps = getConfigFileProperties(); cfgProps.setStringArray(key, val); } // ------------------------------------------------------------------------ public static File getFile(String key) { return getFile(key, null); } // do not include this method, otherwise "getFile(file, null)" would be ambiguous //public File getFile(String key, String dft) public static File getFile(String key, File dft) { RTProperties rtp = getPropertiesForKey(key); return (rtp != null)? rtp.getFile(key, dft) : dft; } public static File getFile(String key[], File dft) { if (key != null) { for (int i = 0; i < key.length; i++) { RTProperties rtp = getPropertiesForKey(key[i]); if (rtp != null) { return rtp.getFile(key[i], dft); } } } return dft; } public static void setFile(String key, File value) { RTProperties cfgProps = getConfigFileProperties(); cfgProps.setFile(key, value); } // ------------------------------------------------------------------------ public static double getDouble(String key) { return getDouble(key, 0.0); } public static double getDouble(String key, double dft) { RTProperties rtp = getPropertiesForKey(key);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -