1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.settings4j.connector;
18
19 import java.util.prefs.BackingStoreException;
20 import java.util.prefs.Preferences;
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 public class PreferencesConnector extends AbstractPropertyConnector {
47
48 private final Preferences systemPrefs;
49
50 private final Preferences userPrefs;
51
52
53
54
55
56 public PreferencesConnector() {
57 super();
58 this.systemPrefs = Preferences.systemRoot();
59 this.userPrefs = Preferences.userRoot();
60 }
61
62
63
64 @Override
65 protected String getProperty(final String keyPath, final String defaultValue) {
66 final String normalizedKey = normalizeKey(keyPath);
67 final String path = getPath(normalizedKey);
68 final String key = getKey(normalizedKey);
69 String value = getPreferenceValue(path, key, defaultValue, this.userPrefs);
70 if (value == null) {
71 value = getPreferenceValue(path, key, defaultValue, this.systemPrefs);
72 }
73 return value;
74 }
75
76
77
78 private String getPath(final String keyPath) {
79 String path = null;
80 final int endOfPath = keyPath.lastIndexOf('/');
81 if (endOfPath != -1) {
82 path = keyPath.substring(0, endOfPath);
83 }
84 return path;
85 }
86
87
88
89
90
91
92
93
94
95
96 protected String getPreferenceValue(final String path, final String key, final String defaultValue,
97 final Preferences preferences) {
98 if (path != null) {
99
100 try {
101 if (preferences.nodeExists(path)) {
102 return preferences.node(path).get(key, defaultValue);
103 }
104 return defaultValue;
105 } catch (final BackingStoreException e) {
106 throw new RuntimeException("Cannot access specified node path [" + path + "]", e);
107 }
108 }
109 return preferences.get(key, defaultValue);
110 }
111
112
113
114
115
116
117
118
119 public void setString(final String keyPath, final String value) {
120 final String normalizedKey = normalizeKey(keyPath);
121 final String path = getPath(normalizedKey);
122 final String key = getKey(normalizedKey);
123 setPreferenceValue(path, key, value, this.userPrefs);
124 }
125
126
127
128
129
130
131
132 public void setSystemString(final String keyPath, final String value) {
133 final String normalizedKey = normalizeKey(keyPath);
134 final String path = getPath(normalizedKey);
135 final String key = getKey(normalizedKey);
136 setPreferenceValue(path, key, value, this.systemPrefs);
137 }
138
139
140
141
142
143
144
145
146
147 protected void setPreferenceValue(final String path, final String key, final String value,
148 final Preferences preferences) {
149 if (path != null) {
150 preferences.node(path).put(key, value);
151 } else {
152 preferences.put(key, value);
153 }
154 try {
155 preferences.flush();
156 } catch (BackingStoreException e) {
157 throw new RuntimeException("Cannot access specified node path [" + path + "]", e);
158 }
159 }
160
161 private String getKey(final String keyPath) {
162 String key = keyPath;
163 final int endOfPath = keyPath.lastIndexOf('/');
164 if (endOfPath != -1) {
165 key = keyPath.substring(endOfPath + 1);
166 }
167 return key;
168 }
169
170
171 private String normalizeKey(final String key) {
172 if (key == null) {
173 return null;
174 }
175 String normalizeKey = key;
176
177 normalizeKey = normalizeKey.replace('\\', '/');
178
179 if (normalizeKey.startsWith("/")) {
180 normalizeKey = normalizeKey.substring(1);
181 }
182 return normalizeKey;
183 }
184 }