1 | |
package org.naftulin.configmgr; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.Map; |
5 | |
|
6 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
7 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
8 | |
import org.apache.log4j.Logger; |
9 | |
|
10 | 0 | public class ConfigurationUtilityHelper { |
11 | |
private static final String JUNIT_WARNING = ">>Configuration Managment System in JUNit Mode<<"; |
12 | |
|
13 | 5 | private static final Logger log = Logger.getLogger(ConfigurationUtilityHelper.class); |
14 | |
|
15 | 5 | private static boolean unitTest = false; |
16 | 5 | private static final Map<String, Object> configObjects = new HashMap<String, Object>(); |
17 | 5 | private static final Map<ConfigPropertyKey, Object> configPropertyObjects = new HashMap<ConfigPropertyKey, Object>(); |
18 | |
|
19 | 10 | public static void setUnitTest(final boolean unitTestDesired) { unitTest =unitTestDesired; } |
20 | 0 | public static boolean getUnitTest() { return unitTest; } |
21 | |
public static void setConfigEntryObject(final String configurationKey, final Object object) { |
22 | 5 | if (!unitTest) { |
23 | 5 | throw new IllegalStateException("Cannot set configuration entry, the state is not unit test. Please set the state to unittest first."); |
24 | |
} |
25 | |
|
26 | 0 | printJUnitModeWarning(); |
27 | |
|
28 | 0 | configObjects.put(configurationKey, object); |
29 | 0 | } |
30 | |
private static void printJUnitModeWarning() { |
31 | 30 | System.out.println(JUNIT_WARNING); |
32 | 30 | log.warn(JUNIT_WARNING); |
33 | 30 | } |
34 | |
|
35 | |
public static void setConfigPropertyObject(final String configurationKey, final String propertyKey, final String value) { |
36 | 20 | if (!unitTest) { |
37 | 5 | throw new IllegalStateException("Cannot set configuration property, the state is not unit test. Please set the state to unittest first."); |
38 | |
} |
39 | 15 | printJUnitModeWarning(); |
40 | 15 | configPropertyObjects.put(new ConfigurationUtilityHelper.ConfigPropertyKey(configurationKey, propertyKey), value); |
41 | 15 | } |
42 | |
|
43 | |
|
44 | |
static Object getObject(final String configurationKey) { |
45 | 110 | Object configurationEntryObject= null; |
46 | 110 | if (unitTest) { |
47 | 0 | printJUnitModeWarning(); |
48 | 0 | configurationEntryObject = configObjects.get(configurationKey); |
49 | |
} else { |
50 | 110 | ConfigurationManager cm = null; |
51 | 110 | cm = getConfigurationManager(); |
52 | |
|
53 | 110 | configurationEntryObject = getConfigurationEntry(configurationKey, cm); |
54 | |
} |
55 | |
|
56 | 105 | return configurationEntryObject; |
57 | |
|
58 | |
} |
59 | |
|
60 | |
public static Object getObject(final String configurationKey, final String propertyKey) { |
61 | 125 | Object configurationEntryObject= null; |
62 | 125 | if (unitTest) { |
63 | 15 | printJUnitModeWarning(); |
64 | 15 | Object value = configPropertyObjects.get(new ConfigurationUtilityHelper.ConfigPropertyKey(configurationKey, propertyKey)); |
65 | 15 | Map<String, Object> m = new HashMap<String, Object>(); |
66 | 15 | m.put(propertyKey, value); |
67 | 15 | configurationEntryObject = m; |
68 | 15 | } else { |
69 | 110 | configurationEntryObject = getObject(configurationKey); |
70 | |
} |
71 | 120 | return configurationEntryObject; |
72 | |
} |
73 | |
|
74 | |
static Object getConfigurationEntry(final String configurationKey, |
75 | |
final ConfigurationManager cm) { |
76 | 110 | final Object configurationEntry = cm.getConfigurationSilent(configurationKey); |
77 | 110 | if (configurationEntry == null) { |
78 | 5 | throw new IllegalArgumentException("Configuration with by key " + configurationKey + " not found. "); |
79 | |
} |
80 | 105 | return configurationEntry; |
81 | |
} |
82 | |
|
83 | |
static ConfigurationManager getConfigurationManager() { |
84 | |
ConfigurationManager cm; |
85 | |
try { |
86 | 115 | cm = ConfigurationManagerFactory.getConfigurationManager(); |
87 | 0 | } catch(ConfigurationManagerException e) { |
88 | 0 | throw new ConfigurationManagerRuntimeException(e.getMessage(), e); |
89 | 115 | } |
90 | 115 | return cm; |
91 | |
} |
92 | |
|
93 | |
static ConfigurationManagement getConfigurationManagement() { |
94 | |
ConfigurationManagement cm; |
95 | |
try { |
96 | 0 | cm = ConfigurationManagerFactory.getConfigurationManagement(); |
97 | 0 | } catch(ConfigurationManagerException e) { |
98 | 0 | throw new ConfigurationManagerRuntimeException(e.getMessage(), e); |
99 | 0 | } |
100 | 0 | return cm; |
101 | |
} |
102 | |
|
103 | |
public static void reload() { |
104 | 0 | if (unitTest) { |
105 | 0 | printJUnitModeWarning(); |
106 | |
|
107 | |
} else { |
108 | |
try { |
109 | 0 | ConfigurationManagement management = ConfigurationManagerFactory.getConfigurationManagement(); |
110 | 0 | management.reload(); |
111 | 0 | } catch(ConfigurationManagerException e) { |
112 | 0 | throw new ConfigurationManagerRuntimeException(e.getMessage(), e); |
113 | 0 | } |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | 0 | private static class ConfigPropertyKey { |
118 | |
final String configKey; |
119 | |
final String propertyKey; |
120 | |
|
121 | 30 | ConfigPropertyKey(String configKey, String propertyKey) { |
122 | 30 | this.configKey = configKey; |
123 | 30 | this.propertyKey = propertyKey; |
124 | 30 | } |
125 | |
|
126 | |
public int hashCode() { |
127 | 30 | return HashCodeBuilder.reflectionHashCode(this); |
128 | |
} |
129 | |
|
130 | |
public boolean equals(Object that) { |
131 | 15 | return EqualsBuilder.reflectionEquals(this, that); |
132 | |
} |
133 | |
|
134 | |
} |
135 | |
|
136 | |
|
137 | |
} |