分享

使用JFig管理你的配置文件

 ljjzlm 2006-08-04
使用JFig管理你的配置文件

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

<?xml version="1.0" encoding="GBK"?>
<CONFIGURATION>
<INCLUDE name="myproject.dev.xml" />
<INCLUDE name="database.config.xml" />
<INCLUDE name="module.config.xml" />
<section name="mail"> <entry key="notifications" value="true" /> </section> </CONFIGURATION>

由于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;
/**
* <code>Config</code>是用来读取配置文件信息,是对JFig这个开源项目的二次封装。虽然牺牲了该项目的
* 一些灵活性,但是更加便于使用。
* <br>需要详细了解 JFig, 可浏览其网站:<a href="https:///project/showfiles.php?group_id=85731&package_id=104908" target="_blank">JFig 下载</a>
* <br>使用时需注意的是:reprocessConfiguration()方法尚未测试成功,怀疑是JVM的支持问题。
* 故如需修改配置文件,还需重启服务器。<br>
* <code>Config</code>可以直接在应用程序或是一般类的main方法中直接使用,不需进行初始化操作(已封装)。可参照main方法。<br>
* <code>base.config.xml</code>是配置文件名,其内容格式如下:
* <pre>
* <?xml version="1.0" encoding="UTF-8"?><!-- 如需使用中文配置信息,UTF-8需改为GBK或gb2312 -->
*
* <configuration>
*    <!-- 此处可引入其它配置文件,类似jsp中的include -->
*    <!-- 不过“继承”也许是更确切的说法了。base.config.xml继承myproject.dev.xml -->
*    <!-- 这样子配置文件中的section将会覆盖父文件的同名section -->
*    <include name="myproject.dev.xml"/>
*
*    <section name="paths">
*   <entry key="homeDir" value="$user.home$" /><!-- 系统中的环境变量 -->
*     <entry key="rootDrive" value="e:" />
*    </section>
*
*    <section name="authentication"><!-- 引用其它 section 信息,格式 [section name]{entry key} -->
*      <entry key="isCheckingApprovalPermission" value="aaaa - [paths]{rootDrive} - [authentication]{aaaaaaabbbb}" />
*    </section>
*
*    <!-- 以 properties 为名的section,其entry将被存入系统环境变量 -->
*    <!-- 故可直接通过 System.getProperty("bruce") 形式读取 -->
*    <section name=”properties”>
*   <entry key=”bruce” value=”conrad” />
*    <entry key=”riley” value=”beagle” />
*    </section>
* </configuration>
* </pre>
* <br>注意:如include文件中的section与<code>base.config.xml</code>中的section重名,则以后者为准。<br>
* @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);
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多