使用JFig管理你的配置文件
Managing Your Configuration with JFig 介紹了專用于管理配置文件的開(kāi)源項(xiàng)目 JFig,通過(guò)文章的描述,感覺(jué)這個(gè)項(xiàng)目的功能還是頗為誘人的,尤為不錯(cuò)的一點(diǎn)是它可以將多個(gè)配置文件組織在一起——前提是這些配置文件的DTD格式是一樣的——這在公司內(nèi)部有多個(gè)組件產(chǎn)品的情況下,是對(duì)配置文檔較好的組織方式了,當(dāng)然,前提是各個(gè)組件使用統(tǒng)一格式的配置文件了,然后在項(xiàng)目中通過(guò)一個(gè)基本配置文件(可以命名為:base.config.xml)把各個(gè)配置文件組織起來(lái),如: <?xml version="1.0" encoding="GBK"?> <CONFIGURATION> <INCLUDE name="myproject.dev.xml" /> <INCLUDE name="database.config.xml" /> <INCLUDE name="module.config.xml" /> 由于JFig自身提供的方法稍有不便,故對(duì)其進(jìn)行了封裝,構(gòu)建了一個(gè)靜態(tài)類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>是用來(lái)讀取配置文件信息,是對(duì)JFig這個(gè)開(kāi)源項(xiàng)目的二次封裝。雖然犧牲了該項(xiàng)目的 * 一些靈活性,但是更加便于使用。 * <br>需要詳細(xì)了解 JFig, 可瀏覽其網(wǎng)站:<a href="https:///project/showfiles.php?group_id=85731&package_id=104908" target="_blank">JFig 下載</a> * <br>使用時(shí)需注意的是:reprocessConfiguration()方法尚未測(cè)試成功,懷疑是JVM的支持問(wèn)題。 * 故如需修改配置文件,還需重啟服務(wù)器。<br> * <code>Config</code>可以直接在應(yīng)用程序或是一般類的main方法中直接使用,不需進(jìn)行初始化操作(已封裝)??蓞⒄誱ain方法。<br> * <code>base.config.xml</code>是配置文件名,其內(nèi)容格式如下: * <pre> * <?xml version="1.0" encoding="UTF-8"?><!-- 如需使用中文配置信息,UTF-8需改為GBK或gb2312 --> * * <configuration> * <!-- 此處可引入其它配置文件,類似jsp中的include --> * <!-- 不過(guò)“繼承”也許是更確切的說(shuō)法了。base.config.xml繼承myproject.dev.xml --> * <!-- 這樣子配置文件中的section將會(huì)覆蓋父文件的同名section --> * <include name="myproject.dev.xml"/> * * <section name="paths"> * <entry key="homeDir" value="$user.home$" /><!-- 系統(tǒng)中的環(huán)境變量 --> * <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將被存入系統(tǒng)環(huán)境變量 --> * <!-- 故可直接通過(guò) 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重名,則以后者為準(zhǔn)。<br> * @author 郁也風(fēng) * @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); } } |
|