Java tutorial
/* * Copyright 2008-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.guzz.util; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.guzz.io.Resource; /** * ??. <BR> */ public class PropertyUtil { private static transient final Log log = LogFactory.getLog(Properties.class); /** Prefix for property placeholders: "${" */ public static final String PLACEHOLDER_PREFIX = "${"; /** Suffix for property placeholders: "}" */ public static final String PLACEHOLDER_SUFFIX = "}"; /** * Load a Properties. * @param fileName * @return return null if failed. */ public static Properties loadProperties(String fileName) { if (fileName == null) { return null; } return loadProperties(new File(fileName)); } /** * Load a Properties. * @param f file to load * @return return null if failed. */ public static Properties loadProperties(File f) { if (f == null || !f.isFile() || !f.exists()) { return null; } InputStream fis = null; try { Properties props = new Properties(); fis = new FileInputStream(f); props.load(fis); return props; } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("erron on load file: [" + f + "], msg:" + e); } return null; } finally { CloseUtil.close(fis); } } /** * properties?key. ?key?. * ??key, , ??, ?Properties, ???. * @param f * @param key * @param defaultValue */ public static String loadProperty(File f, String key, String defaultValue) { Properties props = loadProperties(f); if (props == null) return defaultValue; return props.getProperty(key, defaultValue); } /** * Load properties from classpath. * @param resName resource file name * @return return null if failed. */ public static Properties loadFromResource(String resName) { return loadFromResource(PropertyUtil.class, resName); } /** * Load properties from classpath. * @param clazz relative class * @param resName resource file name * @return return null if failed. */ public static Properties loadFromResource(Class clazz, String resName) { URL resUrl = clazz.getResource(resName); if (resUrl == null) { log.debug("resource not available! resName: " + resName); return null; } InputStream fis = null; try { fis = resUrl.openStream(); Properties props = new Properties(); props.load(fis); return props; } catch (Exception e) { if (log.isDebugEnabled()) { log.error("erron on load resource, url:[" + resUrl + "], msg:" + e); } } finally { CloseUtil.close(fis); } return null; } /** * intProperties?. <BR> * Propertiesnull, Properties?key, value???, . * @param props Properties * @param key * @param defaultValue * @return Properties? */ public static int getPropertyAsInt(Properties props, String key, int defaultValue) { if (props == null) { return defaultValue; } String strValue = props.getProperty(key); if (strValue == null) { return defaultValue; } return StringUtil.toInt(strValue, defaultValue); } /** * Properties?, trim()?. <BR> * The method return defaultValue on one of these conditions: * <li>props == null * <li>props.getProperty(key) == null * <li>props.getProperty(key).trim().length() == 0 * @param props Properties * @param key * @param defaultValue * @return Propertiestrim?? */ public static String getTrimString(Properties props, String key, String defaultValue) { if (props == null) { return defaultValue; } String strValue = props.getProperty(key); if (strValue == null) { return defaultValue; } strValue = strValue.trim(); return strValue.length() == 0 ? defaultValue : strValue; } /** * Fetch the value of the given key, and resolve ${...} placeholders , then trim() the value and return. * @param props Properties * @param key the key to fetch * @param defaultValue return defaultValue on props is null or the key doesn't exsits in props. * @return the resolved value * * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX */ public static String getTrimPlaceholdersString(Properties props, String key, String defaultValue) { if (props == null) { return defaultValue; } String text = props.getProperty(key); if (text == null) { return defaultValue; } StringBuffer buf = new StringBuffer(text); // The following code does not use JDK 1.4's StringBuffer.indexOf(String) // method to retain JDK 1.3 compatibility. The slight loss in performance // is not really relevant, as this code will typically just run on startup. int startIndex = text.indexOf(PLACEHOLDER_PREFIX); while (startIndex != -1) { int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); if (endIndex != -1) { String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); String propVal = props.getProperty(placeholder); if (propVal != null) { buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length()); } else { log.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "]"); startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length()); } } else { startIndex = -1; } } return buf.toString().trim(); } /** * >= JDK1.4 * resolve ${...} placeholders in the @param text , then trim() the value and return. * @param props Map * @param text the String text to replace * @return the resolved value * * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX */ public static String getTrimStringMatchPlaceholdersInMap(Map props, String text) { if (text == null || props == null || props.isEmpty()) { return text; } StringBuffer buf = new StringBuffer(text); int startIndex = buf.indexOf(PLACEHOLDER_PREFIX); while (startIndex != -1) { int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); if (endIndex != -1) { String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); String propVal = (String) props.get(placeholder); if (propVal != null) { buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); startIndex = buf.indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length()); } else { log.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "]"); startIndex = buf.indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length()); } } else { startIndex = -1; } } return buf.toString().trim(); } /** * longProperties?. Properties?key, . * @param props Properties * @param key * @param defaultValue * @return Properties? */ public static long getPropertyAsLong(Properties props, String key, long defaultValue) { if (props == null) { return defaultValue; } String strValue = props.getProperty(key); if (strValue == null) { return defaultValue; } try { return Long.parseLong(strValue.trim()); } catch (Exception e) { return defaultValue; } } /** * booleanProperties?. Properties?key, * . ?. "true"(?), true. : * <tt>Boolean.valueOf("True")</tt> returns <tt>true</tt>.<br> * ?: <tt>Boolean.valueOf("yes")</tt> returns <tt>false</tt>. * @param props Properties * @param key * @param defaultValue * @return Properties?. "true"(?), true. */ public static boolean getPropertyAsBool(Properties props, String key, boolean defaultValue) { if (props == null) { return defaultValue; } String strValue = props.getProperty(key); if (strValue == null) { return defaultValue; } strValue = strValue.trim(); if (strValue.length() == 0) { return defaultValue; } try { return strValue.equalsIgnoreCase("true"); } catch (Exception e) { return defaultValue; } } /** * Properties?. <BR><BR> * , null: * <li>Propertiesnull. * <li>Properties?key. * <li>Propertieskey(?<code>trim().length() == 0</code>). * @param props Properties * @param key . ??null. * @param token . ??null. * @return Properties?. */ public static String[] getPropertyAsStrAry(Properties props, String key, String token) { if (props == null) { return null; } String value = props.getProperty(key); if (value == null) { return null; } value = value.trim(); if (value.length() == 0) { return null; } return value.split(token); } /** * floatProperties?. Properties?key, . * @param props Properties * @param key . ??null. * @param defaultValue * @return Properties? */ public static float getPropertyAsFloat(Properties props, String key, float defaultValue) { if (props == null) { return defaultValue; } String strValue = props.getProperty(key); if (strValue == null) { return defaultValue; } try { return Float.parseFloat(strValue.trim()); } catch (Exception e) { return defaultValue; } } public static String getString(Properties props, String key, String defaultValue) { String value = props.getProperty(key); return value == null ? defaultValue : value; } /** * JDK Properties?Key??. <BR> * JDK PropertiestoString()key??, ?, ??. * @param props JDK Properties * @param keyPrefix key? * @return Properties?Key???. */ public static String toString(Properties props, String keyPrefix) { if (props == null || keyPrefix == null) { return "null! keyPrefix=" + keyPrefix; } final String delit = ", "; final int delitLen = delit.length(); StringBuffer sb = new StringBuffer(64); sb.append('{'); // ??()Key?. JDK // Hashtable(Properties)toString. synchronized (props) { int max = props.size() - 1; Iterator it = props.entrySet().iterator(); for (int i = 0; i <= max; i++) { Map.Entry e = (Map.Entry) (it.next()); String key = (String) e.getKey(); if (key.startsWith(keyPrefix)) { sb.append(key).append('=').append(e.getValue()); sb.append(delit); } } } sb.delete((sb.length() > delitLen) ? (sb.length() - delitLen) : 1, sb.length()); sb.append('}'); return sb.toString(); } /** * Mysql?my.cnf???[key]keykey[key]value?PropertiesMap * * @param resource ???resource * @return Map ??vsprop?Properties[]?null */ public static Map loadGroupedProps(Resource resource) { Map resources = new HashMap(); LineNumberReader lnr = null; String line = null; String groupName = null; Properties props = null; try { lnr = new LineNumberReader(new InputStreamReader(resource.getInputStream())); while ((line = lnr.readLine()) != null) { line = line.trim(); int length = line.length(); if (length == 0) continue; if (line.charAt(0) == '#') continue; if (line.startsWith("rem ")) continue; if (line.charAt(0) == '[' && line.charAt(length - 1) == ']') {//[xxxx] //?? if (groupName != null) { Properties[] p = (Properties[]) resources.get(groupName); if (p == null) { resources.put(groupName, new Properties[] { props }); } else { resources.put(groupName, ArrayUtil.addToArray(p, props)); } } //? groupName = line.substring(1, length - 1).trim(); props = new Properties(); } else { // if (groupName == null) { log.warn("ignore ungrouped config property:" + line); } else { int pos = line.indexOf('='); if (pos < 1) { props.put(line, ""); log.warn("loading special config property:" + line); } else { String key = line.substring(0, pos).trim(); String value = line.substring(pos + 1, length).trim(); props.put(key, value); } } } } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("load resource failed. resouce:[" + resource + "], msg:" + e.getMessage()); } return null; } finally { CloseUtil.close(lnr); CloseUtil.close(resource); } //?? if (groupName != null) { Properties[] p = (Properties[]) resources.get(groupName); if (p == null) { resources.put(groupName, new Properties[] { props }); } else { resources.put(groupName, ArrayUtil.addToArray(p, props)); } } return resources; } }