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.impl.config.cache;
17  
18  import java.beans.PropertyChangeListener;
19  import java.io.Serializable;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import javax.annotation.concurrent.ThreadSafe;
24  
25  import org.apache.commons.lang.ObjectUtils;
26  
27  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
28  import de.smartics.properties.api.config.domain.ConfigurationValidationException;
29  import de.smartics.properties.api.config.domain.Property;
30  import de.smartics.properties.api.config.domain.ResolvedProperty;
31  import de.smartics.properties.api.config.domain.UnknownPropertyException;
32  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
33  import de.smartics.properties.api.core.domain.DuplicatePropertyDeclarationsException;
34  import de.smartics.properties.api.core.domain.PropertiesContext;
35  import de.smartics.properties.api.core.domain.PropertyContext;
36  import de.smartics.properties.api.core.domain.PropertyDescriptor;
37  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
38  import de.smartics.properties.api.core.domain.PropertyKey;
39  import de.smartics.properties.api.core.domain.PropertyValidationException;
40  import de.smartics.properties.api.core.domain.PropertyValueConversionException;
41  import de.smartics.properties.api.core.domain.ReadOnlyPropertyException;
42  import de.smartics.properties.spi.config.support.ConfigurationPropertiesManagementSpi;
43  import de.smartics.util.lang.Arguments;
44  
45  /**
46   * A delegate that provides a cache.
47   */
48  @ThreadSafe
49  public abstract class AbstractCacheConfigurationPropertiesManagement implements
50      ConfigurationPropertiesManagement, Serializable
51  { // NOPMD
52    // ********************************* Fields *********************************
53  
54    // --- constants ------------------------------------------------------------
55  
56    /**
57     * The class version identifier.
58     */
59    private static final long serialVersionUID = 1L;
60  
61    // --- members --------------------------------------------------------------
62  
63    /**
64     * The cache implementation that provides synchronized access to the stored
65     * elements.
66     */
67    private final DependencyTrackingCache cache;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    /**
74     * Default constructor.
75     */
76    public AbstractCacheConfigurationPropertiesManagement()
77    {
78      this(new DependencyTrackingCache());
79    }
80  
81    /**
82     * Default constructor that allows to specify a cache to be used.
83     *
84     * @param cache the synchronized cache.
85     * @throws NullPointerException if {@code cache} is <code>null</code>.
86     */
87    protected AbstractCacheConfigurationPropertiesManagement(
88        final DependencyTrackingCache cache) throws NullPointerException
89    {
90      Arguments.checkNotNull("cache", cache);
91  
92      this.cache = cache;
93    }
94  
95    // ****************************** Inner Classes *****************************
96  
97    // ********************************* Methods ********************************
98  
99    // --- init -----------------------------------------------------------------
100 
101   // --- get&set --------------------------------------------------------------
102 
103   /**
104    * Returns a reference to the delegate.
105    *
106    * @return a reference to the delegate. Never <code>null</code>.
107    */
108   protected abstract ConfigurationPropertiesManagementSpi getDelegate();
109 
110   /**
111    * Provides access to the cache.
112    *
113    * @return the cache.
114    */
115   protected final DependencyTrackingCache getCache()
116   {
117     return cache;
118   }
119 
120   // --- business -------------------------------------------------------------
121 
122   // ... cache relevant methods ...............................................
123 
124   // ... get property / properties
125 
126   @Override
127   public final ResolvedProperty getResolvedProperty(final String key,
128       final Object defaultValue) throws IllegalArgumentException,
129     UnknownPropertyException, PropertyValidationException
130   {
131     final ResolvedProperty property =
132         getCache().getResolvedProperty(getDelegate(), key, defaultValue);
133     return property;
134   }
135 
136   @Override
137   public final Property getProperty(final String key, final Object defaultValue)
138     throws IllegalArgumentException, UnknownPropertyException
139   {
140     final Property property =
141         getCache().getProperty(getDelegate(), key, defaultValue);
142     return property;
143   }
144 
145   @Override
146   public final Property getProperty(final String key)
147     throws IllegalArgumentException, UnknownPropertyException
148   {
149     return getProperty(key, null);
150   }
151 
152   @Override
153   public final Property getProperty(final PropertyDescriptor descriptor,
154       final Object defaultValue) throws IllegalArgumentException,
155     UnknownPropertyException
156   {
157     return getProperty(descriptor.getKey().toString(), defaultValue);
158   }
159 
160   @Override
161   public final Object getPropertyValue(final String key,
162       final Object defaultValue) throws IllegalArgumentException,
163     UnknownPropertyException, PropertyValidationException
164   {
165     final ResolvedProperty property = getResolvedProperty(key, defaultValue);
166     return property.getResolvedValue();
167   }
168 
169   @Override
170   public final <T> T getProperties(final Class<T> propertiesInterface)
171   {
172     return getDelegate().getProperties(propertiesInterface, toSerializable());
173   }
174 
175   // ... set property / properties
176 
177   @Override
178   public final Property setProperty(final PropertyKey key, final String value)
179     throws NullPointerException, PropertyValidationException,
180     ReadOnlyPropertyException
181   {
182     final Property oldProperty = getDelegate().setProperty(key, value);
183     final String stringKey = key.toString();
184     cache.removeFromCache(stringKey);
185     return oldProperty;
186   }
187 
188   @Override
189   public final Property unsetProperty(final PropertyKey key)
190     throws NullPointerException, ReadOnlyPropertyException
191   {
192     final Property oldProperty = getDelegate().unsetProperty(key);
193     final String stringKey = key.toString();
194     cache.removeFromCache(stringKey);
195     return oldProperty;
196   }
197 
198   @Override
199   public final ConfigurationPropertiesManagement addDefinitions(
200       final Properties properties) throws NullPointerException
201   {
202     final ConfigurationPropertiesManagement manager =
203         getDelegate().addDefinitions(properties);
204 
205     cache.removeFromCache(properties);
206 
207     return manager;
208   }
209 
210   @Override
211   public final void flush()
212   {
213     getDelegate().flush();
214   }
215 
216   // ... delegation only ......................................................
217 
218   @Override
219   public final PropertyDescriptorRegistry getRegistry()
220   {
221     return getDelegate().getRegistry();
222   }
223 
224   @Override
225   public final Object getPropertyValue(final PropertyDescriptor descriptor)
226     throws NullPointerException, PropertyValidationException
227   {
228     return getPropertyValue(descriptor.getKey());
229   }
230 
231   @Override
232   public final Object getPropertyValue(final PropertyKey key)
233     throws IllegalArgumentException, UnknownPropertyException,
234     PropertyValidationException
235   {
236     return getPropertyValue(key.toString());
237   }
238 
239   @Override
240   public final Object getPropertyValue(final PropertyDescriptor descriptor,
241       final Object defaultValue) throws NullPointerException,
242     PropertyValueConversionException, PropertyValidationException,
243     UnknownPropertyException
244   {
245     return getPropertyValue(descriptor.getKey().toString(), defaultValue);
246   }
247 
248   @Override
249   public final Object getPropertyValue(final String key)
250     throws IllegalArgumentException, UnknownPropertyException,
251     PropertyValidationException
252   {
253     return getPropertyValue(key, null);
254   }
255 
256   @Override
257   public final String getPropertyValueAsString(
258       final PropertyDescriptor descriptor) throws NullPointerException,
259     PropertyValidationException
260   {
261     final Object value = getPropertyValue(descriptor);
262     return ObjectUtils.toString(value, null);
263   }
264 
265   @Override
266   public final String getPropertyValueAsString(final String key)
267     throws IllegalArgumentException, UnknownPropertyException,
268     PropertyValidationException
269   {
270     final Object value = getPropertyValue(key);
271     return ObjectUtils.toString(value, null);
272   }
273 
274   @Override
275   public final String getPropertyValueAsString(
276       final PropertyDescriptor descriptor, final Object defaultValue)
277     throws NullPointerException, PropertyValidationException
278   {
279     final Object value = getPropertyValue(descriptor, defaultValue);
280     return ObjectUtils.toString(value, null);
281   }
282 
283   @Override
284   public final String getPropertyValueAsString(final String key,
285       final Object defaultValue) throws IllegalArgumentException,
286     UnknownPropertyException, PropertyValidationException
287   {
288     final Object value = getPropertyValue(key, defaultValue);
289     return ObjectUtils.toString(value, null);
290   }
291 
292   @Override
293   public final Property getProperty(final PropertyKey key)
294     throws IllegalArgumentException, UnknownPropertyException
295   {
296     return getProperty(key.toString());
297   }
298 
299   @Override
300   public final Property getProperty(final PropertyDescriptor descriptor)
301     throws IllegalArgumentException, UnknownPropertyException
302   {
303     return getProperty(descriptor.getKey().toString(), null);
304   }
305 
306   @Override
307   public final ResolvedProperty getResolvedProperty(
308       final PropertyDescriptor descriptor, final Object defaultValue)
309     throws IllegalArgumentException, UnknownPropertyException,
310     PropertyValidationException
311   {
312     return getResolvedProperty(descriptor.getKey(), defaultValue);
313   }
314 
315   @Override
316   public final ResolvedProperty getResolvedProperty(final PropertyKey key,
317       final Object defaultValue) throws IllegalArgumentException,
318     UnknownPropertyException, PropertyValidationException
319   {
320     return getResolvedProperty(key.toString(), defaultValue);
321   }
322 
323   @Override
324   public final void addDescriptors(final Class<?> propertySetType)
325     throws DuplicatePropertyDeclarationsException
326   {
327     getDelegate().addDescriptors(propertySetType);
328   }
329 
330   @Override
331   public final ConfigurationKey getKey()
332   {
333     return getDelegate().getKey();
334   }
335 
336   @Override
337   public final PropertyDescriptor getDescriptor(final String key)
338     throws UnknownPropertyException
339   {
340     return getDelegate().getDescriptor(key);
341   }
342 
343   @Override
344   public final PropertiesContext getContext(final Class<?> declaringType)
345     throws NullPointerException
346   {
347     return getDelegate().getContext(declaringType);
348   }
349 
350   @Override
351   public final PropertyDescriptor getDescriptor(final PropertyKey key)
352     throws UnknownPropertyException
353   {
354     return getDelegate().getDescriptor(key);
355   }
356 
357   @Override
358   public final List<PropertyDescriptor> getMandatoryPropertyDescriptors()
359   {
360     return getDelegate().getMandatoryPropertyDescriptors();
361   }
362 
363   @Override
364   public final PropertyContext getContext(final PropertyDescriptor descriptor)
365     throws NullPointerException
366   {
367     return getDelegate().getContext(descriptor);
368   }
369 
370   @Override
371   public final void validate() throws ConfigurationValidationException
372   {
373     getDelegate().validate();
374   }
375 
376   @Override
377   public final void validate(final boolean lenient)
378     throws ConfigurationValidationException
379   {
380     getDelegate().validate(lenient);
381   }
382 
383   @Override
384   public final void addPropertyChangeListener(final PropertyKey name,
385       final PropertyChangeListener listener) throws NullPointerException
386   {
387     getDelegate().addPropertyChangeListener(name, listener);
388   }
389 
390   @Override
391   public final void removePropertyChangeListener(final PropertyKey name,
392       final PropertyChangeListener listener) throws NullPointerException
393   {
394     getDelegate().removePropertyChangeListener(name, listener);
395   }
396 
397   @Override
398   public final void addPropertyChangeListener(
399       final PropertyChangeListener listener) throws NullPointerException
400   {
401     getDelegate().addPropertyChangeListener(listener);
402   }
403 
404   @Override
405   public final void removePropertyChangeListener(
406       final PropertyChangeListener listener) throws NullPointerException
407   {
408     getDelegate().removePropertyChangeListener(listener);
409   }
410 
411   // --- object basics --------------------------------------------------------
412 
413 }