Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 266   Methods: 14
NCLOC: 141   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ConfigurationManager.java 57,1% 76,4% 78,6% 71,9%
coverage coverage
 1    package org.jconfig;
 2   
 3    import java.util.HashMap;
 4    import org.jconfig.error.ErrorReporter;
 5    import org.jconfig.event.FileListener;
 6    import org.jconfig.event.FileListenerEvent;
 7    import org.jconfig.handler.AbstractHandler;
 8    import org.jconfig.handler.ConfigurationHandler;
 9    import org.jconfig.handler.InputStreamHandler;
 10    import org.jconfig.bootstrap.*;
 11    /**
 12    * This class manages all configurations. This is the main entry point.
 13    * You can get the configurations from the ConfigurationManager.
 14    * The simple way is: <BR/>
 15    * public static final Configuration config = ConfigurationManager.getConfiguration();<BR/>
 16    * or: <BR/>
 17    * public static final Configuration config = ConfigurationManager.getConfiguration(&quot;myconfig&quot;);<BR/>
 18    * <BR />
 19    * If you use the getConfiguration() the ConfigurationManager will return the configuration
 20    * with the name &quot;default&quot;. If there is no configuration wit the given name then
 21    * the ConfigurationManager will use the InputStreamHandler and will try to read the
 22    * file "config.xml". This file has to be in the classpath.
 23    *
 24    *@author Andreas Mecky andreas.mecky@xcom.de
 25    *@author Terry Dye terry.dye@xcom.de
 26    */
 27    public class ConfigurationManager {
 28   
 29    // the one and only instance
 30    private static ConfigurationManager cm = null;
 31    // all instances of the Configurations
 32    private static HashMap configurations = null;
 33    // a mapping of associated handlers and configurations
 34    private static HashMap handlerMapping;
 35   
 36    /**
 37    * Constructor for the ConfigurationManager object
 38    *
 39    */
 40  1 private ConfigurationManager() {
 41  1 configurations = new HashMap();
 42  1 handlerMapping = new HashMap();
 43  1 BootstrapLoader bl = BootstrapLoaderFactory.getLoader();
 44  1 try {
 45  1 LoadedItem[] configs = bl.load();
 46  1 for ( int i = 0; i < configs.length;i++) {
 47  1 LoadedItem li = configs[i];
 48  1 Configuration config = li.getConfig();
 49  1 ConfigurationHandler handler = li.getHandler();
 50  1 String name = li.getName();
 51  1 configurations.put(name, config);
 52  1 handlerMapping.put(name, handler);
 53    }
 54    }
 55    catch (Exception e) {
 56  0 ErrorReporter.getErrorHandler().reportError("Exception while trying to load configuratins at startup:",e);
 57    }
 58    }
 59   
 60    /**
 61    * This method returns the one and only instance of the
 62    * ConfigurationManager.
 63    *
 64    * @return the instance of the ConfigurationManager
 65    */
 66  122 public static synchronized ConfigurationManager getInstance() {
 67  122 if (cm == null) {
 68  1 cm = new ConfigurationManager();
 69    }
 70  122 return cm;
 71    }
 72    /**
 73    * This method returns the configuration with the name "default".
 74    * It calls getConfiguration("default").
 75    *
 76    * @return the configuration
 77    */
 78  6 public static Configuration getConfiguration() {
 79  6 return getConfiguration("default");
 80    }
 81   
 82    /**
 83    * This method returns the configuration for the specific name.
 84    * If there is no configuration then it will use the InputStreamHandler
 85    * and take "config.xml" as filename.<BR /><BR />
 86    *
 87    * Implementation detail: A new instance of the ConfigurationManager
 88    * will be created/instantiated if one does not exist.<BR /><BR />
 89    *
 90    * An empty Configuration will be returned if the Configuration sought
 91    * after is not found.<BR /><BR />
 92    *
 93    * This method will first look for a file called "name"_config.xml.
 94    * If this file does not exist it will look for the default config.xml
 95    * file.<BR /><BR />
 96    * Note that if the name is "default" then the method will directly
 97    * look for the config.xml file since this is the reserved name
 98    * for the default configuration.
 99    *
 100    * @param name the name of the configuration
 101    * @return the configuration
 102    */
 103  54 public static Configuration getConfiguration(String name) {
 104  54 getInstance();
 105  54 if ( name == null ) {
 106  0 return new DefaultConfiguration(name);
 107    }
 108  54 if (configurations.containsKey(name)) {
 109  19 return (Configuration) configurations.get(name);
 110    } else {
 111  35 String fileName = "config.xml";
 112  35 if ( !name.equals("default") ) {
 113  34 fileName = name+"_config.xml";
 114    }
 115  35 InputStreamHandler ish = new InputStreamHandler(fileName);
 116  35 try {
 117  35 Configuration config = ish.load(name);
 118  30 if ( config != null ) {
 119    //config.setConfigName(name);
 120  30 configurations.put(name, config);
 121  30 handlerMapping.put(name, ish);
 122  30 getInstance().addFileListener(name,new ConfigurationFileListener(name));
 123    }
 124  30 return config;
 125    } catch (ConfigurationManagerException e) {
 126  5 ErrorReporter.getErrorHandler().reportError("Problem while trying to read configuration. Returning new configuration.",e);
 127  5 return new DefaultConfiguration(name);
 128    }
 129    }
 130    }
 131   
 132    /**
 133    * This method will load a configuration with the specific ConfigurationHandler
 134    * and store it in the internal map with the given name.
 135    *
 136    * @param configHandler an implementation of the ConfigurationHandler
 137    * @param configurationName the name of the configuration
 138    */
 139  2 public void load(ConfigurationHandler configurationHandler,String configurationName) throws ConfigurationManagerException {
 140  2 Configuration config = configurationHandler.load(configurationName);
 141  2 if(configurationHandler instanceof AbstractHandler) {
 142  2 ((AbstractHandler)configurationHandler).addFileListener(new ConfigurationFileListener(configurationName));
 143    }
 144  2 if (config != null) {
 145    //config.setConfigName(configurationName);
 146  2 configurations.put(configurationName, config);
 147  2 handlerMapping.put(configurationName, configurationHandler);
 148    }
 149    }
 150   
 151    /**
 152    * This method will store the configuration for the given name using the associated
 153    * handler that was used when the configuration was loaded.
 154    *
 155    * @param configName the name of the configuration
 156    * @throws ConfigurationManagerException
 157    */
 158  1 public void save(String configName) throws ConfigurationManagerException {
 159  1 ConfigurationHandler handler = (ConfigurationHandler)handlerMapping.get(configName);
 160  1 if ( handler == null ) {
 161  0 throw new ConfigurationManagerException("No handler assigned to this configuration");
 162    }
 163  1 Configuration config = (Configuration)configurations.get(configName);
 164  1 if ( config == null ) {
 165  0 throw new ConfigurationManagerException("No configuration found with the given name");
 166    }
 167  1 save(handler, config);
 168    }
 169   
 170  30 public void addFileListener(String configName,FileListener fl) {
 171  30 ConfigurationHandler handler = (ConfigurationHandler)handlerMapping.get(configName);
 172  30 if ( handler != null ) {
 173  30 ((AbstractHandler)handler).addFileListener(fl);
 174    }
 175    }
 176    /**
 177    * This method will save a configuration using the defined ConfigurationHandler. In
 178    * most cases it is simpler to use the save(String configName) method since the ConfigurationManager
 179    * stores the handler that is asscociated with the configuration. Use this method if you want to
 180    * use a different handler. For example you have used the URLHandler to download a configuration
 181    * from a webserver and now want to save the file to a local directory
 182    *
 183    * @param handler the ConfigurationHandler that will be used to store the configuration
 184    * @param config the configuration to store
 185    * @throws ConfigurationManagerException
 186    */
 187  1 public void save(ConfigurationHandler handler, Configuration config) throws ConfigurationManagerException {
 188  1 handler.store(config);
 189  1 configurations.put(config.getConfigName(), config);
 190  1 handlerMapping.put(config.getConfigName(), handler);
 191    }
 192   
 193    /**
 194    * This method removes a configuration from the ConfigurationManager
 195    *
 196    * @param configName the name of the configuration
 197    */
 198  33 public void removeConfiguration(String configName) {
 199  33 if ( configName != null ) {
 200  33 configurations.remove(configName);
 201  33 handlerMapping.remove(configName);
 202    }
 203    }
 204   
 205    /**
 206    * This method will reload a configuration with the same handler that
 207    * was used for the initially loading.
 208    *
 209    * @param name name of the configuration
 210    */
 211  0 public void reload(String name) throws ConfigurationManagerException {
 212  0 if (configurations.containsKey(name)) {
 213  0 Configuration cfg = getConfiguration(name);
 214  0 ConfigurationHandler handler = getConfigurationHandler(name);
 215  0 if (handler == null) {
 216  0 throw new ConfigurationManagerException("There is no handler associated for this configuration");
 217    }
 218  0 cfg = handler.load(name);
 219  0 configurations.put(name, cfg);
 220    } else {
 221  0 throw new ConfigurationManagerException("There is no configuration with this name");
 222    }
 223    }
 224   
 225    /**
 226    * Returns the handler used during the creation of the Configuration.
 227    *
 228    * @param name
 229    * @return
 230    * @since Aug 26, 2003 2:35:20 PM
 231    */
 232  0 public ConfigurationHandler getConfigurationHandler(String name) {
 233  0 ConfigurationHandler handler =
 234    (ConfigurationHandler) handlerMapping.get(name);
 235  0 return handler;
 236    }
 237   
 238    /**
 239    * This method will return all configurations names
 240    *
 241    * @return a String[] containing all configuration names
 242    */
 243  1 public String[] getConfigurationNames() {
 244  1 return (String[]) configurations.keySet().toArray(new String[configurations.size()]);
 245    }
 246   
 247    }
 248   
 249    class ConfigurationFileListener implements FileListener {
 250    private String config = null;
 251   
 252  32 ConfigurationFileListener(String configurationName) {
 253  32 config = configurationName;
 254    }
 255    /**
 256    * @see org.jconfig.event.FileListener#fileChanged(org.jconfig.event.FileListenerEvent)
 257    */
 258  0 public void fileChanged(FileListenerEvent e) {
 259  0 try {
 260  0 ConfigurationManager.getInstance().reload(config);
 261    } catch (ConfigurationManagerException e1) {
 262  0 System.out.println("Problem with reloading the file after file was saved.");
 263    }
 264    }
 265   
 266    }