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.concurrent.ThreadSafe;
22
23 import de.smartics.properties.api.config.domain.ConfigurationLoadingException;
24 import de.smartics.properties.api.config.domain.ConfigurationNotFoundException;
25 import de.smartics.properties.api.config.domain.Property;
26 import de.smartics.properties.api.config.domain.PropertyCollection;
27 import de.smartics.properties.api.config.domain.SerializableConfigurationPropertiesManagement;
28 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
30 import de.smartics.properties.api.core.domain.PropertyValidationException;
31 import de.smartics.properties.api.core.domain.ReadOnlyPropertyException;
32 import de.smartics.util.lang.Arguments;
33 import de.smartics.util.lang.NullArgumentException;
34
35
36
37
38 @ThreadSafe
39 public abstract class AbstractInMemoryConfigurationProperties extends
40 AbstractConfigurationPropertiesManagement implements
41 SerializableConfigurationPropertiesManagement
42 {
43
44
45
46
47
48
49
50
51
52 private static final long serialVersionUID = 1L;
53
54
55
56
57
58
59 private final InMemoryPropertiesManager properties;
60
61
62
63
64
65
66
67
68 private final boolean requiresDefaultOnClassPath;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public AbstractInMemoryConfigurationProperties(final ConfigurationKey key,
84 final PropertyDescriptorRegistry registry) throws NullArgumentException
85 {
86 this(key, registry, true);
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100 public AbstractInMemoryConfigurationProperties(final ConfigurationKey key,
101 final PropertyDescriptorRegistry registry,
102 final boolean requiresDefaultOnClassPath) throws NullArgumentException
103 {
104 super(key, registry);
105
106 properties = new InMemoryPropertiesManager(key);
107 this.requiresDefaultOnClassPath = requiresDefaultOnClassPath;
108 }
109
110
111
112
113
114
115
116
117
118
119
120 @Override
121 protected final Property getPropertyFromStore(final String name)
122 {
123 try
124 {
125 return properties.getProperty(name);
126 }
127 catch (final IllegalArgumentException e)
128 {
129 return new Property(properties.getSourceId(), name, null);
130 }
131 }
132
133 @Override
134 public final Property setPropertyToStore(final String key, final String value)
135 throws PropertyValidationException, ReadOnlyPropertyException
136 {
137 final Property property = ensureProperty(key, value);
138 final Property oldValue = properties.setProperty(property);
139 return oldValue;
140 }
141
142 @Override
143 protected final Property deletePropertyInStore(final String name)
144 {
145 return properties.removeProperty(name);
146 }
147
148 @Override
149 protected final PropertyCollection getPropertyCollectionFromStore()
150 {
151 return properties.getProperties();
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 public final AbstractInMemoryConfigurationProperties addClassPathProperties(
172 final Class<?> propertiesInterface) throws NullArgumentException,
173 ConfigurationLoadingException
174 {
175 Arguments.checkNotNull("propertiesInterface", propertiesInterface);
176
177 final Properties properties = loadDefault(propertiesInterface);
178 addDescriptors(propertiesInterface);
179 addDefinitions(properties);
180 return this;
181 }
182
183 private Properties loadDefault(final Class<?> descriptorDefinition)
184 throws ConfigurationLoadingException
185 {
186 final String name = descriptorDefinition.getSimpleName() + ".properties";
187
188 final InputStream in = descriptorDefinition.getResourceAsStream(name);
189 if (in != null)
190 {
191 final PropertiesHelper utils = new PropertiesHelper(getKey());
192 return utils.readProperties(name, in);
193 }
194
195 if (requiresDefaultOnClassPath)
196 {
197 throw new ConfigurationNotFoundException(getKey(), name);
198 }
199 else
200 {
201 return new Properties();
202 }
203 }
204
205 private Property ensureProperty(final String name, final Object value)
206 {
207 final Property property;
208 if (value instanceof Property)
209 {
210 property = (Property) value;
211 }
212 else
213 {
214 property = new Property(properties.getSourceId(), name, value);
215 }
216 return property;
217 }
218
219
220
221
222
223
224
225
226 @Override
227 public String toString()
228 {
229 return super.toString() + "Properties:\n" + properties.toString();
230 }
231 }