View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * Prometheus PreferenceSet.
28   */
29  public abstract class PrometheusPreferenceSet
30          extends MetisPreferenceSet {
31      /**
32       * The Encryptor.
33       */
34      private PrometheusPreferenceEncryptor theEncryptor;
35  
36      /**
37       * Constructor.
38       *
39       * @param pParams the parameters
40       * @param pId     the Id
41       * @throws OceanusException on error
42       */
43      protected PrometheusPreferenceSet(final MetisPreferenceParams pParams,
44                                        final OceanusBundleId pId) throws OceanusException {
45          this(pParams, pId.getValue());
46      }
47  
48      /**
49       * Constructor.
50       *
51       * @param pParams the parameters
52       * @param pName   the name
53       * @throws OceanusException on error
54       */
55      protected PrometheusPreferenceSet(final MetisPreferenceParams pParams,
56                                        final String pName) throws OceanusException {
57          /* Initialize super-class */
58          super(pParams, pName);
59      }
60  
61      @Override
62      protected MetisViewerEntry defineViewerEntry(final MetisPreferenceParams pParams) {
63          /* Access security if available */
64          if (pParams instanceof PrometheusPreferenceParams myParams) {
65              theEncryptor = myParams.getEncryptor();
66          }
67  
68          /* Create the viewer record */
69          return super.defineViewerEntry(pParams);
70      }
71  
72      /**
73       * Obtain the encryptor.
74       *
75       * @return the encryptor
76       */
77      public PrometheusPreferenceEncryptor getEncryptor() {
78          return theEncryptor;
79      }
80  
81      /**
82       * Define new ByteArray preference.
83       *
84       * @param pKey the key for the preference
85       * @return the preference item
86       */
87      protected PrometheusByteArrayPreference defineByteArrayPreference(final MetisPreferenceKey pKey) {
88          /* Define the preference */
89          final PrometheusByteArrayPreference myPref = new PrometheusByteArrayPreference(this, pKey);
90  
91          /* Add it to the list of preferences */
92          definePreference(myPref);
93  
94          /* Return the preference */
95          return myPref;
96      }
97  
98      /**
99       * Define new CharArray preference.
100      *
101      * @param pKey the key for the preference
102      * @return the preference item
103      * @throws OceanusException on error
104      */
105     protected PrometheusCharArrayPreference defineCharArrayPreference(final MetisPreferenceKey pKey) throws OceanusException {
106         /* Define the preference */
107         final PrometheusCharArrayPreference myPref = new PrometheusCharArrayPreference(this, pKey);
108 
109         /* Add it to the list of preferences */
110         definePreference(myPref);
111 
112         /* Return the preference */
113         return myPref;
114     }
115 
116     /**
117      * Obtain ByteArray preference.
118      *
119      * @param pKey the key of the preference
120      * @return the ByteArray preference
121      */
122     public PrometheusByteArrayPreference getByteArrayPreference(final MetisPreferenceKey pKey) {
123         /* Access preference */
124         final MetisPreferenceItem myPref = getPreference(pKey);
125 
126         /* Reject if not found */
127         if (myPref == null) {
128             throw new IllegalArgumentException(ERROR_UNKNOWN
129                     + pKey);
130         }
131 
132         /* Reject if wrong type */
133         if (!(myPref instanceof PrometheusByteArrayPreference)) {
134             throw new IllegalArgumentException(ERROR_INVALID
135                     + pKey);
136         }
137 
138         /* Return the preference */
139         return (PrometheusByteArrayPreference) myPref;
140     }
141 
142     /**
143      * Obtain ByteArray value.
144      *
145      * @param pKey the key of the preference
146      * @return the ByteArray value
147      */
148     public byte[] getByteArrayValue(final MetisPreferenceKey pKey) {
149         /* Access preference */
150         final PrometheusByteArrayPreference myPref = getByteArrayPreference(pKey);
151 
152         /* Return the value */
153         return myPref.getValue();
154     }
155 
156     /**
157      * Obtain CharArray preference.
158      *
159      * @param pKey the key of the preference
160      * @return the CharArray preference
161      */
162     public PrometheusCharArrayPreference getCharArrayPreference(final MetisPreferenceKey pKey) {
163         /* Access preference */
164         final MetisPreferenceItem myPref = getPreference(pKey);
165 
166         /* Reject if not found */
167         if (myPref == null) {
168             throw new IllegalArgumentException(ERROR_UNKNOWN
169                     + pKey);
170         }
171 
172         /* Reject if wrong type */
173         if (!(myPref instanceof PrometheusCharArrayPreference)) {
174             throw new IllegalArgumentException(ERROR_INVALID
175                     + pKey);
176         }
177 
178         /* Return the preference */
179         return (PrometheusCharArrayPreference) myPref;
180     }
181 
182     /**
183      * Obtain CharArray value.
184      *
185      * @param pKey the key of the preference
186      * @return the CharArray value
187      */
188     public char[] getCharArrayValue(final MetisPreferenceKey pKey) {
189         /* Access preference */
190         final PrometheusCharArrayPreference myPref = getCharArrayPreference(pKey);
191 
192         /* Return the value */
193         return myPref.getValue();
194     }
195 
196     /**
197      * ByteArray preference.
198      */
199     public static class PrometheusByteArrayPreference
200             extends MetisPreferenceItem {
201         /**
202          * Constructor.
203          *
204          * @param pSet the preference Set
205          * @param pKey the key of the preference
206          */
207         protected PrometheusByteArrayPreference(final PrometheusPreferenceSet pSet,
208                                                 final MetisPreferenceKey pKey) {
209             /* Store name */
210             super(pSet, pKey, PrometheusPreferenceType.BYTEARRAY);
211 
212             /* Check whether we have an existing value */
213             if (pSet.checkExists(pKey)) {
214                 /* Access the value */
215                 final byte[] myValue = getHandle().getByteArray(getPreferenceName(), null);
216 
217                 /* Set as initial value */
218                 setTheValue(myValue);
219             }
220         }
221 
222         @Override
223         public byte[] getValue() {
224             return (byte[]) super.getValue();
225         }
226 
227         /**
228          * Set value.
229          *
230          * @param pNewValue the new value
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      * CharArray preference.
244      */
245     public static class PrometheusCharArrayPreference
246             extends MetisPreferenceItem {
247         /**
248          * Constructor.
249          *
250          * @param pSet the preference Set
251          * @param pKey the key of the preference
252          * @throws OceanusException on error
253          */
254         protected PrometheusCharArrayPreference(final PrometheusPreferenceSet pSet,
255                                                 final MetisPreferenceKey pKey) throws OceanusException {
256             /* Store name */
257             super(pSet, pKey, PrometheusPreferenceType.CHARARRAY);
258 
259             /* Check whether we have an existing value */
260             if (pSet.checkExists(pKey)) {
261                 /* Access the value */
262                 final byte[] myBytes = getHandle().getByteArray(getPreferenceName(), null);
263                 final PrometheusPreferenceEncryptor mySecurity = getSet().getEncryptor();
264 
265                 /* Decrypt the value */
266                 final char[] myValue = myBytes == null
267                         ? null
268                         : mySecurity.decryptValue(myBytes);
269 
270                 /* Set as initial value */
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          * Set value.
287          *
288          * @param pNewValue the new value
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             /* Store the value */
297             final PrometheusPreferenceEncryptor mySecurity = getSet().getEncryptor();
298             getHandle().putByteArray(getPreferenceName(), mySecurity.encryptValue((char[]) pNewValue));
299         }
300     }
301 }