1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.support;
17
18 import java.io.InputStream;
19 import java.util.Properties;
20
21 import javax.annotation.CheckForNull;
22 import javax.annotation.concurrent.ThreadSafe;
23
24 import de.smartics.properties.api.config.domain.ConfigurationLoadingException;
25 import de.smartics.properties.api.config.domain.ConfigurationNotFoundException;
26 import de.smartics.properties.api.config.domain.Property;
27 import de.smartics.properties.api.config.domain.PropertyCollection;
28 import de.smartics.properties.api.config.domain.PropertyLocation;
29 import de.smartics.properties.api.config.domain.PropertyProvider;
30 import de.smartics.properties.api.config.domain.PropertyStoreAccessor;
31 import de.smartics.properties.api.config.domain.SerializableConfigurationPropertiesManagement;
32 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
33 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
34 import de.smartics.properties.api.core.domain.PropertyValidationException;
35 import de.smartics.properties.api.core.domain.ReadOnlyPropertyException;
36 import de.smartics.properties.api.core.security.PropertyValueSecurity;
37 import de.smartics.util.lang.Arg;
38 import de.smartics.util.lang.NullArgumentException;
39
40
41
42
43 @ThreadSafe
44 public abstract class AbstractInMemoryConfigurationProperties extends
45 AbstractConfigurationPropertiesManagement implements
46 SerializableConfigurationPropertiesManagement
47 {
48
49
50
51
52
53
54
55
56
57
58
59 private static final long serialVersionUID = 1L;
60
61
62
63
64
65
66 private final InMemoryPropertiesManager properties;
67
68
69
70
71
72
73
74
75
76
77 private final boolean requiresDefaultOnClassPath;
78
79
80
81
82
83
84 private final PropertyStoreAccessor propertyStoreAccessor =
85 new InMemoryPropertyStoreAccessor();
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public AbstractInMemoryConfigurationProperties(final ConfigurationKey<?> key,
102 final PropertyDescriptorRegistry registry,
103 final PropertyValueSecurity decrypter) throws NullArgumentException
104 {
105 this(key, registry, true, decrypter);
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public AbstractInMemoryConfigurationProperties(final ConfigurationKey<?> key,
121 final PropertyDescriptorRegistry registry,
122 final boolean requiresDefaultOnClassPath,
123 final PropertyValueSecurity decrypter) throws NullArgumentException
124 {
125 super(key, registry, decrypter);
126
127 properties = new InMemoryPropertiesManager(key);
128 this.requiresDefaultOnClassPath = requiresDefaultOnClassPath;
129 }
130
131
132
133
134
135
136 private final class InMemoryPropertyStoreAccessor implements
137 PropertyStoreAccessor
138 {
139
140
141
142
143
144
145 private static final long serialVersionUID = 1L;
146
147 @Override
148 public Property getPropertyFromStore(final String name)
149 {
150 try
151 {
152 return properties.getProperty(name);
153 }
154 catch (final IllegalArgumentException e)
155 {
156 return new Property(properties.getSourceId(), name, null);
157 }
158 }
159
160 @Override
161 public Property setPropertyToStore(final String key, final String value)
162 throws PropertyValidationException, ReadOnlyPropertyException
163 {
164 final Property property = ensureProperty(key, value);
165 final Property oldValue = properties.setProperty(property);
166 return oldValue;
167 }
168
169 @Override
170 public Property deletePropertyInStore(final String name)
171 {
172 return properties.removeProperty(name);
173 }
174
175 @Override
176 public PropertyCollection getPropertyCollectionFromStore()
177 {
178 return properties.getProperties();
179 }
180 }
181
182
183
184
185
186
187
188 @Override
189 public final PropertyStoreAccessor getPropertyStoreAccessor()
190 {
191 return propertyStoreAccessor;
192 }
193
194
195
196 @Override
197 protected final void addDefinitionsToStore(final PropertyProvider properties)
198 {
199 setPropertiesToStore(properties);
200 }
201
202
203
204
205
206
207
208 protected final void setPropertiesToStore(final PropertyProvider provider)
209 {
210 for (final Property property : provider.getProperties())
211 {
212 setPropertyToStore(property.getName(), property.getValue());
213 }
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public final AbstractInMemoryConfigurationProperties addClassPathProperties(
234 final Class<?> propertiesInterface) throws NullArgumentException,
235 ConfigurationLoadingException
236 {
237 Arg.checkNotNull("propertiesInterface", propertiesInterface);
238
239 final PropertyProvider properties = loadDefault(propertiesInterface);
240 addDescriptors(propertiesInterface);
241 if (properties != null)
242 {
243 addDefinitions(properties);
244 }
245 return this;
246 }
247
248 @CheckForNull
249 private PropertyProvider loadDefault(final Class<?> descriptorDefinition)
250 throws ConfigurationLoadingException
251 {
252 final String name = descriptorDefinition.getSimpleName() + ".properties";
253
254 final InputStream in = descriptorDefinition.getResourceAsStream(name);
255 if (in != null)
256 {
257 final PropertiesHelper utils = new PropertiesHelper(getKey());
258 final Properties properties = utils.readProperties(name, in);
259 final PropertyProvider provider =
260 new PropertiesPropertyProvider(getKey(), new PropertyLocation(name),
261 properties);
262 return provider;
263 }
264
265 if (requiresDefaultOnClassPath)
266 {
267 throw new ConfigurationNotFoundException(getKey(), name);
268 }
269 else
270 {
271 return null;
272 }
273 }
274
275 private Property ensureProperty(final String name, final Object value)
276 {
277 final Property property;
278 if (value instanceof Property)
279 {
280 property = (Property) value;
281 }
282 else
283 {
284 property = new Property(properties.getSourceId(), name, value);
285 }
286 return property;
287 }
288
289
290
291
292
293
294
295
296 @Override
297 public String toString()
298 {
299 return super.toString() + "Properties:\n" + properties.toString();
300 }
301 }