View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.api.config.domain;
17  
18  import java.beans.PropertyChangeListener;
19  
20  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
21  import de.smartics.properties.api.core.domain.PropertiesContext;
22  import de.smartics.properties.api.core.domain.PropertyContext;
23  import de.smartics.properties.api.core.domain.PropertyDescriptor;
24  import de.smartics.properties.api.core.domain.PropertyKey;
25  import de.smartics.properties.api.core.domain.PropertyValidationException;
26  import de.smartics.properties.api.core.domain.PropertyValueConversionException;
27  
28  /**
29   * Provides access to all configuration properties for a given application in a
30   * given environment.
31   * <p>
32   * There are two ways to access property values.
33   * </p>
34   * <ol>
35   * <li>Use {@link #getProperties(Class)} to obtain an implementation of an
36   * interface that provides access to the property values through its methods.
37   * This is the recommended way to deal with properties.</li>
38   * <li>Use one of the {@code getProperty} methods. This access may be more
39   * convenient, if the context deals with property keys that are provided by the
40   * user (e.g. from an UI).</li>
41   * </ol>
42   *
43   * @impl This interface is intended to be implemented by service providers. API
44   *       users are intended to us this interface to access properties. For
45   *       details on creating instances of classes implementing this interface
46   *       please refer to {@link de.smartics.properties.api.config}.
47   */
48  public interface ConfigurationProperties
49  {
50    // ********************************* Fields *********************************
51  
52    // --- constants ------------------------------------------------------------
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Inner Classes *****************************
57  
58    // ********************************* Methods ********************************
59  
60    // --- get&set --------------------------------------------------------------
61  
62    /**
63     * Returns the key of the configuration.
64     *
65     * @return the key of the configuration. Never <code>null</code>.
66     */
67    ConfigurationKey getKey();
68  
69    /**
70     * Returns the properties context for the declaring type. The declaring type
71     * is the interface type annotated with the
72     * {@link de.smartics.properties.api.core.annotations.PropertySet} annotation.
73     * The context provides information about the properties of that set.
74     *
75     * @param declaringType the type that declares the properties whose context is
76     *          requested.
77     * @return the context for the properties declared by the given type.
78     * @throws NullPointerException if {@code declaringType} is <code>null</code>.
79     */
80    PropertiesContext getContext(Class<?> declaringType)
81      throws NullPointerException;
82  
83    /**
84     * Returns the properties context the property described by the given
85     * descriptor is part of.
86     *
87     * @param descriptor the descriptor of the property whose context is
88     *          requested.
89     * @return the context of the property described by the descriptor.
90     * @throws NullPointerException if {@code descriptor} is <code>null</code>.
91     */
92    PropertyContext getContext(PropertyDescriptor descriptor)
93      throws NullPointerException;
94  
95    // --- business -------------------------------------------------------------
96  
97    /**
98     * Returns a implementation of the given interface that has access to the
99     * property keys, the property descriptors and the properties itself, when
100    * they are declared in the given interface. The interface must be annotated
101    * with an {@link de.smartics.properties.api.core.annotations.PropertySet}
102    * annotation.
103    *
104    * @param propertiesInterface a
105    *          {@link de.smartics.properties.api.core.annotations.PropertySet}
106    *          annotated interface for which a implementation to access the
107    *          property keys, descriptors and values is requested.
108    * @param <T> type variable to enable a type save return value.
109    * @return a implementation of the given interface to access property
110    *         information.
111    */
112   <T> T getProperties(Class<T> propertiesInterface);
113 
114   /**
115    * Returns the property value for the given descriptor.
116    *
117    * @param descriptor the key to the property.
118    * @return the value to the property. A value of <code>null</code> is returned
119    *         in cause of an optional property being not set.
120    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
121    * @throws PropertyValidationException if the property is invalid according to
122    *           its constraints.
123    */
124   Object getPropertyValue(PropertyDescriptor descriptor)
125     throws NullPointerException, PropertyValidationException;
126 
127   /**
128    * Returns the property value for the given key.
129    *
130    * @param key the key to the property.
131    * @return the value to the property. A value of <code>null</code> is returned
132    *         in cause of an optional property being not set.
133    * @throws IllegalArgumentException if {@code key} is blank.
134    * @throws UnknownPropertyException if {@code key} is not known.
135    * @throws PropertyValidationException if the property is invalid according to
136    *           its constraints.
137    */
138   Object getPropertyValue(String key) throws IllegalArgumentException,
139     UnknownPropertyException, PropertyValidationException;
140 
141   /**
142    * Returns the property value for the given key.
143    *
144    * @param key the key to the property.
145    * @return the value to the property. A value of <code>null</code> is returned
146    *         in cause of an optional property being not set.
147    * @throws IllegalArgumentException if {@code key} is blank.
148    * @throws UnknownPropertyException if {@code key} is not known.
149    * @throws PropertyValidationException if the property is invalid according to
150    *           its constraints.
151    */
152   Object getPropertyValue(PropertyKey key) throws IllegalArgumentException,
153     UnknownPropertyException, PropertyValidationException;
154 
155   /**
156    * Returns the property value for the given descriptor, allowing to
157    * transparently provide a default value to be returned in case the property
158    * has not been set.
159    *
160    * @param descriptor the key to the property.
161    * @param defaultValue the default value to use in case no value has been
162    *          specified.
163    * @return the value to the property. A value of <code>null</code> is returned
164    *         in cause of an optional property being not set.
165    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
166    * @throws PropertyValueConversionException if conversion fails.
167    * @throws PropertyValidationException if the property is invalid according to
168    *           its constraints.
169    * @throws UnknownPropertyException if the requested property is not known to
170    *           the system.
171    */
172   Object getPropertyValue(PropertyDescriptor descriptor, Object defaultValue)
173     throws NullPointerException, PropertyValueConversionException,
174     PropertyValidationException, UnknownPropertyException;
175 
176   /**
177    * Returns the property value for the given key, allowing to transparently
178    * provide a default value to be returned in case the property has not been
179    * set.
180    *
181    * @param key the key to the property.
182    * @param defaultValue the default value to use in case no value has been
183    *          specified.
184    * @return the value to the property. A value of <code>null</code> is returned
185    *         in cause of an optional property being not set.
186    * @throws IllegalArgumentException if {@code key} is blank.
187    * @throws UnknownPropertyException if {@code key} is not known.
188    * @throws PropertyValidationException if the property is invalid according to
189    *           its constraints.
190    */
191   Object getPropertyValue(String key, Object defaultValue)
192     throws IllegalArgumentException, UnknownPropertyException,
193     PropertyValidationException;
194 
195   /**
196    * Returns the property for the given key.
197    *
198    * @param key the unique key of the property.
199    * @return the requested property.
200    * @throws IllegalArgumentException if {@code key} is blank.
201    * @throws UnknownPropertyException if {@code key} is not known.
202    */
203   Property getProperty(String key) throws IllegalArgumentException,
204     UnknownPropertyException;
205 
206   /**
207    * Returns the property for the given key, allowing to transparently provide a
208    * default value to be returned in case the property has not been set.
209    *
210    * @param key the unique key of the property.
211    * @param defaultValue the default value to use in case no value has been
212    *          specified.
213    * @return the requested property.
214    * @throws IllegalArgumentException if {@code key} is blank.
215    * @throws UnknownPropertyException if {@code key} is not known.
216    */
217   Property getProperty(String key, Object defaultValue)
218     throws IllegalArgumentException, UnknownPropertyException;
219 
220   /**
221    * Returns the property for the given key.
222    *
223    * @param key the unique key of the property.
224    * @return the requested property.
225    * @throws IllegalArgumentException if {@code key} is blank.
226    * @throws UnknownPropertyException if {@code key} is not known.
227    */
228   Property getProperty(PropertyKey key) throws IllegalArgumentException,
229     UnknownPropertyException;
230 
231   /**
232    * Returns the property for the given descriptor.
233    *
234    * @param descriptor the descriptor containing the unique key of the property.
235    * @return the requested property.
236    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
237    * @throws UnknownPropertyException if the key of the {@code descriptor} is
238    *           not known.
239    */
240   Property getProperty(PropertyDescriptor descriptor)
241     throws NullPointerException, UnknownPropertyException;
242 
243   /**
244    * Returns the property for the given descriptor, allowing to transparently
245    * provide a default value to be returned in case the property has not been
246    * set.
247    *
248    * @param descriptor the descriptor containing the unique key of the property.
249    * @param defaultValue the default value to use in case no value has been
250    *          specified.
251    * @return the requested property.
252    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
253    * @throws UnknownPropertyException if the key of the {@code descriptor} is
254    *           not known.
255    */
256   Property getProperty(PropertyDescriptor descriptor, Object defaultValue)
257     throws NullPointerException, UnknownPropertyException;
258 
259   /**
260    * Returns the resolved property for the given descriptor's key, allowing to
261    * transparently provide a default value to be returned in case the property
262    * has not been set.
263    *
264    * @param descriptor descriptor key to the property.
265    * @param defaultValue the default value to use in case no value has been
266    *          specified.
267    * @return the resolved property. Never <code>null</code>, although the value
268    *         / resolved value of the returned property may be <code>null</code>.
269    * @throws IllegalArgumentException if {@code key} is blank.
270    * @throws UnknownPropertyException if {@code key} is not known.
271    * @throws PropertyValidationException if the property is invalid according to
272    *           its constraints.
273    */
274   ResolvedProperty getResolvedProperty(PropertyDescriptor descriptor,
275       Object defaultValue) throws IllegalArgumentException,
276     UnknownPropertyException, PropertyValidationException;
277 
278   /**
279    * Returns the resolved property for the given key, allowing to transparently
280    * provide a default value to be returned in case the property has not been
281    * set.
282    *
283    * @param key the key to the property.
284    * @param defaultValue the default value to use in case no value has been
285    *          specified.
286    * @return the resolved property. Never <code>null</code>, although the value
287    *         / resolved value of the returned property may be <code>null</code>.
288    * @throws IllegalArgumentException if {@code key} is blank.
289    * @throws UnknownPropertyException if {@code key} is not known.
290    * @throws PropertyValidationException if the property is invalid according to
291    *           its constraints.
292    */
293   ResolvedProperty getResolvedProperty(PropertyKey key, Object defaultValue)
294     throws IllegalArgumentException, UnknownPropertyException,
295     PropertyValidationException;
296 
297   /**
298    * Returns the resolved property for the given key, allowing to transparently
299    * provide a default value to be returned in case the property has not been
300    * set.
301    *
302    * @param key the key to the property.
303    * @param defaultValue the default value to use in case no value has been
304    *          specified.
305    * @return the resolved property. Never <code>null</code>, although the value
306    *         / resolved value of the returned property may be <code>null</code>.
307    * @throws IllegalArgumentException if {@code key} is blank.
308    * @throws UnknownPropertyException if {@code key} is not known.
309    * @throws PropertyValidationException if the property is invalid according to
310    *           its constraints.
311    */
312   ResolvedProperty getResolvedProperty(String key, Object defaultValue)
313     throws IllegalArgumentException, UnknownPropertyException,
314     PropertyValidationException;
315 
316   /**
317    * Returns the property string value for the given descriptor. If the property
318    * type is not {@link String}, the {@link Object#toString()} method is called
319    * to create the string representation of the value. <code>null</code> is
320    * always returned as <code>null</code> (not as the String "null").
321    *
322    * @param descriptor the key to the property.
323    * @return the value to the property. A value of <code>null</code> is returned
324    *         in cause of an optional property being not set.
325    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
326    * @throws PropertyValidationException if the property is invalid according to
327    *           its constraints.
328    */
329   String getPropertyValueAsString(PropertyDescriptor descriptor)
330     throws NullPointerException, PropertyValidationException;
331 
332   /**
333    * Returns the property string value for the given key. If the property type
334    * is not {@link String}, the {@link Object#toString()} method is called to
335    * create the string representation of the value. <code>null</code> is always
336    * returned as <code>null</code> (not as the String "null").
337    *
338    * @param key the key to the property.
339    * @return the value to the property. A value of <code>null</code> is returned
340    *         in cause of an optional property being not set.
341    * @throws IllegalArgumentException if {@code key} is blank.
342    * @throws UnknownPropertyException if {@code key} is not known.
343    * @throws PropertyValidationException if the property is invalid according to
344    *           its constraints.
345    */
346   String getPropertyValueAsString(String key) throws IllegalArgumentException,
347     UnknownPropertyException, PropertyValidationException;
348 
349   /**
350    * Returns the property string value for the given descriptor. If the property
351    * type is not {@link String}, the {@link Object#toString()} method is called
352    * to create the string representation of the value. <code>null</code> is
353    * always returned as <code>null</code> (not as the String "null").
354    *
355    * @param descriptor the key to the property.
356    * @param defaultValue the default value to use in case no value has been
357    *          specified.
358    * @return the value to the property. A value of <code>null</code> is returned
359    *         in cause of an optional property being not set.
360    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
361    * @throws PropertyValidationException if the property is invalid according to
362    *           its constraints.
363    */
364   String getPropertyValueAsString(PropertyDescriptor descriptor,
365       final Object defaultValue) throws NullPointerException,
366     PropertyValidationException;
367 
368   /**
369    * Returns the property string value for the given key. If the property type
370    * is not {@link String}, the {@link Object#toString()} method is called to
371    * create the string representation of the value. <code>null</code> is always
372    * returned as <code>null</code> (not as the String "null").
373    *
374    * @param key the key to the property.
375    * @param defaultValue the default value to use in case no value has been
376    *          specified.
377    * @return the value to the property. A value of <code>null</code> is returned
378    *         in cause of an optional property being not set.
379    * @throws IllegalArgumentException if {@code key} is blank.
380    * @throws UnknownPropertyException if {@code key} is not known.
381    * @throws PropertyValidationException if the property is invalid according to
382    *           its constraints.
383    */
384   String getPropertyValueAsString(String key, final Object defaultValue)
385     throws IllegalArgumentException, UnknownPropertyException,
386     PropertyValidationException;
387 
388   /**
389    * Validates all properties in the given configuration in a non-lenient
390    * fashion.
391    *
392    * @throws ConfigurationValidationException if the configuration contains
393    *           properties that do not meet the required constraints.
394    */
395   void validate() throws ConfigurationValidationException;
396 
397   /**
398    * Validates all properties in the given configuration.
399    *
400    * @param lenient the lenient flag that tells the validation process to handle
401    *          unknown properties gracefully if set to <code>true</code>. If the
402    *          value is <code>false</code> unknown properties are reported as
403    *          validation failures.
404    * @throws ConfigurationValidationException if the configuration contains
405    *           properties that do not meet the required constraints.
406    */
407   void validate(boolean lenient) throws ConfigurationValidationException;
408 
409   /**
410    * Adds the given listener as a listener to the given property.
411    *
412    * @param name the name of the property to track changes.
413    * @param listener the listener to add.
414    * @throws NullPointerException if {@code name} or {@code listener} is
415    *           <code>null</code>.
416    */
417   void addPropertyChangeListener(PropertyKey name,
418       PropertyChangeListener listener) throws NullPointerException;
419 
420   /**
421    * Removes the given listener as a listener to the given property.
422    *
423    * @param name the name of the property to stop tracking changes.
424    * @param listener the listener to remove.
425    * @throws NullPointerException if {@code name} or {@code listener} is
426    *           <code>null</code>.
427    */
428   void removePropertyChangeListener(PropertyKey name,
429       PropertyChangeListener listener) throws NullPointerException;
430 
431   /**
432    * Adds the given listener to track any property changes.
433    *
434    * @param listener the listener to add.
435    * @throws NullPointerException if {@code listener} is <code>null</code>.
436    */
437   void addPropertyChangeListener(PropertyChangeListener listener)
438     throws NullPointerException;
439 
440   /**
441    * Removes the given listener to stop tracking property changes.
442    *
443    * @param listener the listener to remove.
444    * @throws NullPointerException if {@code listener} is <code>null</code>.
445    */
446   void removePropertyChangeListener(PropertyChangeListener listener)
447     throws NullPointerException;
448 
449   // --- object basics --------------------------------------------------------
450 
451   /**
452    * Creates a serializable variant of this implementation.
453    *
454    * @return a serializable variant of this implementation.
455    * @see de.smartics.properties.spi.config.domain.ConfigurationPropertiesProxy
456    * @see de.smartics.properties.spi.config.domain.ConfigurationPropertiesManagementProxy
457    */
458   SerializableConfigurationProperties toSerializable();
459 
460 }