1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.admin.domain.model;
17
18 import java.io.Serializable;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Set;
22 import java.util.TreeSet;
23
24 import javax.annotation.CheckForNull;
25
26 import de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader;
27 import de.smartics.properties.admin.domain.service.ConfigurationService;
28 import de.smartics.properties.admin.domain.service.RemoteConfigurationPropertiesManagementFactory;
29 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
30 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
31 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
32 import de.smartics.properties.api.core.security.PropertyValueSecurity;
33 import de.smartics.properties.impl.config.ds.DataSourceConfiguration;
34 import de.smartics.properties.impl.config.ds.DataSourceConfigurationLoader;
35 import de.smartics.properties.resource.domain.ArtifactId;
36 import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContext;
37 import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager;
38
39
40
41
42 public class ManagedApplication implements Serializable
43 {
44
45
46
47
48
49
50
51
52
53 private static final long serialVersionUID = 1L;
54
55
56
57
58
59
60
61 private static ManagedApplication instance;
62
63
64
65
66
67
68 private final ArtifactId applicationId;
69
70
71
72
73
74
75 private final DataSourceConfiguration dataSourceConfig;
76
77
78
79
80
81
82 private final RemoteConfigurationPropertiesManagementFactory factory;
83
84
85
86
87 @SuppressWarnings({ "unchecked", "rawtypes" })
88 private final Set<ConfigurationKey<?>> configurationKeys = Collections
89 .synchronizedSet(new TreeSet());
90
91
92
93 private static void initialize()
94 throws CannotInitializeManagedApplicationException
95 {
96 try
97 {
98 final ArtifactId applicationId =
99 new ApplicationIdConfigurationLoader().load();
100 final DataSourceConfiguration dataSourceConfig =
101 loadDataSourceConfiguration();
102 final RemoteConfigurationPropertiesManagementFactory factory =
103 new ConfigurationService().createFactory(applicationId,
104 dataSourceConfig);
105 instance =
106 new ManagedApplication(applicationId, dataSourceConfig, factory);
107 }
108 catch (final Exception e)
109 {
110 throw new CannotInitializeManagedApplicationException(e);
111 }
112 }
113
114 private static DataSourceConfiguration loadDataSourceConfiguration()
115 {
116 try
117 {
118 return new DataSourceConfigurationLoader().load();
119 }
120 catch (final IllegalStateException e)
121 {
122 return null;
123 }
124 }
125
126
127
128
129
130
131
132
133
134
135
136 public ManagedApplication(final ArtifactId applicationId,
137 final DataSourceConfiguration dataSourceConfig,
138 final RemoteConfigurationPropertiesManagementFactory factory)
139 {
140 this.applicationId = applicationId;
141 this.dataSourceConfig = dataSourceConfig;
142 this.factory = factory;
143 configurationKeys.addAll(factory.getRegisteredConfigurationKeys());
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public ArtifactId getApplicationId()
160 {
161 return applicationId;
162 }
163
164
165
166
167
168
169 @CheckForNull
170 public DataSourceConfiguration getDataSourceConfig()
171 {
172 return dataSourceConfig;
173 }
174
175
176
177
178
179
180 public String getRemoteRepositoryUrl()
181 {
182 return factory.getRemoteUrl();
183 }
184
185
186
187
188
189
190 public PropertyValueSecurity getSecurity()
191 {
192 return factory.getFactoryConfiguration().getDecrypter();
193 }
194
195
196
197
198
199
200 public PropertyDescriptorRegistry getDescriptorRegistry()
201 {
202 return factory.getRegistry();
203 }
204
205
206
207
208
209
210
211
212
213
214 public static ManagedApplication getApplication()
215 throws CannotInitializeManagedApplicationException
216 {
217 if (instance == null)
218 {
219 initialize();
220 }
221 return instance;
222 }
223
224
225
226
227
228
229
230 public ConfigurationPropertiesManagement getConfiguration(
231 final ConfigurationKey<?> key)
232 {
233 final ConfigurationPropertiesManagement config = factory.create(key);
234 configurationKeys.add(key);
235 return config;
236 }
237
238
239
240
241
242
243
244
245 public ConfigurationPropertiesManagement getConfiguration(
246 final String keyString)
247 {
248 final ConfigurationKeyContext context =
249 ConfigurationKeyContextManager.INSTANCE.context();
250 final ConfigurationKey<?> key =
251 context.configurationKeyFactory().createKeyFromString(keyString);
252 final ConfigurationPropertiesManagement config = getConfiguration(key);
253 return config;
254 }
255
256
257
258
259
260
261 public Collection<ConfigurationKey<?>> getConfigurationKeys()
262 {
263 return Collections.unmodifiableCollection(configurationKeys);
264 }
265
266
267
268
269 public void clean()
270 {
271 for (final ConfigurationKey<?> key : factory
272 .getRegisteredConfigurationKeys())
273 {
274 factory.remove(key);
275 }
276
277 configurationKeys.clear();
278
279 factory.release();
280
281 instance = null;
282 }
283
284
285 }