📄 propertycontainer.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.config.lowlevel;import java.util.ArrayList;import java.util.Hashtable;import java.util.List;import org.butor.log.Log;/** * @version 1.0 * @author */public class PropertyContainer implements IPropertyContainer { public static final String MACRO_CONF_DIR = "${conf_dir}"; public static final String MACRO_LOG_DIR = "${log_dir}"; public static final String MACRO_REPOSITORY_CONF_DIR = "${repository_conf_dir}"; public static final String MACRO_APP_DIR = "${app_dir}"; // public static final String CACHE_FOUND_SUFFIX = "_found_and_cached"; public static final String CACHE_NOT_FOUND_SUFFIX = "_not_found_and_cached"; /** * The custom configuration properties. */ protected ArrayList f_properties = new ArrayList(); /** * Cache for accessed properties. */ protected Hashtable f_cache = new Hashtable(); protected String f_confDir = null; protected String f_appDir = null; protected String f_logDir = null; protected String f_repositoryConfDir = null; /** * Constructor for GenericConfig. */ public PropertyContainer() { super(); } /** * Get size. * return int, number of properties in this container. */ public int size() { return f_properties.size(); } /** * Get all properties. * * @return List. */ public List getProperties() { return (this.f_properties); } /** * Add a new custom configuration property. * * @param name Object, property key * @param value Object, property value */ public void addProperty(Object key, Object value) { removeProperty(key); f_properties.add(new Property(key, value)); } /** * Add a new custom configuration property. * * @param value Object, property value */ public void addProperty(Object value) { addProperty(null, value); } /** * remove a property element by key. * * @param name Object, Property key */ public void removeProperty(Object key) { if (key == null) { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "removeProperty(Object)", "Got NULL key"); return; } // free if cached synchronized(f_cache) { String keyFoundCache = key +CACHE_FOUND_SUFFIX; String keyNotFoundCache = key +CACHE_NOT_FOUND_SUFFIX; if (f_cache.containsKey(keyFoundCache)) { f_cache.remove(keyFoundCache); } if (f_cache.containsKey(keyNotFoundCache)) { f_cache.remove(keyNotFoundCache); } } synchronized (f_properties) { for (int i=0; i<f_properties.size(); i++) { Property prop = (Property)f_properties.get(i); if (prop.getKey() != null && prop.getKey().equals(key)) { f_properties.remove(i); break; } } } } /** * Get configuration property value by key. * * @param name Object, Property key * * @return Object, property value */ public Object getProperty(String key) { if (key == null) { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_WARN, "getProperty(String)", "Got NULL key"); return null; } String keyFoundCache = key +CACHE_FOUND_SUFFIX; String keyNotFoundCache = key +CACHE_NOT_FOUND_SUFFIX; if (f_cache.containsKey(keyFoundCache)) { return f_cache.get(keyFoundCache); } else if (f_cache.containsKey(keyNotFoundCache)) { return null; } synchronized (f_properties) { for (int i=0; i<f_properties.size(); i++) { Property prop = (Property)f_properties.get(i); if (prop.getKey() != null && prop.getKey().equals(key)) { Object value = prop.getValue(); f_cache.put(keyFoundCache, value); return value; } } } Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "getProperty(String)", "property not found, key='" +key +"'"); f_cache.put(keyNotFoundCache, ""); return null; } /** * check if a key exists in config. * * @param name String, Property key * * @return boolean. */ public boolean existsKey(String key) { if (key == null) { Log.logStr(this, Log.LOG_TYPE_WARN, "existsKey()", "Got NULL key"); return false; } String keyFoundCache = key +CACHE_FOUND_SUFFIX; String keyNotFoundCache = key +CACHE_NOT_FOUND_SUFFIX; if (f_cache.containsKey(keyFoundCache)) { return true; } else if (f_cache.containsKey(keyNotFoundCache)) { return false; } synchronized (f_properties) { for (int i=0; i<f_properties.size(); i++) { Property prop = (Property)f_properties.get(i); if (prop.getKey() != null && prop.getKey().equals(key)) { Object value = prop.getValue(); f_cache.put(keyFoundCache, value); return true; } } } f_cache.put(keyNotFoundCache, ""); return false; } /** * Get configuration property key by index. * * @param index int, Property index * * @return Object, property name */ public Object getPropertyKey(int index) { synchronized (f_properties) { if (index < 0 || index > f_properties.size()) { return null; } Property prop = (Property)f_properties.get(index); Object key = prop.getKey(); return key; } } /** * Get configuration property value by index. * * @param index int, Property index * * @return Object, property value */ public Object getProperty(int index) { String keyFoundCache = index +CACHE_FOUND_SUFFIX; String keyNotFoundCache = index +CACHE_NOT_FOUND_SUFFIX; if (f_cache.containsKey(keyFoundCache)) { return f_cache.get(keyFoundCache); } else if (f_cache.containsKey(keyNotFoundCache)) { return null; } synchronized (f_properties) { if (index < 0 || index > f_properties.size()) { f_cache.put(keyNotFoundCache, ""); return null; } Property prop = (Property)f_properties.get(index); Object value = prop.getValue(); f_cache.put(keyFoundCache, value); return value; } } /** * toString this object content. * * @return String. */ public String toString() { synchronized(f_properties) { StringBuffer buffer = new StringBuffer(""); for (int i=0; i<f_properties.size(); i++) { Property prop = (Property)f_properties.get(i); buffer.append(prop); buffer.append("\n"); } return buffer.toString(); } } // special getters ======================================================= /** * get a long property value. * * @param key String, Name of the property to get * @param defaultVal long, Default value to return if the key is * not found in the property file * * @return value associated with the key */ public long getPropertyLong(String key, long defaultVal) { long val; String valStr = (String) getProperty(key); if (valStr == null) { return defaultVal; } try { val = (new Long(valStr)).longValue(); } catch (NumberFormatException e) { Log.logException(this, Log.LOG_TYPE_WARN, "getPropertyLong", e); val = defaultVal; } return val; } /** * get a long property value. * * @param key String, Name of the property to get * @param defaultVal int, Default value to return if the key is * not found in the property file * * @return value associated with the key */ public int getPropertyInt(String key, int defaultVal) { int val; String valStr = (String) getProperty(key); if (valStr == null) { return defaultVal; } try { val = (new Integer(valStr)).intValue(); } catch (NumberFormatException e) { Log.logException(this, Log.LOG_TYPE_WARN, "getPropertyInt", e); val = defaultVal; } return val; } /** * get a double property value. * * @param key String, Name of the property to get * @param defaultVal double, Default value to return if the key is * not found in the property file * * @return value associated with the key */ public double getPropertyDouble(String key, double defaultVal) { double val; String valStr = (String) getProperty(key); if (valStr == null) { return defaultVal; } try { val = (new Double(valStr)).doubleValue(); } catch (NumberFormatException e) { Log.logException(this, Log.LOG_TYPE_WARN, "getPropertyDouble", e); val = defaultVal; } return val; } /** * Get a property list * * @param name String, property list name * * @return PropertyList */ public IProperties getPropertyList(String name) { Object value = getProperty(name); if (value instanceof IProperties) { return (IProperties)value; } Log.logStr(this, Log.WARN, "getPropertyList()", "Property " +name +" is not a List!"); return null; } /** * get a boolean property value. * * @param key String, Name of the property to get * @param defaultVal boolean, Default value to return if the key is * not found in the property file * * @return value associated with the key */ public boolean getPropertyBoolean(String key, boolean defaultVal) { boolean val; String valStr = (String) getProperty(key); if (valStr == null) { return defaultVal; } try { val = (new Boolean(valStr)).booleanValue(); } catch (NumberFormatException e) { Log.logException(this, Log.LOG_TYPE_WARN, "getPropertyBoolean", e); val = defaultVal; } return val; } /** * get a String property value. * * @param key String, Name of the property to get * @param defaultVal String, Default value to return if the key is * not found in the property file * * @return value associated with the key */ public String getProperty(String key, String defaultVal) { String valStr = (String) getProperty(key); if (valStr == null) { valStr = defaultVal; } return valStr; } /** * Set the application directory. * * @param dir String. */ protected void setAppDir(String dir) { f_appDir = dir; } public String getAppDir() { return f_appDir; } /** * Set the application directory. * * @param dir String. */ protected void setRepositoryConfDir(String dir) { f_repositoryConfDir = dir; } public String getRepositoryConfDir() { return f_repositoryConfDir; } /** * Set the application directory. * * @param dir String. */ protected void setConfDir(String dir) { f_confDir = dir; } public String getConfDir() { return f_confDir; } /** * Set the logging directory. * * @param dir String. */ protected void setLogDir(String dir) { f_logDir = dir; } public String getLogDir() { return f_logDir; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -