-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
ConfigurationImpl.java
480 lines (387 loc) · 14.5 KB
/
ConfigurationImpl.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* eXist Open Source Native XML Database
* Copyright (C) 2008-2013 The eXist Project
* http://exist-db.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
package org.exist.config;
import java.lang.ref.WeakReference;
import java.util.*;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.exist.dom.persistent.DocumentImpl;
import org.exist.security.PermissionDeniedException;
import org.exist.storage.DBBroker;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* configuration -> element
* property -> attribute
*
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
public class ConfigurationImpl implements Configuration {
private Map<String, Object> runtimeProperties = new HashMap<String, Object>();
protected WeakReference<Configurable> configuredObjectReference = null;
private Element element;
private ConfigurationImpl() {
//Nothing to do
}
protected ConfigurationImpl(final Element element) {
this.element = element;
}
@Override
public Element getElement() {
return element;
}
@Override
public String getName() {
return element.getLocalName();
}
@Override
public String getValue() {
return element.getNodeValue();
}
@Override
public Configuration getConfiguration(String name) {
if (element.getLocalName().equals(name)) {
return this;
}
final List<Configuration> list = getConfigurations(name);
if (list == null)
{return null;}
if (list.size() > 0)
{return list.get(0);}
return null;
}
@Override
public List<Configuration> getConfigurations(String name) {
final List<Configuration> list = new ArrayList<Configuration>();
Node child = element.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
final Element el = (Element)child;
if (name.equals( el.getLocalName() ) && NS.equals( el.getNamespaceURI() )) {
final Configuration config = new ConfigurationImpl(el);
list.add(config);
}
}
child = child.getNextSibling();
}
return list;
}
private Map<String, String> props = null;
private void cache() {
if (props != null)
return;
props = new HashMap<String, String>();
Set<String> names = new HashSet<String>();
Node child = element.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
if (NS.equals(child.getNamespaceURI())) {
String name = child.getLocalName();
if (names.contains(name)) {
if (props.containsKey(name)) {
props.remove(name);
}
} else {
props.put(name, child.getNodeValue());
names.add(name);
}
}
}
child = child.getNextSibling();
}
//load attributes values
NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node attr = attrs.item(i);
if ( !"xmlns".equals( attr.getPrefix() ) ) {
props.put(attr.getLocalName(), attr.getNodeValue());
}
}
}
public void clearCache() {
props = null;
}
@Override
public String getProperty(String name) {
cache();
return props.get(name);
// if (hasAttribute(name))
// {return getAttribute(name);}
// final NodeList nodes = getElementsByTagNameNS(NS, name);
// if (nodes.getLength() == 1) {
// return nodes.item(0).getNodeValue();
// }
// return null;
}
public String getProperty(String name, String default_property) {
final String property = getProperty(name);
if (property == null) {
return default_property;
}
return property;
}
@Override
public Map<String, String> getPropertyMap(String name) {
final Map<String, String> map = new HashMap<String, String>();
if (hasProperty(name)) {
map.put(name, getProperty(name));
return map;
}
Node child = element.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
final Element el = (Element) child;
if (name.equals( el.getLocalName() ) && NS.equals( el.getNamespaceURI() )) {
if(!el.hasAttributes()){
continue;
}
final NamedNodeMap attrs = el.getAttributes();
if (attrs.getLength() != 1) {
continue;
}
Node attr = attrs.getNamedItem("key");
if (attr == null)
continue;
final String key = attr.getNodeValue();
final String value = el.getNodeValue();
if(key == null || key.isEmpty() || value == null || value.isEmpty()){
; //skip
} else {
map.put(key, value);
}
}
}
child = child.getNextSibling();
}
return map;
// if(hasAttribute(name)) {
// return null;
// }
// final Map<String, String> map = new HashMap<String, String>();
// final NodeList nodes = getElementsByTagNameNS(NS, name);
// for(int i = 0; i < nodes.getLength(); i++) {
// final Node item = nodes.item(i);
// if(!item.hasAttributes()){
// return null;
// }
// final NamedNodeMap attrs = item.getAttributes();
// if(attrs.getLength() != 1){
// return null;
// }
// final String key = attrs.getNamedItem("key").getNodeValue();
// final String value = item.getNodeValue();
// if(value == null || value.isEmpty()){
// return null;
// }
// map.put(key, value);
// }
// return map;
}
@Override
public boolean hasProperty(String name) {
cache();
return props.containsKey(name);
// if (hasAttribute(name))
// {return true;}
// return (getElementsByTagName(name).getLength() == 1);
}
public Object getRuntimeProperty(String name) {
return runtimeProperties.get(name);
}
public boolean hasRuntimeProperty(String name) {
return runtimeProperties.containsKey(name);
}
public void setRuntimeProperty(String name, Object obj) {
runtimeProperties.put(name, obj);
}
@Override
public Boolean getPropertyBoolean(String name) {
final String value = getProperty(name);
if(value == null)
{return null;}
if ("yes".equalsIgnoreCase(value))
{return true;}
else if ("no".equalsIgnoreCase(value))
{return false;}
else if ("true".equalsIgnoreCase(value))
{return true;}
else if ("false".equalsIgnoreCase(value))
{return false;}
//???
return null;
}
public Boolean getPropertyBoolean(String name, boolean defaultValue) {
final String value = getProperty(name);
if(value == null)
{return Boolean.valueOf(defaultValue);}
return Boolean.valueOf("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value));
}
@Override
public Integer getPropertyInteger(String name) {
final String value = getProperty(name);
if (value == null)
{return null;}
return Integer.valueOf(value);
}
public Integer getPropertyInteger(String name, Integer defaultValue, boolean positive) {
final String value = getProperty(name);
if (value == null)
{return defaultValue;}
final Integer result = Integer.valueOf(value);
if ((positive) && (result < 0))
{return defaultValue.intValue();}
return result;
}
@Override
public Long getPropertyLong(String name) {
final String value = getProperty(name);
if (value == null)
{return null;}
return Long.valueOf(value);
}
public Long getPropertyLong(String name, Long defaultValue, boolean positive) {
final String value = getProperty(name);
if (value == null)
{return defaultValue;}
final long result = Long.valueOf(value);
if ((positive) && (result < 0))
{return defaultValue.longValue();}
return result;
}
public Integer getPropertyMegabytes(String name, Integer defaultValue) {
String cacheMem = element.getAttribute(name);
if (cacheMem != null) {
if (cacheMem.endsWith("M") || cacheMem.endsWith("m"))
{cacheMem = cacheMem.substring(0, cacheMem.length() - 1);}
final Integer result = new Integer(cacheMem);
if (result < 0)
{return defaultValue;}
return result;
}
return defaultValue;
}
public String getConfigFilePath() {
return "";//XXX: put config url
}
@Override
public Set<String> getProperties() {
final Set<String> properties = new HashSet<String>();
final NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
//ignore namespace declarations
if ( !"xmlns".equals( attrs.item(i).getPrefix() ) )
{properties.add(attrs.item(i).getNodeName());}
}
final NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
properties.add(child.getNodeName());
}
}
return properties;
}
//related objects
Map<String, Object> objects = null;
@Override
public synchronized Object putObject(String name, Object object) {
if (objects == null)
objects = new HashMap<String, Object>();
return objects.put(name, object);
}
@Override
public synchronized Object getObject(String name) {
if (objects == null)
return null;
return objects.get(name);
}
private boolean saving = false;
@Override
public void checkForUpdates(Element element) {
if (!saving && configuredObjectReference != null && configuredObjectReference.get() != null) {
clearCache();
this.element = element;
Configurator.configure(configuredObjectReference.get(), this);
}
}
@Override
public void save() throws PermissionDeniedException, ConfigurationException {
//ignore in-memory nodes
if (element instanceof org.exist.dom.memtree.ElementImpl) {
return;
}
synchronized (this) {
try {
saving = true;
if (configuredObjectReference != null && configuredObjectReference.get() != null)
{Configurator.save(
configuredObjectReference.get(),
((DocumentImpl)element.getOwnerDocument()).getURI()
);}
} catch (final Exception e) {
throw new ConfigurationException(e.getMessage(), e);
} finally {
saving = false;
}
}
}
@Override
public void save(final DBBroker broker) throws PermissionDeniedException, ConfigurationException {
//ignore in-memory nodes
if (element instanceof org.exist.dom.memtree.ElementImpl) {
return;
}
synchronized(this) {
try {
saving = true;
if (configuredObjectReference != null && configuredObjectReference.get() != null)
{Configurator.save(broker,
configuredObjectReference.get(),
((DocumentImpl) element.getOwnerDocument()).getURI()
);}
} catch (final Exception e) {
throw new ConfigurationException(e.getMessage(), e);
} finally {
saving = false;
}
}
}
@Override
public boolean equals(final Object obj) {
return equals(obj, Optional.empty());
}
@Override
public boolean equals(final Object obj, final Optional<String> property) {
if (obj instanceof ConfigurationImpl) {
final ConfigurationImpl conf = (ConfigurationImpl)obj;
if (!(getName().equals(conf.getName()))) {
return false;
}
final String name = property.orElse(Configuration.ID);
final Optional<String> value = Optional.ofNullable(getProperty(name));
return value.map(v -> v.equals(conf.getProperty(name))).orElse(false);
}
return false;
}
}