1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.preference;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.resource.OceanusBundleId;
21 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceKey;
22 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceSet;
23
24
25
26
27 public abstract class PrometheusPreferenceSet
28 extends MetisPreferenceSet {
29
30
31
32 private PrometheusPreferenceSecurity theSecurity;
33
34
35
36
37
38
39
40
41 protected PrometheusPreferenceSet(final PrometheusPreferenceManager pManager,
42 final OceanusBundleId pId) throws OceanusException {
43 this(pManager, pId.getValue());
44 }
45
46
47
48
49
50
51
52
53 protected PrometheusPreferenceSet(final PrometheusPreferenceManager pManager,
54 final String pName) throws OceanusException {
55
56 super(pManager, pName);
57 }
58
59 @Override
60 public PrometheusPreferenceManager getPreferenceManager() {
61 return (PrometheusPreferenceManager) super.getPreferenceManager();
62 }
63
64
65
66
67
68
69
70 public PrometheusPreferenceSecurity getSecurity() throws OceanusException {
71 if (theSecurity == null) {
72 theSecurity = getPreferenceManager().getSecurity();
73 }
74 return theSecurity;
75 }
76
77
78
79
80
81
82
83 protected PrometheusByteArrayPreference defineByteArrayPreference(final MetisPreferenceKey pKey) {
84
85 final PrometheusByteArrayPreference myPref = new PrometheusByteArrayPreference(this, pKey);
86
87
88 definePreference(myPref);
89
90
91 return myPref;
92 }
93
94
95
96
97
98
99
100
101 protected PrometheusCharArrayPreference defineCharArrayPreference(final MetisPreferenceKey pKey) throws OceanusException {
102
103 final PrometheusCharArrayPreference myPref = new PrometheusCharArrayPreference(this, pKey);
104
105
106 definePreference(myPref);
107
108
109 return myPref;
110 }
111
112
113
114
115
116
117
118 public PrometheusByteArrayPreference getByteArrayPreference(final MetisPreferenceKey pKey) {
119
120 final MetisPreferenceItem myPref = getPreference(pKey);
121
122
123 if (myPref == null) {
124 throw new IllegalArgumentException(ERROR_UNKNOWN
125 + pKey);
126 }
127
128
129 if (!(myPref instanceof PrometheusByteArrayPreference)) {
130 throw new IllegalArgumentException(ERROR_INVALID
131 + pKey);
132 }
133
134
135 return (PrometheusByteArrayPreference) myPref;
136 }
137
138
139
140
141
142
143
144 public byte[] getByteArrayValue(final MetisPreferenceKey pKey) {
145
146 final PrometheusByteArrayPreference myPref = getByteArrayPreference(pKey);
147
148
149 return myPref.getValue();
150 }
151
152
153
154
155
156
157
158 public PrometheusCharArrayPreference getCharArrayPreference(final MetisPreferenceKey pKey) {
159
160 final MetisPreferenceItem myPref = getPreference(pKey);
161
162
163 if (myPref == null) {
164 throw new IllegalArgumentException(ERROR_UNKNOWN
165 + pKey);
166 }
167
168
169 if (!(myPref instanceof PrometheusCharArrayPreference)) {
170 throw new IllegalArgumentException(ERROR_INVALID
171 + pKey);
172 }
173
174
175 return (PrometheusCharArrayPreference) myPref;
176 }
177
178
179
180
181
182
183
184 public char[] getCharArrayValue(final MetisPreferenceKey pKey) {
185
186 final PrometheusCharArrayPreference myPref = getCharArrayPreference(pKey);
187
188
189 return myPref.getValue();
190 }
191
192
193
194
195 public static class PrometheusByteArrayPreference
196 extends MetisPreferenceItem {
197
198
199
200
201
202
203 protected PrometheusByteArrayPreference(final PrometheusPreferenceSet pSet,
204 final MetisPreferenceKey pKey) {
205
206 super(pSet, pKey, PrometheusPreferenceType.BYTEARRAY);
207
208
209 if (pSet.checkExists(pKey)) {
210
211 final byte[] myValue = getHandle().getByteArray(getPreferenceName(), null);
212
213
214 setTheValue(myValue);
215 }
216 }
217
218 @Override
219 public byte[] getValue() {
220 return (byte[]) super.getValue();
221 }
222
223
224
225
226
227
228 public void setValue(final byte[] pNewValue) {
229 setNewValue(pNewValue);
230 }
231
232 @Override
233 protected void storeThePreference(final Object pNewValue) {
234 getHandle().putByteArray(getPreferenceName(), (byte[]) pNewValue);
235 }
236 }
237
238
239
240
241 public static class PrometheusCharArrayPreference
242 extends MetisPreferenceItem {
243
244
245
246
247
248
249
250 protected PrometheusCharArrayPreference(final PrometheusPreferenceSet pSet,
251 final MetisPreferenceKey pKey) throws OceanusException {
252
253 super(pSet, pKey, PrometheusPreferenceType.CHARARRAY);
254
255
256 if (pSet.checkExists(pKey)) {
257
258 final byte[] myBytes = getHandle().getByteArray(getPreferenceName(), null);
259 final PrometheusPreferenceSecurity mySecurity = getSet().getSecurity();
260
261
262 final char[] myValue = myBytes == null
263 ? null
264 : mySecurity.decryptValue(myBytes);
265
266
267 setTheValue(myValue);
268 }
269 }
270
271 @Override
272 protected PrometheusPreferenceSet getSet() {
273 return (PrometheusPreferenceSet) super.getSet();
274 }
275
276 @Override
277 public char[] getValue() {
278 return (char[]) super.getValue();
279 }
280
281
282
283
284
285
286 public void setValue(final char[] pNewValue) {
287 setNewValue(pNewValue);
288 }
289
290 @Override
291 protected void storeThePreference(final Object pNewValue) throws OceanusException {
292
293 final PrometheusPreferenceSecurity mySecurity = getSet().getSecurity();
294 getHandle().putByteArray(getPreferenceName(), mySecurity.encryptValue((char[]) pNewValue));
295 }
296 }
297 }