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
|
/*******************************************************************************
* Copyright (c) 2011 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.orcs.core.internal;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.core.SystemPreferences;
import org.eclipse.osee.orcs.core.ds.DataStoreConstants;
import org.eclipse.osee.orcs.core.ds.KeyValueDataAccessor;
/**
* @author Roberto E. Escobar
*/
public class SystemPreferencesImpl implements SystemPreferences {
private static Map<String, Pair<Long, String>> cache;
private KeyValueDataAccessor accessor;
public void setDataAccessor(KeyValueDataAccessor accessor) {
this.accessor = accessor;
}
public void start() {
cache = new ConcurrentHashMap<String, Pair<Long, String>>();
}
public void stop() {
cache = null;
}
@Override
public String getSystemUuid() throws OseeCoreException {
return getValue(DataStoreConstants.DATASTORE_ID_KEY);
}
@Override
public String getValue(String key) throws OseeCoreException {
String toReturn = accessor.getValue(key);
cacheValue(key, toReturn);
return toReturn;
}
@Override
public String getCachedValue(String key) throws OseeCoreException {
return getCachedValue(key, Integer.MAX_VALUE);
}
@Override
public String getCachedValue(String key, long maxStaleness) throws OseeCoreException {
Pair<Long, String> pair = cache.get(key);
String value;
if (pair == null || pair.getFirst() + maxStaleness < System.currentTimeMillis()) {
value = getValue(key);
} else {
value = pair.getSecond();
}
return value;
}
@Override
public boolean isEnabled(String key) throws OseeCoreException {
return isBoolean(key);
}
@Override
public boolean isCacheEnabled(String key) throws OseeCoreException {
String dbProperty = getCachedValue(key);
if (Strings.isValid(dbProperty)) {
return dbProperty.equals("true");
}
return false;
}
@Override
public void setEnabled(String key, boolean enabled) throws OseeCoreException {
setBoolean(key, enabled);
}
@Override
public void setBoolean(String key, boolean value) throws OseeCoreException {
putValue(key, String.valueOf(value));
}
@Override
public boolean isBoolean(String key) throws OseeCoreException {
String dbProperty = getValue(key);
if (Strings.isValid(dbProperty)) {
return dbProperty.equals("true");
}
return false;
}
@Override
public boolean isBooleanUsingCache(String key) throws OseeCoreException {
return isCacheEnabled(key);
}
@Override
public void putValue(String key, String value) throws OseeCoreException {
accessor.putValue(key, value);
cacheValue(key, value);
}
@Override
public Set<String> getKeys() throws OseeCoreException {
return accessor.getKeys();
}
private static void cacheValue(String key, String value) {
Long time = System.currentTimeMillis();
cache.put(key, new Pair<Long, String>(time, value));
}
}
|