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.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   * Prometheus PreferenceSet.
26   */
27  public abstract class PrometheusPreferenceSet
28          extends MetisPreferenceSet {
29      /**
30       * The Security Manager.
31       */
32      private PrometheusPreferenceSecurity theSecurity;
33  
34      /**
35       * Constructor.
36       *
37       * @param pManager the preference manager
38       * @param pId      the Id
39       * @throws OceanusException on error
40       */
41      protected PrometheusPreferenceSet(final PrometheusPreferenceManager pManager,
42                                        final OceanusBundleId pId) throws OceanusException {
43          this(pManager, pId.getValue());
44      }
45  
46      /**
47       * Constructor.
48       *
49       * @param pManager the preference manager
50       * @param pName    the name
51       * @throws OceanusException on error
52       */
53      protected PrometheusPreferenceSet(final PrometheusPreferenceManager pManager,
54                                        final String pName) throws OceanusException {
55          /* Initialize super-class */
56          super(pManager, pName);
57      }
58  
59      @Override
60      public PrometheusPreferenceManager getPreferenceManager() {
61          return (PrometheusPreferenceManager) super.getPreferenceManager();
62      }
63  
64      /**
65       * Obtain the security.
66       *
67       * @return the security
68       * @throws OceanusException on error
69       */
70      public PrometheusPreferenceSecurity getSecurity() throws OceanusException {
71          if (theSecurity == null) {
72              theSecurity = getPreferenceManager().getSecurity();
73          }
74          return theSecurity;
75      }
76  
77      /**
78       * Define new ByteArray preference.
79       *
80       * @param pKey the key for the preference
81       * @return the preference item
82       */
83      protected PrometheusByteArrayPreference defineByteArrayPreference(final MetisPreferenceKey pKey) {
84          /* Define the preference */
85          final PrometheusByteArrayPreference myPref = new PrometheusByteArrayPreference(this, pKey);
86  
87          /* Add it to the list of preferences */
88          definePreference(myPref);
89  
90          /* Return the preference */
91          return myPref;
92      }
93  
94      /**
95       * Define new CharArray preference.
96       *
97       * @param pKey the key for the preference
98       * @return the preference item
99       * @throws OceanusException on error
100      */
101     protected PrometheusCharArrayPreference defineCharArrayPreference(final MetisPreferenceKey pKey) throws OceanusException {
102         /* Define the preference */
103         final PrometheusCharArrayPreference myPref = new PrometheusCharArrayPreference(this, pKey);
104 
105         /* Add it to the list of preferences */
106         definePreference(myPref);
107 
108         /* Return the preference */
109         return myPref;
110     }
111 
112     /**
113      * Obtain ByteArray preference.
114      *
115      * @param pKey the key of the preference
116      * @return the ByteArray preference
117      */
118     public PrometheusByteArrayPreference getByteArrayPreference(final MetisPreferenceKey pKey) {
119         /* Access preference */
120         final MetisPreferenceItem myPref = getPreference(pKey);
121 
122         /* Reject if not found */
123         if (myPref == null) {
124             throw new IllegalArgumentException(ERROR_UNKNOWN
125                     + pKey);
126         }
127 
128         /* Reject if wrong type */
129         if (!(myPref instanceof PrometheusByteArrayPreference)) {
130             throw new IllegalArgumentException(ERROR_INVALID
131                     + pKey);
132         }
133 
134         /* Return the preference */
135         return (PrometheusByteArrayPreference) myPref;
136     }
137 
138     /**
139      * Obtain ByteArray value.
140      *
141      * @param pKey the key of the preference
142      * @return the ByteArray value
143      */
144     public byte[] getByteArrayValue(final MetisPreferenceKey pKey) {
145         /* Access preference */
146         final PrometheusByteArrayPreference myPref = getByteArrayPreference(pKey);
147 
148         /* Return the value */
149         return myPref.getValue();
150     }
151 
152     /**
153      * Obtain CharArray preference.
154      *
155      * @param pKey the key of the preference
156      * @return the CharArray preference
157      */
158     public PrometheusCharArrayPreference getCharArrayPreference(final MetisPreferenceKey pKey) {
159         /* Access preference */
160         final MetisPreferenceItem myPref = getPreference(pKey);
161 
162         /* Reject if not found */
163         if (myPref == null) {
164             throw new IllegalArgumentException(ERROR_UNKNOWN
165                     + pKey);
166         }
167 
168         /* Reject if wrong type */
169         if (!(myPref instanceof PrometheusCharArrayPreference)) {
170             throw new IllegalArgumentException(ERROR_INVALID
171                     + pKey);
172         }
173 
174         /* Return the preference */
175         return (PrometheusCharArrayPreference) myPref;
176     }
177 
178     /**
179      * Obtain CharArray value.
180      *
181      * @param pKey the key of the preference
182      * @return the CharArray value
183      */
184     public char[] getCharArrayValue(final MetisPreferenceKey pKey) {
185         /* Access preference */
186         final PrometheusCharArrayPreference myPref = getCharArrayPreference(pKey);
187 
188         /* Return the value */
189         return myPref.getValue();
190     }
191 
192     /**
193      * ByteArray preference.
194      */
195     public static class PrometheusByteArrayPreference
196             extends MetisPreferenceItem {
197         /**
198          * Constructor.
199          *
200          * @param pSet the preference Set
201          * @param pKey the key of the preference
202          */
203         protected PrometheusByteArrayPreference(final PrometheusPreferenceSet pSet,
204                                                 final MetisPreferenceKey pKey) {
205             /* Store name */
206             super(pSet, pKey, PrometheusPreferenceType.BYTEARRAY);
207 
208             /* Check whether we have an existing value */
209             if (pSet.checkExists(pKey)) {
210                 /* Access the value */
211                 final byte[] myValue = getHandle().getByteArray(getPreferenceName(), null);
212 
213                 /* Set as initial value */
214                 setTheValue(myValue);
215             }
216         }
217 
218         @Override
219         public byte[] getValue() {
220             return (byte[]) super.getValue();
221         }
222 
223         /**
224          * Set value.
225          *
226          * @param pNewValue the new value
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      * CharArray preference.
240      */
241     public static class PrometheusCharArrayPreference
242             extends MetisPreferenceItem {
243         /**
244          * Constructor.
245          *
246          * @param pSet the preference Set
247          * @param pKey the key of the preference
248          * @throws OceanusException on error
249          */
250         protected PrometheusCharArrayPreference(final PrometheusPreferenceSet pSet,
251                                                 final MetisPreferenceKey pKey) throws OceanusException {
252             /* Store name */
253             super(pSet, pKey, PrometheusPreferenceType.CHARARRAY);
254 
255             /* Check whether we have an existing value */
256             if (pSet.checkExists(pKey)) {
257                 /* Access the value */
258                 final byte[] myBytes = getHandle().getByteArray(getPreferenceName(), null);
259                 final PrometheusPreferenceSecurity mySecurity = getSet().getSecurity();
260 
261                 /* Decrypt the value */
262                 final char[] myValue = myBytes == null
263                         ? null
264                         : mySecurity.decryptValue(myBytes);
265 
266                 /* Set as initial value */
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          * Set value.
283          *
284          * @param pNewValue the new value
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             /* Store the value */
293             final PrometheusPreferenceSecurity mySecurity = getSet().getSecurity();
294             getHandle().putByteArray(getPreferenceName(), mySecurity.encryptValue((char[]) pNewValue));
295         }
296     }
297 }