Skip to content

Package: AbstractBaseKapuaSetting

AbstractBaseKapuaSetting

nameinstructionbranchcomplexitylinemethod
AbstractBaseKapuaSetting(DataConfiguration)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
fromMap(Map)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
get(Class, SettingKey)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
get(Class, SettingKey, Object)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getBoolean(SettingKey)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getBoolean(SettingKey, Boolean)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getBoolean(SettingKey, boolean)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getDouble(SettingKey)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getDouble(SettingKey, Double)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getDouble(SettingKey, double)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getFloat(SettingKey)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getFloat(SettingKey, Float)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getFloat(SettingKey, float)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getInt(SettingKey)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getInt(SettingKey, Integer)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getInt(SettingKey, int)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getList(Class, SettingKey)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getLong(SettingKey)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getLong(SettingKey, Long)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getLong(SettingKey, long)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getMap(Class, SettingKey)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getMap(Class, SettingKey, String)
M: 40 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getString(SettingKey)
M: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getString(SettingKey, String)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
isSystemPropertyHotSwap()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setSystemPropertyHotSwap(boolean)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others.
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.commons.setting;
14:
15: import java.util.HashMap;
16: import java.util.Iterator;
17: import java.util.List;
18: import java.util.Map;
19: import java.util.regex.Pattern;
20:
21: import org.eclipse.kapua.commons.setting.system.SystemSettingKey;
22:
23: import org.apache.commons.configuration.Configuration;
24: import org.apache.commons.configuration.DataConfiguration;
25: import org.apache.commons.configuration.MapConfiguration;
26: import org.apache.commons.configuration.PropertyConverter;
27:
28: /**
29: * An abstract base class which does not make any assumptions on where the
30: * configuration comes from
31: *
32: * @param <K> The settings key type
33: */
34: public class AbstractBaseKapuaSetting<K extends SettingKey> {
35:
36: private boolean systemPropertyHotSwap;
37:
38: /**
39: * Create an abstract configuration from a provided map
40: * <p>
41: * This is useful for testing when the configuration has to be provided
42: * </p>
43: *
44: * @param map the map of values
45: * @return the configuration, may be {@code null} if the "map" parameter was null
46: */
47: public static <K extends SettingKey> AbstractBaseKapuaSetting<K> fromMap(Map<String, Object> map) {
48:• if (map == null) {
49: return null;
50: }
51: return new AbstractBaseKapuaSetting<>(new DataConfiguration(new MapConfiguration(map)));
52: }
53:
54: protected final DataConfiguration config;
55:
56: public AbstractBaseKapuaSetting(final DataConfiguration dataConfiguration) {
57: this.config = dataConfiguration;
58: systemPropertyHotSwap = config.getBoolean(SystemSettingKey.SETTINGS_HOTSWAP.key(), false);
59: }
60:
61: /**
62: * Get the property for the key
63: *
64: * @param cls
65: * @param key
66: * @return
67: * @deprecated since 1.0. Use typed get instead.
68: */
69: @Deprecated
70: public <T> T get(Class<T> cls, K key) {
71: return config.get(cls, key.key());
72: }
73:
74: /**
75: * Get the property for the key 'key'. Returns a default value if no property for that key is found.
76: *
77: * @param cls
78: * @param key
79: * @param defaultValue
80: * @return
81: * @deprecated since 1.0. Use typed get instead.
82: */
83: @Deprecated
84: public <T> T get(Class<T> cls, K key, T defaultValue) {
85: return config.get(cls, key.key(), defaultValue);
86: }
87:
88: /**
89: * Get the property list values for the key 'key'
90: *
91: * @param cls
92: * @param key
93: * @return
94: */
95: public <T> List<T> getList(Class<T> cls, K key) {
96: return config.getList(cls, key.key());
97: }
98:
99: /**
100: * Get properties map with key matching the provided prefix and regex
101: *
102: * @param valueType
103: * @param prefixKey
104: * @param regex
105: * @return
106: */
107: public <V> Map<String, V> getMap(Class<V> valueType, K prefixKey, String regex) {
108: Map<String, V> map = new HashMap<>();
109: Configuration subsetConfig = config.subset(prefixKey.key());
110: DataConfiguration subsetDataConfig = new DataConfiguration(subsetConfig);
111:• for (Iterator<String> it = subsetConfig.getKeys(); it.hasNext(); ) {
112: String key = it.next();
113:• if (Pattern.matches(regex, key)) {
114: map.put(key, subsetDataConfig.get(valueType, key));
115: }
116: }
117: return map;
118: }
119:
120: /**
121: * Get properties map with key matching the provided prefix
122: *
123: * @param valueType
124: * @param prefixKey
125: * @return
126: */
127: public <V> Map<String, V> getMap(Class<V> valueType, K prefixKey) {
128: Map<String, V> map = new HashMap<>();
129: Configuration subsetConfig = config.subset(prefixKey.key());
130: DataConfiguration subsetDataConfig = new DataConfiguration(subsetConfig);
131:• for (Iterator<String> it = subsetConfig.getKeys(); it.hasNext(); ) {
132: String key = it.next();
133: map.put(key, subsetDataConfig.get(valueType, key));
134: }
135: return map;
136: }
137:
138: /**
139: * Get an integer property
140: *
141: * @param key
142: * @return
143: */
144: public int getInt(K key) {
145:• if (systemPropertyHotSwap) {
146: String sysProp = System.getProperty(key.key());
147:• if (sysProp != null) {
148: return PropertyConverter.toInteger(sysProp);
149: }
150: }
151: return config.getInt(key.key());
152: }
153:
154: /**
155: * Get an integer property with a default value (if property is not found)
156: *
157: * @param key
158: * @param defaultValue
159: * @return
160: */
161: public int getInt(K key, int defaultValue) {
162:• if (systemPropertyHotSwap) {
163: String sysProp = System.getProperty(key.key());
164:• if (sysProp != null) {
165: return PropertyConverter.toInteger(sysProp);
166: }
167: }
168: return config.getInt(key.key(), defaultValue);
169: }
170:
171: /**
172: * Get an integer property with a default value (if property is not found)
173: *
174: * @param key
175: * @param defaultValue
176: * @return
177: */
178: public int getInt(K key, Integer defaultValue) {
179:• if (systemPropertyHotSwap) {
180: String sysProp = System.getProperty(key.key());
181:• if (sysProp != null) {
182: return PropertyConverter.toInteger(sysProp);
183: }
184: }
185: return config.getInt(key.key(), defaultValue);
186: }
187:
188: /**
189: * Get a boolean property
190: *
191: * @param key
192: * @return
193: */
194: public boolean getBoolean(K key) {
195:• if (systemPropertyHotSwap) {
196: String sysProp = System.getProperty(key.key());
197:• if (sysProp != null) {
198: return PropertyConverter.toBoolean(sysProp);
199: }
200: }
201: return config.getBoolean(key.key());
202: }
203:
204: /**
205: * Get a boolean property with a default value (if property is not found)
206: *
207: * @param key
208: * @param defaultValue
209: * @return
210: */
211: public boolean getBoolean(K key, boolean defaultValue) {
212:• if (systemPropertyHotSwap) {
213: String sysProp = System.getProperty(key.key());
214:• if (sysProp != null) {
215: return PropertyConverter.toBoolean(sysProp);
216: }
217: }
218: return config.getBoolean(key.key(), defaultValue);
219: }
220:
221: /**
222: * Get a boolean property with a default value (if property is not found)
223: *
224: * @param key
225: * @param defaultValue
226: * @return
227: */
228: public boolean getBoolean(K key, Boolean defaultValue) {
229:• if (systemPropertyHotSwap) {
230: String sysProp = System.getProperty(key.key());
231:• if (sysProp != null) {
232: return PropertyConverter.toBoolean(sysProp);
233: }
234: }
235: return config.getBoolean(key.key(), defaultValue);
236: }
237:
238: /**
239: * Get a String property
240: *
241: * @param key
242: * @return
243: */
244: public String getString(K key) {
245:• if (systemPropertyHotSwap) {
246: String sysProp = System.getProperty(key.key());
247:• if (sysProp != null) {
248: return sysProp;
249: }
250: }
251: return config.getString(key.key());
252: }
253:
254: /**
255: * Get a String property with a default value (if property is not found)
256: *
257: * @param key
258: * @param defaultValue
259: * @return
260: */
261: public String getString(K key, String defaultValue) {
262:• if (systemPropertyHotSwap) {
263: String sysProp = System.getProperty(key.key());
264:• if (sysProp != null) {
265: return sysProp;
266: }
267: }
268: return config.getString(key.key(), defaultValue);
269: }
270:
271: /**
272: * Get a long property
273: *
274: * @param key
275: * @return
276: */
277: public long getLong(K key) {
278:• if (systemPropertyHotSwap) {
279: String sysProp = System.getProperty(key.key());
280:• if (sysProp != null) {
281: return PropertyConverter.toLong(sysProp);
282: }
283: }
284: return config.getLong(key.key());
285: }
286:
287: /**
288: * Get a long property with a default value (if property is not found)
289: *
290: * @param key
291: * @param defaultValue
292: * @return
293: */
294: public long getLong(K key, long defaultValue) {
295:• if (systemPropertyHotSwap) {
296: String sysProp = System.getProperty(key.key());
297:• if (sysProp != null) {
298: return PropertyConverter.toLong(sysProp);
299: }
300: }
301: return config.getLong(key.key(), defaultValue);
302: }
303:
304: /**
305: * Get a long property with a default value (if property is not found)
306: *
307: * @param key
308: * @param defaultValue
309: * @return
310: */
311: public long getLong(K key, Long defaultValue) {
312:• if (systemPropertyHotSwap) {
313: String sysProp = System.getProperty(key.key());
314:• if (sysProp != null) {
315: return PropertyConverter.toLong(sysProp);
316: }
317: }
318: return config.getLong(key.key(), defaultValue);
319: }
320:
321: /**
322: * Get a float property
323: *
324: * @param key
325: * @return
326: */
327: public float getFloat(K key) {
328:• if (systemPropertyHotSwap) {
329: String sysProp = System.getProperty(key.key());
330:• if (sysProp != null) {
331: return PropertyConverter.toFloat(sysProp);
332: }
333: }
334: return config.getFloat(key.key());
335: }
336:
337: /**
338: * Get a float property with a default value (if property is not found)
339: *
340: * @param key
341: * @param defaultValue
342: * @return
343: */
344: public float getFloat(K key, float defaultValue) {
345:• if (systemPropertyHotSwap) {
346: String sysProp = System.getProperty(key.key());
347:• if (sysProp != null) {
348: return PropertyConverter.toFloat(sysProp);
349: }
350: }
351: return config.getFloat(key.key(), defaultValue);
352: }
353:
354: /**
355: * Get a float property with a default value (if property is not found)
356: *
357: * @param key
358: * @param defaultValue
359: * @return
360: */
361: public float getFloat(K key, Float defaultValue) {
362:• if (systemPropertyHotSwap) {
363: String sysProp = System.getProperty(key.key());
364:• if (sysProp != null) {
365: return PropertyConverter.toFloat(sysProp);
366: }
367: }
368: return config.getFloat(key.key(), defaultValue);
369: }
370:
371: /**
372: * Get a double property
373: *
374: * @param key
375: * @return
376: */
377: public double getDouble(K key) {
378:• if (systemPropertyHotSwap) {
379: String sysProp = System.getProperty(key.key());
380:• if (sysProp != null) {
381: return PropertyConverter.toDouble(sysProp);
382: }
383: }
384: return config.getDouble(key.key());
385: }
386:
387: /**
388: * Get a double property with a default value (if property is not found)
389: *
390: * @param key
391: * @param defaultValue
392: * @return
393: */
394: public double getDouble(K key, double defaultValue) {
395:• if (systemPropertyHotSwap) {
396: String sysProp = System.getProperty(key.key());
397:• if (sysProp != null) {
398: return PropertyConverter.toFloat(sysProp);
399: }
400: }
401: return config.getDouble(key.key(), defaultValue);
402: }
403:
404: /**
405: * Get a double property with a default value (if property is not found)
406: *
407: * @param key
408: * @param defaultValue
409: * @return
410: */
411: public double getDouble(K key, Double defaultValue) {
412:• if (systemPropertyHotSwap) {
413: String sysProp = System.getProperty(key.key());
414:• if (sysProp != null) {
415: return PropertyConverter.toDouble(sysProp);
416: }
417: }
418: return config.getDouble(key.key(), defaultValue);
419: }
420:
421: public boolean isSystemPropertyHotSwap() {
422: return systemPropertyHotSwap;
423: }
424:
425: public void setSystemPropertyHotSwap(boolean systemPropertyHotSwap) {
426: this.systemPropertyHotSwap = systemPropertyHotSwap;
427: }
428: }