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.spi.config.domain;
17  
18  import java.beans.PropertyChangeListener;
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  
22  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
23  import de.smartics.properties.api.config.domain.ConfigurationProperties;
24  import de.smartics.properties.api.config.domain.ConfigurationValidationException;
25  import de.smartics.properties.api.config.domain.DescribedProperty;
26  import de.smartics.properties.api.config.domain.SerializableConfigurationProperties;
27  import de.smartics.properties.api.config.domain.UnknownPropertyException;
28  import de.smartics.properties.api.config.domain.ValidatedProperty;
29  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
30  import de.smartics.properties.api.core.app.PropertyRootException;
31  import de.smartics.properties.api.core.domain.PropertiesContext;
32  import de.smartics.properties.api.core.domain.PropertyContext;
33  import de.smartics.properties.api.core.domain.PropertyDescriptor;
34  import de.smartics.properties.api.core.domain.PropertyKey;
35  import de.smartics.properties.api.core.domain.PropertyValidationException;
36  import de.smartics.properties.api.core.domain.PropertyValueConversionException;
37  import de.smartics.properties.api.core.security.SecurityException;
38  
39  /**
40   * A serialization proxy that allows non serializable configuration properties
41   * instances to be serialized using their factories cache and ability to
42   * recreate that instances.
43   */
44  public class ConfigurationPropertiesProxy implements
45      SerializableConfigurationProperties
46  {
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    /**
52     * The class version identifier.
53     */
54    private static final long serialVersionUID = 1L;
55  
56    // --- members --------------------------------------------------------------
57  
58    /**
59     * The key of the configuration properties instance is to be associated with.
60     *
61     * @serial
62     */
63    private final ConfigurationKey<?> key;
64  
65    /**
66     * The factory to create instances of the configuration properties.
67     *
68     * @serial
69     */
70    private final ConfigurationPropertiesFactory factory;
71  
72    /**
73     * The transient delegate.
74     */
75    private transient ConfigurationProperties configuration;
76  
77    // ****************************** Initializer *******************************
78  
79    // ****************************** Constructors ******************************
80  
81    /**
82     * Default constructor.
83     *
84     * @param key the key of the configuration properties instance is to be
85     *          associated with
86     * @param factory the factory to create instances of the configuration
87     *          properties.
88     */
89    public ConfigurationPropertiesProxy(final ConfigurationKey<?> key,
90        final ConfigurationPropertiesFactory factory)
91    {
92      this.key = key;
93      this.factory = factory;
94      this.configuration = getConfiguration();
95    }
96  
97    // ****************************** Inner Classes *****************************
98  
99    // ********************************* Methods ********************************
100 
101   // --- init -----------------------------------------------------------------
102 
103   private ConfigurationProperties getConfiguration()
104   {
105     final ConfigurationProperties properties = factory.create(key);
106     return properties;
107   }
108 
109   // --- get&set --------------------------------------------------------------
110 
111   /**
112    * Returns the key of the configuration properties instance is to be
113    * associated with.
114    *
115    * @return the key of the configuration properties instance is to be
116    *         associated with.
117    */
118   @Override
119   public final ConfigurationKey<?> getKey()
120   {
121     return key;
122   }
123 
124   /**
125    * Returns the factory to create instances of the configuration properties.
126    *
127    * @return the factory to create instances of the configuration properties.
128    */
129   protected final ConfigurationPropertiesFactory getFactory()
130   {
131     return factory;
132   }
133 
134   @Override
135   public boolean isInAdminMode()
136   {
137     return false;
138   }
139 
140   // --- business -------------------------------------------------------------
141 
142   @Override
143   public final PropertiesContext getContext(final Class<?> declaringType)
144     throws NullPointerException
145   {
146     return configuration.getContext(declaringType);
147   }
148 
149   @Override
150   public final PropertyContext getContext(final PropertyDescriptor descriptor)
151     throws NullPointerException
152   {
153     return configuration.getContext(descriptor);
154   }
155 
156   @Override
157   public final <T> T getProperties(final Class<T> propertiesInterface)
158   {
159     return configuration.getProperties(propertiesInterface);
160   }
161 
162   @Override
163   public final Object getPropertyValue(final PropertyDescriptor descriptor)
164     throws NullPointerException, PropertyValidationException
165   {
166     return configuration.getPropertyValue(descriptor);
167   }
168 
169   @Override
170   public final Object getPropertyValue(final String key)
171     throws IllegalArgumentException, UnknownPropertyException,
172     PropertyValidationException
173   {
174     return configuration.getPropertyValue(key);
175   }
176 
177   @Override
178   public final Object getPropertyValue(final PropertyKey key)
179     throws IllegalArgumentException, UnknownPropertyException,
180     PropertyValidationException
181   {
182     return configuration.getPropertyValue(key);
183   }
184 
185   @Override
186   public final Object getPropertyValue(final PropertyDescriptor descriptor,
187       final Object defaultValue) throws NullPointerException,
188     PropertyValueConversionException, PropertyValidationException,
189     UnknownPropertyException
190   {
191     return configuration.getPropertyValue(descriptor, defaultValue);
192   }
193 
194   @Override
195   public final Object getPropertyValue(final String key,
196       final Object defaultValue) throws IllegalArgumentException,
197     UnknownPropertyException, PropertyValidationException
198   {
199     return configuration.getPropertyValue(key, defaultValue);
200   }
201 
202   @Override
203   public final DescribedProperty getProperty(final String key)
204     throws IllegalArgumentException, UnknownPropertyException
205   {
206     return configuration.getProperty(key);
207   }
208 
209   @Override
210   public final DescribedProperty getProperty(final String key,
211       final Object defaultValue) throws IllegalArgumentException,
212     UnknownPropertyException
213   {
214     return configuration.getProperty(key, defaultValue);
215   }
216 
217   @Override
218   public final DescribedProperty getProperty(final PropertyKey key)
219     throws IllegalArgumentException, UnknownPropertyException
220   {
221     return configuration.getProperty(key);
222   }
223 
224   @Override
225   public final DescribedProperty getProperty(final PropertyDescriptor descriptor)
226     throws IllegalArgumentException, UnknownPropertyException
227   {
228     return configuration.getProperty(descriptor);
229   }
230 
231   @Override
232   public final DescribedProperty getProperty(
233       final PropertyDescriptor descriptor, final Object defaultValue)
234     throws IllegalArgumentException, UnknownPropertyException
235   {
236     return configuration.getProperty(descriptor, defaultValue);
237   }
238 
239   @Override
240   public Object getPropertyAsType(final PropertyDescriptor descriptor)
241     throws IllegalArgumentException, UnknownPropertyException,
242     PropertyValueConversionException, SecurityException, PropertyRootException
243   {
244     return configuration.getPropertyAsType(descriptor);
245   }
246 
247   @Override
248   public final ValidatedProperty getValidatedProperty(
249       final PropertyDescriptor descriptor, final Object defaultValue)
250     throws IllegalArgumentException, UnknownPropertyException,
251     PropertyValidationException
252   {
253     return configuration.getValidatedProperty(descriptor, defaultValue);
254   }
255 
256   @Override
257   public final ValidatedProperty getValidatedProperty(final PropertyKey key,
258       final Object defaultValue) throws IllegalArgumentException,
259     UnknownPropertyException, PropertyValidationException
260   {
261     return configuration.getValidatedProperty(key, defaultValue);
262   }
263 
264   @Override
265   public final ValidatedProperty getValidatedProperty(final String key,
266       final Object defaultValue) throws IllegalArgumentException,
267     UnknownPropertyException, PropertyValidationException
268   {
269     return configuration.getValidatedProperty(key, defaultValue);
270   }
271 
272   @Override
273   public final String getPropertyValueAsString(
274       final PropertyDescriptor descriptor) throws NullPointerException,
275     PropertyValidationException
276   {
277     return configuration.getPropertyValueAsString(descriptor);
278   }
279 
280   @Override
281   public final String getPropertyValueAsString(final String key)
282     throws IllegalArgumentException, UnknownPropertyException,
283     PropertyValidationException
284   {
285     return configuration.getPropertyValueAsString(key);
286   }
287 
288   @Override
289   public final String getPropertyValueAsString(
290       final PropertyDescriptor descriptor, final Object defaultValue)
291     throws NullPointerException, PropertyValidationException
292   {
293     return configuration.getPropertyValueAsString(descriptor, defaultValue);
294   }
295 
296   @Override
297   public final String getPropertyValueAsString(final String key,
298       final Object defaultValue) throws IllegalArgumentException,
299     UnknownPropertyException, PropertyValidationException
300   {
301     return configuration.getPropertyValueAsString(key, defaultValue);
302   }
303 
304   @Override
305   public final void validate() throws ConfigurationValidationException
306   {
307     configuration.validate();
308   }
309 
310   @Override
311   public final void validate(final Class<?>... groups)
312     throws ConfigurationValidationException
313   {
314     configuration.validate(groups);
315   }
316 
317   @Override
318   public final void validate(final boolean lenient, final Class<?>... groups)
319     throws ConfigurationValidationException
320   {
321     configuration.validate(lenient, groups);
322   }
323 
324   @Override
325   public final void validate(final PropertyDescriptor descriptor,
326       final Class<?>... ifInOneOfTheseGroups)
327     throws ConfigurationValidationException
328   {
329     configuration.validate(descriptor, ifInOneOfTheseGroups);
330   }
331 
332   @Override
333   public final void validate(final PropertyDescriptor descriptor,
334       final String value, final Class<?>... ifInOneOfTheseGroups)
335     throws ConfigurationValidationException
336   {
337     configuration.validate(descriptor, value, ifInOneOfTheseGroups);
338   }
339 
340   @Override
341   public final void addPropertyChangeListener(final PropertyKey name,
342       final PropertyChangeListener listener) throws NullPointerException
343   {
344     configuration.addPropertyChangeListener(name, listener);
345   }
346 
347   @Override
348   public final void removePropertyChangeListener(final PropertyKey name,
349       final PropertyChangeListener listener) throws NullPointerException
350   {
351     configuration.removePropertyChangeListener(name, listener);
352   }
353 
354   @Override
355   public final void addPropertyChangeListener(
356       final PropertyChangeListener listener) throws NullPointerException
357   {
358     configuration.addPropertyChangeListener(listener);
359   }
360 
361   @Override
362   public final void removePropertyChangeListener(
363       final PropertyChangeListener listener) throws NullPointerException
364   {
365     configuration.removePropertyChangeListener(listener);
366   }
367 
368   @Override
369   public ConfigurationProperties toRepresentative()
370   {
371     return this;
372   }
373 
374   @Override
375   public SerializableConfigurationProperties toSerializable()
376   {
377     return this;
378   }
379 
380   // --- object basics --------------------------------------------------------
381 
382   /**
383    * Reads the object from the given stream.
384    *
385    * @param in the stream to read from.
386    * @throws IOException on read problems.
387    * @throws ClassNotFoundException if a class cannot be found.
388    */
389   private void readObject(final ObjectInputStream in) throws IOException,
390     ClassNotFoundException
391   {
392     in.defaultReadObject();
393 
394     configuration = getConfiguration();
395   }
396 }