View Javadoc

1   package org.naftulin.configmgr;
2   
3   import java.util.Map;
4   
5   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  		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  		final String s = getString(configurationKey, propertyKey);
36  		if (s.length() == 0) {
37  			throw new IllegalArgumentException("Found configuration "+ configurationKey + 
38  					" and entry " + propertyKey + " but property value retrieved is of lenth 0, which is not a chatracter");
39  		}
40  		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  		final String propertyValue = getString(configurationKey, propertyKey);
58  		try {
59  			value = (Double) Double.parseDouble(propertyValue);
60  		} catch(NumberFormatException e) {
61  			throw new IllegalArgumentException("Found configuration "+ configurationKey + 
62  					" and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not double.");
63  		}
64  		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  		final String propertyValue = getString(configurationKey, propertyKey);
82  		try {
83  			value = (Float) Float.parseFloat(propertyValue);
84  		} catch(NumberFormatException e) {
85  			throw new IllegalArgumentException("Found configuration "+ configurationKey + 
86  					" and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not float.");
87  		}
88  		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 		final String propertyValue = getString(configurationKey, propertyKey);
106 		try {
107 			value = (Integer) Integer.parseInt(propertyValue);
108 		} catch(NumberFormatException e) {
109 			throw new IllegalArgumentException("Found configuration "+ configurationKey + 
110 					" and entry " + propertyKey + " but property value " + propertyValue + " retrieved is not integer.");
111 		}
112 		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 		final String propertyValue = getString(configurationKey, propertyKey);
130 		try {
131 			value = (Long) Long.parseLong(propertyValue);
132 		} catch(NumberFormatException e) {
133 			throw new IllegalArgumentException("Found configuration "+ configurationKey + 
134 					" and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not long value.");
135 		}
136 		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 		final String propertyValue = getString(configurationKey, propertyKey);
154 		try {
155 			value = (Short) Short.parseShort(propertyValue);
156 		} catch(NumberFormatException e) {
157 			throw new IllegalArgumentException("Found configuration "+ configurationKey + 
158 					" and entry " + propertyKey + " but property value + " + propertyValue + " retrieved is not short value.");
159 		}
160 		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 		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 		final Object configurationEntry = ConfigurationUtilityHelper.getObject(configurationKey, propertyKey);
191 		
192 		validateEntryIsMap(configurationKey, configurationEntry);
193 		final Object o = ((Map) configurationEntry).get(propertyKey);
194 		if (o == null) {
195 			throw new IllegalArgumentException("Configuration with by key " + configurationKey + " was found, " +
196 					" configuration entry " + configurationEntry + " was found, but proptery key " +
197 					propertyKey + " was not found in configuration entry.");
198 		}
199 		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 		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 		return ConfigurationUtilityHelper.getConfigurationManagement();		
225 	}
226 
227 	public static void reload() {
228 		ConfigurationUtilityHelper.reload();
229 	}
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 		return ConfigurationUtilityHelper.getConfigurationManager();
239 	}
240 	
241 	private static void validateEntryIsMap(final String configurationKey,
242 			final Object configurationEntry) {
243 		if (!(configurationEntry instanceof Map)) {
244 			throw new IllegalArgumentException("Configuration with by key " + configurationKey + " was found, but it is of type " + 
245 					configurationEntry.getClass().getName() +" not of Map type. " +
246 					"Only Map typed entry content (Property, JNDI Database or Direct Database by default) can be used. ");
247 		}
248 	}
249 
250 
251 
252 }