1 package org.naftulin.configmgr; 2 3 /*** 4 * A factory that creates and maintains configuration management engine and configuration manager. 5 * 6 * @author Henry Naftulin 7 * @since 1.0 8 */ 9 public class ConfigurationManagerFactory { 10 private static String configurationFile = "config.xml"; 11 private static ConfigurationManagement management; 12 private static ConfigurationManager manager; 13 14 /*** 15 * Package private constructor, needed to access clear method. Only used 16 * for unit testing. 17 */ 18 ConfigurationManagerFactory() {} 19 20 public static synchronized boolean isIniialized() { 21 return management != null; 22 } 23 /*** 24 * Sets a file from which master configuration would be read. 25 * By default, the file is 'config.xml', but it could be owervritten by using this API. 26 * Only valid before configuration management is initialized, otherwise throws an exception. 27 * @param configFile the configuration file for master record. 28 * @throws ConfigurationManagerException if a configuration management engine is already initiazed. 29 */ 30 public static synchronized void setConfigurationFile(final String configFile) throws ConfigurationManagerException { 31 if (management != null) { 32 throw new ConfigurationManagerException("Configuration Manager already initialize"); 33 } 34 configurationFile = configFile; 35 } 36 37 /*** 38 * Returns configuration management engine object. If the object was not created yet, 39 * this call will create it. 40 * @return configuration management engine object. 41 * @throws ConfigurationManagerException if an error occured while creating a configuration 42 * managment object. 43 */ 44 public static synchronized ConfigurationManagement getConfigurationManagement() throws ConfigurationManagerException { 45 if (management == null) { 46 management = new ConfigurationManagementImpl(configurationFile); 47 management.reload(); 48 } 49 return management; 50 } 51 52 /*** 53 * Returns configuration manager. If the manager was not created yet, this call 54 * will create it. 55 * @return configuration manager. 56 * @throws ConfigurationManagerException if an error occurs while creating a configuration 57 * manager. 58 */ 59 public static synchronized ConfigurationManager getConfigurationManager() throws ConfigurationManagerException { 60 if (manager == null) { 61 manager = getConfigurationManagement().getConfigurationManager(); 62 } 63 return manager; 64 } 65 66 /*** 67 * Clears the factory: resets configuration management, configuration manager and configuration file. 68 * Needed for unit test settings only. 69 */ 70 static void clear() { 71 synchronized(ConfigurationManagerFactory.class) { 72 ConfigurationManagerFactory.management = null; 73 ConfigurationManagerFactory.manager = null; 74 ConfigurationManagerFactory.configurationFile = "config.xml"; 75 } 76 } 77 }