package com.oyl.base.util; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.ConfigurationFactory; import org.apache.commons.configuration.ConversionException; public class BaseXmlConfig { public static final String MODE_DEV = "development"; protected static final String ERR_NOELEMENT = "###NO SUCH ELEMENT###"; protected static final String ERR_CONVERT = "###CONVERSION ERROR###"; protected static final String ERR_GENERAL = "###ERROR###"; protected static final String NUMERIC_ERR_IND = "-1"; protected static final String BLANKS = " "; // 40 spaces protected static final String NL = System.getProperty("line.separator"); protected static final List EMPTY_LIST = new ArrayList(); protected static Map configReg = new HashMap(); protected Map configMap; protected Configuration config; // ------------------ CONSTRUCTORS --------------------*/ protected BaseXmlConfig(File cfgFile) throws ConfigurationException, MalformedURLException { ConfigurationFactory factory = new ConfigurationFactory(); URL configURL = cfgFile.toURL(); factory.setConfigurationURL(configURL); config = factory.getConfiguration(); configMap = new HashMap(); }
/** * Loads the specific configuration file. * * @param configFileName the file name */ public void loadConfig(String configFileName) { try { ConfigurationFactory factory = new ConfigurationFactory(configFileName); config = factory.getConfiguration(); log.info("Configuration loaded: " + configFileName); } catch (Exception ex) { log.error(ex.getMessage(), ex); throw new RuntimeException("Configuration loading error: " + configFileName, ex); } }
/** * Load in a single property data source, or a "config.xml" descriptor file (specifying multiple * data sources). Properties are added to the global composite property data set. * * <p> * * <p>N.B. Due to underlying Jakarta (commons configuration) implementation, any previously loaded * properties (with duplicate keys) will override subsequently loaded properties. ie clients * should load in override (eg user) data before loading default (eg core) data. * * <p> * * <p>N.B. Also system properties are currently loaded before any calls to loadPropertyData, so * they take precedence over everything. * * @param listName the path name of the property data source. If the name ends in "config.xml" * then the file is treated as a Jakarta commons configuration descriptor file. If the file * ends in ".xml" it is loaded as a Jakarta XML property configuration file. Otherwise it is * loaded as a legacy flat-file key-value pair plain text property file. N.B. Although Jakarta * supports other data sources, eg JDBC, these are not yet supported via this method. * @throws ConfigurationException * @see gda.configuration.properties.PropertiesConfig#loadPropertyData(java.lang.String) */ @Override public void loadPropertyData(String listName) throws ConfigurationException { Configuration userConfig = null; if (listName.contains(".xml")) { if (listName.endsWith("config.xml")) { // ***** // FIXME 'ConfigurationFactory' should be replaced with the new and improved // 'DefaultConfigurationBuilder' // ***** // create a JCC configuration factory from a JCC config descriptor // file and make a JCC configuration interface/object from it ConfigurationFactory factory = new ConfigurationFactory(); // README - fix to get relative paths in config.xml working. // See comment for this method for explanation. configFactoryBasePathBugWorkaround(factory, listName); // now try to load in config.xml - relative paths should now work factory.setConfigurationFileName(listName); userConfig = factory.getConfiguration(); } else { // load a JCC XML-format property file userConfig = new XMLConfiguration(listName); } } else { if (listName.contains(".properties")) { // load a classic java properties flat-textfile, // containing just name-value pairs - with extended JCC syntax userConfig = new PropertiesConfiguration(listName); } } if (userConfig != null) { config.addConfiguration(userConfig); configMap.put(listName, userConfig); } }