Coverage Report - org.naftulin.configmgr.ConfigurationUtility
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurationUtility
86%
48/56
83%
5/6
2.143
 
 1  
 package org.naftulin.configmgr;
 2  
 
 3  
 import java.util.Map;
 4  
 
 5  0
 public class ConfigurationUtility {
 6  
         
 7  
         /**
 8  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 9  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 10  
          * and looks at value in the map based on property key. Returns value found as Boolean.
 11  
          * @param configurationKey
 12  
          * @param propertyKey
 13  
          * @return value in configuration map based on configuration key and property key.
 14  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 15  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 16  
          *         does not contain value under property key.  
 17  
          */
 18  
         public static Boolean getBoolean(final String configurationKey, final String propertyKey) {
 19  20
                 return (Boolean) Boolean.valueOf(getString(configurationKey, propertyKey));
 20  
         }
 21  
 
 22  
         /**
 23  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 24  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 25  
          * and looks at value in the map based on property key. Returns value found as Character.
 26  
          * @param configurationKey
 27  
          * @param propertyKey
 28  
          * @return value in configuration map based on configuration key and property key.
 29  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 30  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 31  
          *         does not contain value under property key, or if value stored is cannot be parsed to
 32  
          *  specified type.  
 33  
          */
 34  
         public static Character getCharacter(final String configurationKey, final String propertyKey) {
 35  20
                 final String s = getString(configurationKey, propertyKey);
 36  20
                 if (s.length() == 0) {
 37  10
                         throw new IllegalArgumentException("Found configuration "+ configurationKey + 
 38  5
                                         " and entry " + propertyKey + " but property value retrieved is of lenth 0, which is not a chatracter");
 39  
                 }
 40  15
                 return (Character) Character.valueOf(s.charAt(0));
 41  
         }
 42  
 
 43  
         /**
 44  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 45  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 46  
          * and looks at value in the map based on property key. Returns value found as Double.
 47  
          * @param configurationKey
 48  
          * @param propertyKey
 49  
          * @return value in configuration map based on configuration key and property key.
 50  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 51  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 52  
          *         does not contain value under property key, or if value stored is cannot be parsed to
 53  
          *  specified type.  
 54  
          */
 55  
         public static Double getDouble(final String configurationKey, final String propertyKey) {
 56  
                 Double value;
 57  10
                 final String propertyValue = getString(configurationKey, propertyKey);
 58  
                 try {
 59  10
                         value = (Double) Double.parseDouble(propertyValue);
 60  5
                 } catch(NumberFormatException e) {
 61  10
                         throw new IllegalArgumentException("Found configuration "+ configurationKey + 
 62  5
                                         " and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not double.");
 63  
                 }
 64  5
                 return value;
 65  
         }
 66  
 
 67  
         /**
 68  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 69  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 70  
          * and looks at value in the map based on property key. Returns value found as Float.
 71  
          * @param configurationKey
 72  
          * @param propertyKey
 73  
          * @return value in configuration map based on configuration key and property key.
 74  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 75  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 76  
          *         does not contain value under property key, or if value stored is cannot be parsed to
 77  
          *  specified type.  
 78  
          */
 79  
         public static Float getFloat(final String configurationKey, final String propertyKey) {
 80  
                 Float value;
 81  10
                 final String propertyValue = getString(configurationKey, propertyKey);
 82  
                 try {
 83  10
                         value = (Float) Float.parseFloat(propertyValue);
 84  5
                 } catch(NumberFormatException e) {
 85  10
                         throw new IllegalArgumentException("Found configuration "+ configurationKey + 
 86  5
                                         " and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not float.");
 87  
                 }
 88  5
                 return value;
 89  
         }
 90  
 
 91  
         /**
 92  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 93  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 94  
          * and looks at value in the map based on property key. Returns value found as Integer.
 95  
          * @param configurationKey
 96  
          * @param propertyKey
 97  
          * @return value in configuration map based on configuration key and property key.
 98  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 99  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 100  
          *         does not contain value under property key, or if value stored is cannot be parsed to
 101  
          *  specified type.  
 102  
          */
 103  
         public static Integer getInteger(final String configurationKey, final String propertyKey) {
 104  
                 Integer value;
 105  30
                 final String propertyValue = getString(configurationKey, propertyKey);
 106  
                 try {
 107  25
                         value = (Integer) Integer.parseInt(propertyValue);
 108  10
                 } catch(NumberFormatException e) {
 109  20
                         throw new IllegalArgumentException("Found configuration "+ configurationKey + 
 110  10
                                         " and entry " + propertyKey + " but property value " + propertyValue + " retrieved is not integer.");
 111  
                 }
 112  15
                 return value;
 113  
         }
 114  
 
 115  
         /**
 116  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 117  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 118  
          * and looks at value in the map based on property key. Returns value found as Long.
 119  
          * @param configurationKey
 120  
          * @param propertyKey
 121  
          * @return value in configuration map based on configuration key and property key.
 122  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 123  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 124  
          *         does not contain value under property key, or if value stored is cannot be parsed to
 125  
          *  specified type.  
 126  
          */
 127  
         public static Long getLong(final String configurationKey, final String propertyKey) {
 128  
                 Long value;
 129  15
                 final String propertyValue = getString(configurationKey, propertyKey);
 130  
                 try {
 131  15
                         value = (Long) Long.parseLong(propertyValue);
 132  5
                 } catch(NumberFormatException e) {
 133  10
                         throw new IllegalArgumentException("Found configuration "+ configurationKey + 
 134  5
                                         " and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not long value.");
 135  
                 }
 136  10
                 return value;
 137  
         }
 138  
 
 139  
         /**
 140  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 141  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 142  
          * and looks at value in the map based on property key. Returns value found as Short.
 143  
          * @param configurationKey
 144  
          * @param propertyKey
 145  
          * @return value in configuration map based on configuration key and property key.
 146  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 147  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 148  
          *         does not contain value under property key, or if value stored is cannot be parsed to
 149  
          *  specified type.  
 150  
          */
 151  
         public static Short getShort(final String configurationKey, final String propertyKey) {
 152  
                 Short value;
 153  10
                 final String propertyValue = getString(configurationKey, propertyKey);
 154  
                 try {
 155  10
                         value = (Short) Short.parseShort(propertyValue);
 156  5
                 } catch(NumberFormatException e) {
 157  10
                         throw new IllegalArgumentException("Found configuration "+ configurationKey + 
 158  5
                                         " and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not short value.");
 159  
                 }
 160  5
                 return value;
 161  
         }
 162  
         
 163  
         /**
 164  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 165  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 166  
          * and looks at value in the map based on property key. Returns value found as String.
 167  
          * @param configurationKey
 168  
          * @param propertyKey
 169  
          * @return value in configuration map based on configuration key and property key.
 170  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 171  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 172  
          *         does not contain value under property key.  
 173  
          */
 174  
         public static String getString(final String configurationKey, final String propertyKey) {
 175  120
                 return (String) getObject(configurationKey, propertyKey);
 176  
         }
 177  
 
 178  
         /**
 179  
          * Looks up configuration by configuration key. Assumes that configuration is of type
 180  
          * Map (property file, database key/value pairs, external configuration stored as Map)
 181  
          * and looks at value in the map based on property key. Returns value found as Object.
 182  
          * @param configurationKey
 183  
          * @param propertyKey
 184  
          * @return value in configuration map based on configuration key and property key.
 185  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 186  
          *         found; or if configuration is found, but is not of type Map, or if Map found
 187  
          *         does not contain value under property key.  
 188  
          */
 189  
         public static Object getObject(final String configurationKey, final String propertyKey) {                
 190  125
                 final Object configurationEntry = ConfigurationUtilityHelper.getObject(configurationKey, propertyKey);
 191  
                 
 192  120
                 validateEntryIsMap(configurationKey, configurationEntry);
 193  120
                 final Object o = ((Map) configurationEntry).get(propertyKey);
 194  120
                 if (o == null) {
 195  10
                         throw new IllegalArgumentException("Configuration with by key " + configurationKey + " was found, " +
 196  5
                                         " configuration entry " + configurationEntry + " was found, but proptery key " +
 197  5
                                         propertyKey + " was not found in configuration entry.");
 198  
                 }
 199  115
                 return ((Map) configurationEntry).get(propertyKey);
 200  
         }
 201  
 
 202  
         /**
 203  
          * Looks up configuration by configuration key, and returns value stored under that configuration. For example,
 204  
          * for property file, Properties would be returned; for XML - parsed and typed xml object would be returned; for
 205  
          * LDAP - the object that was stored in LDAP would be returned.
 206  
          * @param configurationKey
 207  
          * @param propertyKey
 208  
          * @return value in configuration map based on configuration key and property key.
 209  
          * @exception IllegalArgumentException if configuration based on configuration key is not
 210  
          *         found.  
 211  
          */
 212  
         public static Object getObject(final String configurationKey) {                
 213  0
                 return ConfigurationUtilityHelper.getObject(configurationKey);
 214  
         }
 215  
         
 216  
         /**
 217  
          * Returns configuration management engine object. If the object was not created yet,
 218  
          * this call will create it.
 219  
          * @return configuration management engine object.
 220  
          * @throws ConfigurationManagerRuntimeException if an error occurred while creating a configuration
 221  
          * management object.
 222  
          */
 223  
         public static ConfigurationManagement getConfigurationManagement() {
 224  0
                 return ConfigurationUtilityHelper.getConfigurationManagement();                
 225  
         }
 226  
 
 227  
         public static void reload() {
 228  0
                 ConfigurationUtilityHelper.reload();
 229  0
         }
 230  
         /**
 231  
          * Returns configuration manager. If the manager was not created yet, this call
 232  
          * will create it.
 233  
          * @return configuration manager.
 234  
          * @throws ConfigurationManagerRuntimeException if an error occurs while creating a configuration
 235  
          * manager.
 236  
          */
 237  
         public static ConfigurationManager getConfigurationManager() {
 238  5
                 return ConfigurationUtilityHelper.getConfigurationManager();
 239  
         }
 240  
         
 241  
         private static void validateEntryIsMap(final String configurationKey,
 242  
                         final Object configurationEntry) {
 243  120
                 if (!(configurationEntry instanceof Map)) {
 244  0
                         throw new IllegalArgumentException("Configuration with by key " + configurationKey + " was found, but it is of type " + 
 245  0
                                         configurationEntry.getClass().getName() +" not of Map type. " +
 246  0
                                         "Only Map typed entry content (Property, JNDI Database or Direct Database by default) can be used. ");
 247  
                 }
 248  120
         }
 249  
 
 250  
 
 251  
 
 252  
 }