/
ConfigImpl.java
414 lines (364 loc) · 12.9 KB
/
ConfigImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* $Id$
*
* Copyright 2006 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
/*------------------------------------------------------------------------------
*
* Written by: Josh Moore <josh.moore@gmx.de>
*
*------------------------------------------------------------------------------
*/
package ome.logic;
import java.io.File;
import java.io.FileReader;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ome.annotations.PermitAll;
import ome.annotations.RolesAllowed;
import ome.api.IConfig;
import ome.api.ServiceInterface;
import ome.api.local.LocalConfig;
import ome.conditions.ApiUsageException;
import ome.conditions.InternalException;
import ome.conditions.OptimisticLockException;
import ome.conditions.SecurityViolation;
import ome.security.basic.CurrentDetails;
import ome.services.db.DatabaseIdentity;
import ome.system.PreferenceContext;
import ome.util.SqlAction;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.transaction.annotation.Transactional;
/**
* implementation of the IConfig service interface.
*
* Also used as the main developer example for developing (stateless) ome.logic
* implementations. See source code documentation for more.
*
* @author Josh Moore, josh.moore at gmx.de
* @version $Revision$, $Date$
* @since 3.0-M3
* @see IConfig
*/
/*
* Developer notes: --------------- The two annotations below are activated by
* setting the subversion properties on this class file. They can be accessed
* via ome.system.Version
*/
/*
* Developer notes: --------------- The annotations below (and on the individual
* methods) are central to the definition of this service. They are used in
* place of XML configuration files (though the XML deployment descriptors, as
* they are called, can be used to override the annotations), and will influence
*/
// ~ Service annotations
// =============================================================================
/*
* Source: Spring Purpose: Used by EventHandler#checkReadyOnly(MethodInvocation)
* to deteremine if a method is read-only. No annotation implies ready-only, so
* it is essential to have this annotation on all write methods.
*/
@Transactional
/*
* Stateless. This class implements ServiceInterface but not
* StatefulServiceInterface making it stateless. This means that the entire
* server will most likely only contain one of these instances. No mutable
* fields should be present unlessvery carefully synchronized.
*
* Local configurations are not exposed to clients, and are typically only used
* within a server instance.
*/
public class ConfigImpl extends AbstractLevel2Service implements LocalConfig {
/*
* Stateful differences: -------------------- A stateful service must be
* marked as Serializable and all fields must be either marked transient, be
* serializable themselves, or be set to null before serialization. Here
* we've marked the jdbc field as transient out of habit.
*
* @see https://trac.openmicroscopy.org/ome/ticket/173
*/
private transient SqlAction sql;
private transient PreferenceContext prefs;
private transient CurrentDetails currentDetails;
private transient DatabaseIdentity db;
/**
* Protects all access to the configuration properties.
*/
private final transient ReadWriteLock lock = new ReentrantReadWriteLock();
/**
* {@link SqlAction} setter for dependency injection.
*
* @param sql the SQL action instance
* @see ome.services.util.BeanHelper#throwIfAlreadySet(Object, Object)
*/
/*
* Developer notes: --------------- Because of the complicated lifecycle of
* EJBs it is not possible to fully configure them with constructor
* injection (which is safer). Instead, we have to provide public setters
* for all properties which need to be injected. And since Java doesn't have
* the concept of "friends" (yet), this opens up our classes for some weird
* manipulations. Therefore we've made all bean setters "final" and added a
* call to "throwIfAlreadySet" which will only allow previously null fields
* to be set.
*/
public final void setSqlAction(SqlAction sql) {
getBeanHelper().throwIfAlreadySet(this.sql, sql);
this.sql = sql;
}
/**
* {@link PreferenceContext} setter for dependency injection.
*
* @param prefs the preference context
* @see ome.services.util.BeanHelper#throwIfAlreadySet(Object, Object)
*/
public final void setPreferenceContext(PreferenceContext prefs) {
getBeanHelper().throwIfAlreadySet(this.prefs, prefs);
this.prefs = prefs;
}
/**
* {@link CurrentDetails} setter for dependency injection.
*
* @param currentDetails the details of the current thread's security context
* @see ome.services.util.BeanHelper#throwIfAlreadySet(Object, Object)
*/
public final void setCurrentDetails(CurrentDetails currentDetails) {
getBeanHelper().throwIfAlreadySet(this.currentDetails, currentDetails);
this.currentDetails = currentDetails;
}
public final void setDatabaseIdentity(DatabaseIdentity db) {
this.db = db;
}
/*
* Developer notes: --------------- This method provides the lookup value
* needed for finding services within the Spring context and, by convention,
* the value which is to be returned can be found in the file
* "ome/services/service-<class name>.xml"
*/
public final Class<? extends ServiceInterface> getServiceInterface() {
return IConfig.class;
}
// ~ Service methods
// =========================================================================
/*
* Source: ome.annotations package
*
* Purpose: as opposed to RolesAllowed (below), permits this method to be
* called by anyone, regardless of their group membership. As of the move to
* OmeroBlitz, the user will still have to have created a session to gain
* access (unlike under JavaEE).
*/
/**
* see {@link IConfig#getServerTime()}
*/
@PermitAll
public Date getServerTime() {
return new Date();
}
/**
* see {@link IConfig#getDatabaseTime()}
*/
@PermitAll
// see above
public Date getDatabaseTime() {
Date date = sql.now();
return date;
}
/*
* Source: ome.annotations package
*
* Purpose: defines the role which must have been obtained during
* authentication and authorization in order to access this method. This
* works in combination with the BasicMethodSecurity to fully define
* security semantics.
*/
/**
* see {@link IConfig#getConfigValue(String)}
*/
@PermitAll
// see above
public String getConfigValue(String key) {
if (key == null) {
return "";
}
key = prefs.resolveAlias(key);
if (!prefs.canRead(currentDetails.getCurrentEventContext(), key)) {
throw new SecurityViolation("Cannot read configuration: " + key);
}
return getInternalValue(key);
}
@PermitAll
public Map<String, String> getConfigValues(String keyRegex) {
if (keyRegex == null) {
return Collections.emptyMap();
}
Pattern p = Pattern.compile(keyRegex);
Map<String, String> rv = new HashMap<String, String>();
Set<String> keys = prefs.getKeySet();
// Not resolving aliases since these come straight-from the prefs
for (String key : keys) {
if (p.matcher(key).find()) {
if (prefs.canRead(
currentDetails.getCurrentEventContext(), key)) {
rv.put(key, getInternalValue(key));
}
}
}
return rv;
}
@RolesAllowed("system")
public Map<String, String> getConfigDefaults() {
File etc = new File("etc");
File omero = new File(etc, "omero.properties");
Properties p = new Properties();
Map<String, String> rv = new HashMap<String, String>();
try {
FileReader r = new FileReader(omero);
p.load(r);
for (Entry<Object, Object> entry : p.entrySet()) {
rv.put(entry.getKey().toString(),
entry.getValue().toString());
}
return rv;
} catch (Exception e) {
InternalException ie = new InternalException(e.getMessage());
ie.initCause(e);
throw ie;
}
}
@PermitAll
public Map<String, String> getClientConfigValues() {
return getConfigValues("^omero\\.client\\.");
}
@PermitAll
public Map<String, String> getClientConfigDefaults() {
Map<String, String> rv = getConfigDefaults();
Map<String, String> copy = new HashMap<String, String>();
for (Map.Entry<String, String> e : rv.entrySet()) {
if (e.getKey().startsWith("omero.client")) {
copy.put(e.getKey(), e.getValue());
}
}
return copy;
}
public String getInternalValue(String key) {
key = prefs.resolveAlias(key);
lock.readLock().lock();
try {
String value = null;
if (prefs.checkDatabase(key)) {
value = fromDatabase(key);
}
if (value != null) {
return value;
} else {
return prefs.getProperty(key);
}
} finally {
lock.readLock().unlock();
}
}
/**
* see {@link IConfig#setConfigValue(String, String)}
*/
@RolesAllowed("system")
// see above
public void setConfigValue(String key, String value) {
key = prefs.resolveAlias(key);
lock.writeLock().lock();
try {
boolean set = false;
// If the value comes from the db, then set it there and return
if (prefs.checkDatabase(key)) {
String current = fromDatabase(key);
if (current != null && current.length() > 0) {
int count = sql.updateConfiguration(key, value);
if (count != 1) {
throw new OptimisticLockException(
"Configuration tabled during modification of : "
+ key);
}
set = true;
}
}
// Otherwise set it in the preferences.
if (!set) {
// With #800, we will need to raise some form of event here
// to notify consumers. Also, this will be a temporary value
// not saved on restart nor shared across JVMs.
System.setProperty(key, value);
}
} finally {
lock.writeLock().unlock();
}
}
/**
* see {@link IConfig#setConfigValueIfEquals(String, String, String)}
*/
@RolesAllowed("user")
// see above
public boolean setConfigValueIfEquals(String key, String value, String test)
throws ApiUsageException, SecurityViolation {
key = prefs.resolveAlias(key);
lock.writeLock().lock();
try {
String current = getInternalValue(key);
if (test == null) {
if (current != null) {
return false;
}
} else {
if (!test.equals(current)) {
return false;
}
}
setConfigValue(key, value);
return true;
} finally {
lock.writeLock().unlock();
}
}
/**
* see {@link IConfig#getVersion()}
*/
@PermitAll
// see above
public String getVersion() {
String version = getInternalValue("omero.version");
Pattern p = Pattern.compile(VERSION_REGEX);
Matcher m = p.matcher(version);
if (!m.matches()) {
throw new InternalException("Bad version format:" + version);
}
return m.group(1);
}
@PermitAll
// see above
public String getDatabaseVersion() {
return sql.dbVersion(); }
@PermitAll
// see above
public String getDatabaseUuid() {
return db.getUuid();
}
// Helpers
// =========================================================================
private String fromDatabase(String key) {
String value = null;
try {
value = sql.configValue(key);
} catch (EmptyResultDataAccessException erdae) {
// ok returning null
}
return value;
}
}