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