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