使用JFig管理你的配置文件

来源:百度文库 编辑:神马文学网 时间:2024/04/27 16:38:14
使用JFig管理你的配置文件 (转载)
Managing Your Configuration with JFig 介绍了专用于管理配置文件的开源项目 JFig,通过文章的描述,感觉这个项目的功能还是颇为诱人的,尤为不错的一点是它可以将多个配置文件组织在一起——前提是这些配置文件的DTD格式是一样的——这在公司内部有多个组件产品的情况下,是对配置文档较好的组织方式了,当然,前提是各个组件使用统一格式的配置文件了,然后在项目中通过一个基本配置文件(可以命名为:base.config.xml)把各个配置文件组织起来,如:

由于JFig自身提供的方法稍有不便,故对其进行了封装,构建了一个静态类Config,并重载了JFig提供的所有public方法:

package com.someok.utils;import java.util.List;import java.util.Map;import java.util.Properties;import org.apache.log4j.Logger;import org.igfay.jfig.JFig;import org.igfay.jfig.JFigDictionary;import org.igfay.jfig.JFigException;import org.igfay.jfig.JFigIF;import org.igfay.jfig.JFigListener;import org.igfay.jfig.JFigLocator;import org.igfay.jfig.JFigLocatorIF;/*** Config是用来读取配置文件信息,是对JFig这个开源项目的二次封装。虽然牺牲了该项目的* 一些灵活性,但是更加便于使用。* 
需要详细了解 JFig, 可浏览其网站:JFig 下载*
使用时需注意的是:reprocessConfiguration()方法尚未测试成功,怀疑是JVM的支持问题。* 故如需修改配置文件,还需重启服务器。
* Config可以直接在应用程序或是一般类的main方法中直接使用,不需进行初始化操作(已封装)。可参照main方法。
* base.config.xml是配置文件名,其内容格式如下:*
* ** *    *    *    *    **    
* * *
**
* *
** * *
* * *
*
*
*
注意:如include文件中的section与base.config.xml中的section重名,则以后者为准。
* @author 郁也风* @version 1.01, 2004-4-20*/public class Config {private static Logger log = Logger.getLogger(Config.class);private static JFigIF jFig;private static JFigLocatorIF jFigLoc;final public static String CONFIG_NAME = "base.config.xml"; // 缺省的配置文件名。此名称为硬编码,不可在外部更改final static String CONFIG_LOCALTION = "config.location";final static String CLASSPATH = "classpath";// 初始化配置属性static {// System.setProperty("config.location", "classpath");// System.setProperty("config.filename", "base.config.xml");jFigLoc = new JFigLocator(CONFIG_NAME);jFigLoc.setConfigLocation(CLASSPATH);jFig = JFig.getInstance(jFigLoc);}public static void main(String[] args) {System.out.println(Config.getValue("mail", "isChild"));System.out.println(Config.getValue("mail", "test.father"));System.out.println(Config.getValue("aaa", "bbb", "cccc"));System.out.println(System.getProperty("bruce"));Config.print();}/*** Add JFig listeners to list so they can be notified when there* is a significant change in the configuration.**@param listener The feature to be added to the ConfigEventListener* attribute*/public static void addConfigEventListener(JFigListener listener) {jFig.addConfigEventListener(listener);}/*** Print the values in the JFig dictionary.*/public static void print() {jFig.print();}/*** Reprocess the configuration creating a new config dictionary*/public static void reprocessConfiguration() throws JFigException {jFig.reprocessConfiguration();}/*** return the ConfigurationDictionary* Made public so we can access this from a jsp and show the configuration* via html.*/public static JFigDictionary getConfigDictionary() {return jFig.getConfigDictionary();}/*** Convenience method for getting values as array.* The value is tokenized depending on the first token found in* the following order: comma, semicolon, colon, space**/public static String[] getArrayValue(String section, String key) throws JFigException {return jFig.getArrayValue(section, key);}/*** Convenience method for getting values as boolean*/public static boolean getBooleanValue(String section, String key, String notFoundValue) {return jFig.getBooleanValue(section, key, notFoundValue);}/*** Convenience method for getting values as float**@param section Description of Parameter*@param key Description of Parameter*@param notFoundValue Description of Parameter*@return The FloatValue value*@exception JFigException Description of Exception*/public static float getFloatValue(String section, String key, String notFoundValue) throws JFigException {return jFig.getFloatValue(section, key, notFoundValue);}/*** Convenience method for getting values as int*/public static int getIntegerValue(String section, String key) throws JFigException {return jFig.getIntegerValue(section, key);}/*** Convenience method for getting values as int, with default value*/public static int getIntegerValue(String section, String key, String notFoundValue) {return jFig.getIntegerValue(section, key, notFoundValue);}/*** Return the value for this section and key. If none found, return the* default value.** @param section Description of Parameter* @param key Description of Parameter* @param defaultValue Description of Parameter* @return The Value value*/public static String getValue(String section, String key, String defaultValue) {return jFig.getValue(section, key, defaultValue);}/*** Return a list of all values starting with "key" in the section.* If section xxx contains x.1, x.2, and x.3,* getValuesStartingWith("xxx", "x.") returns a list containing* x.1, x.2, and x.3.** @param section* @param key* @param defaultValue* @return List*/public static List getValuesStartingWith(String section, String key) {return jFig.getValuesStartingWith(section, key);}/*** Return a map of all values starting with "key" in the scetcion.* If section xxx contains x.1=a, x.2=b, and x.3=c,* getValuesStartingWith("xxx", "x.") returns a map containing* x.1,a x.2,b and x.3,c.** @param section* @param key* @param defaultValue* @return List*/public static Map getEntriesStartingWith(String section, String key) {return jFig.getEntriesStartingWith(section, key);}/*** Call configParser to get the value for a key in a given section.**/public static String getValue(String section, String key) throws RuntimeException {String value;try {value = jFig.getValue(section, key);} catch (JFigException e) {log.error(e.toString());throw new RuntimeException(e.getMessage());}return value;}/*** Return an entire section as a Map*/public static Map getSection(String section) {return jFig.getSection(section);}/*** Return a section as a Properties object*/public static Properties getSectionAsProperties(String section) {return jFig.getSectionAsProperties(section);}/*** Return a section populated in a supplied Properties object.*/public static Properties getSectionAsProperties(String section, Properties properties) {return jFig.getSectionAsProperties(section, properties);}/*** Set a configuration value.* Most values are set during initial parsing so this is rarely used.*/public static void setConfigurationValue(String sectionName, String keyString, String valueString) {jFig.setConfigurationValue(sectionName, keyString, valueString);}/*** Convenience method for getting values as array with default value.*/public static String[] getArrayValue(String section, String key, String notFoundValue) {return jFig.getArrayValue(section, key, notFoundValue);}}