//************************************************************************** // Filename : ConfigHelper.java, WSSE Toolkit 2005 // // Author : Denis Pilipchuk // // Purpose : Helper class for configuration processing. Its static functions // are thread-safe. // // Disclaimer: This software is provided "as-is" and does not imply // any warranty or responsibility. //**************************************************************************** package wsse.Toolkit.Utils.Config; import java.util.Enumeration; import java.util.HashMap; import java.util.Properties; import java.util.ResourceBundle; import wsse.Toolkit.Utils.Xml.XmlFactory; public class ConfigHelper { // Name of the default config file private static final String DefConfig = "WsseToolkit"; // Supported property names private static final String PropXmlFactory = "XmlFactoryImpl"; private static final String PropUserDirectory = "UserDirImpl"; private static final String PropSamlGenerator = "SamlGeneratorImpl"; // Defaults for supported properties private static final String DefXmlFactoryImpl = "wsse.Toolkit.Utils.Xml.XmlFactory"; private static final String DefUserDirImpl = "wsse.Agents.Sample.SampleUserDirectory"; private static final String DefSamlGenImpl = "wsse.Agents.Sample.SampleAssertionGen"; public static ConfigHelper GetInstance(String config) { if (null == m_CachedHelper) { m_CachedHelper = new ConfigHelper(config); } return m_CachedHelper; } public void InitConfig(String config) { String cfg = null != config ? config : ConfigHelper.DefConfig; // Retrieve config file and keep it as properties Properties loadProp = ConfigHelper.LoadConfig(cfg); m_props = null != loadProp ? loadProp : new Properties(); } ////////////////////////////////////////////////////////////////////////// // Generic entry getter methods public ConfigEntry GetEntry(String entryName) { return GetEntry(entryName, null); } public ConfigEntry GetEntry(String entryName, String defValue) { if (null == entryName || entryName.length() < 1) { throw new NullPointerException("Null or empty config entry name passed"); } // Use cached enrties, if possible ConfigEntry entry = null; synchronized (m_cachedEntries) { if (m_cachedEntries.containsKey(entryName)) { entry = (ConfigEntry)m_cachedEntries.get(entryName); } else { String value = m_props.getProperty(entryName, defValue); if (null != value) { entry = new ConfigEntry(entryName, value); m_cachedEntries.put(entryName, entry); } } } return entry; } ////////////////////////////////////////////////////////////////////////// // Helper methods // Those are just helpers to retrieve well-known configuration entries public XmlFactory GetXmlFactory() throws ConfigException { ConfigEntry entry = GetEntry(ConfigHelper.PropXmlFactory, ConfigHelper.DefXmlFactoryImpl); return (XmlFactory)entry.GetAsObject(); } ////////////////////////////////////////////////////////////////////////// // End of helper methods private ConfigHelper(String config) { InitConfig(config); } // Retrieve the specified config file private static Properties LoadConfig(String name) { Properties prop = null; try { ResourceBundle bundle = ResourceBundle.getBundle(name); //Load Responses Properties file prop = new Properties(); Enumeration enum = bundle.getKeys(); while (enum.hasMoreElements()) { String key = (String) enum.nextElement(); prop.put(key, bundle.getObject(key)); } } catch (Exception e) { // Properties file not found on the classpath, use the defaults } return prop; } private static ConfigHelper m_CachedHelper; private Properties m_props; private HashMap m_cachedEntries = new HashMap(); }