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.io.IOException;
19  import java.io.ObjectInputStream;
20  import java.util.List;
21  
22  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
23  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
24  import de.smartics.properties.api.config.domain.Property;
25  import de.smartics.properties.api.config.domain.PropertyProvider;
26  import de.smartics.properties.api.config.domain.PropertyStoreAccessor;
27  import de.smartics.properties.api.config.domain.SerializableConfigurationPropertiesManagement;
28  import de.smartics.properties.api.config.domain.UnknownPropertyException;
29  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
30  import de.smartics.properties.api.core.domain.DuplicatePropertyDeclarationsException;
31  import de.smartics.properties.api.core.domain.PropertyDescriptor;
32  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
33  import de.smartics.properties.api.core.domain.PropertyKey;
34  import de.smartics.properties.api.core.domain.PropertyValidationException;
35  import de.smartics.properties.api.core.domain.ReadOnlyPropertyException;
36  import de.smartics.properties.api.core.security.PropertyValueSecurity;
37  
38  /**
39   * A serialization proxy that allows non serializable configuration properties
40   * instances to be serialized using their factories cache and ability to
41   * recreate that instances.
42   */
43  public final class ConfigurationPropertiesManagementProxy extends
44      ConfigurationPropertiesProxy implements
45      SerializableConfigurationPropertiesManagement
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 transient delegate.
60     */
61    private transient ConfigurationPropertiesManagement configuration;
62  
63    // ****************************** Initializer *******************************
64  
65    // ****************************** Constructors ******************************
66  
67    /**
68     * Default constructor.
69     *
70     * @param key the key of the configuration properties instance is to be
71     *          associated with
72     * @param factory the factory to create instances of the configuration
73     *          properties.
74     */
75    public ConfigurationPropertiesManagementProxy(final ConfigurationKey<?> key,
76        final ConfigurationPropertiesFactory factory)
77    {
78      super(key, factory);
79      this.configuration = getConfiguration();
80    }
81  
82    // ****************************** Inner Classes *****************************
83  
84    // ********************************* Methods ********************************
85  
86    // --- init -----------------------------------------------------------------
87  
88    private ConfigurationPropertiesManagement getConfiguration()
89    {
90      final ConfigurationKey<?> key = getKey();
91      final ConfigurationPropertiesFactory factory = getFactory();
92      final ConfigurationPropertiesManagement configuration =
93          factory.createManagement(key);
94      return configuration;
95    }
96  
97    // --- get&set --------------------------------------------------------------
98  
99    @Override
100   public PropertyDescriptorRegistry getRegistry()
101   {
102     return configuration.getRegistry();
103   }
104 
105   @Override
106   public PropertyValueSecurity getPropertyValueSecurity()
107   {
108     return configuration.getPropertyValueSecurity();
109   }
110 
111   @Override
112   public PropertyStoreAccessor getPropertyStoreAccessor()
113   {
114     return configuration.getPropertyStoreAccessor();
115   }
116 
117   @Override
118   public boolean isInAdminMode()
119   {
120     return configuration.isInAdminMode();
121   }
122 
123   @Override
124   public void setToAdminMode(final boolean newMode)
125   {
126     configuration.setToAdminMode(newMode);
127   }
128 
129   // --- business -------------------------------------------------------------
130 
131   @Override
132   public void addDescriptors(final Class<?> propertySetType)
133     throws DuplicatePropertyDeclarationsException
134   {
135     configuration.addDescriptors(propertySetType);
136   }
137 
138   @Override
139   public PropertyDescriptor getDescriptor(final String key)
140     throws UnknownPropertyException
141   {
142     return configuration.getDescriptor(key);
143   }
144 
145   @Override
146   public PropertyDescriptor getDescriptor(final PropertyKey key)
147     throws UnknownPropertyException
148   {
149     return configuration.getDescriptor(key);
150   }
151 
152   @Override
153   public List<PropertyDescriptor> getMandatoryPropertyDescriptors()
154   {
155     return configuration.getMandatoryPropertyDescriptors();
156   }
157 
158   @Override
159   public ConfigurationPropertiesManagement addDefinitions(
160       final PropertyProvider properties) throws NullPointerException
161   {
162     return configuration.addDefinitions(properties);
163   }
164 
165   @Override
166   public Property setProperty(final PropertyKey key, final String value)
167     throws NullPointerException, PropertyValidationException,
168     ReadOnlyPropertyException
169   {
170     return configuration.setProperty(key, value);
171   }
172 
173   @Override
174   public Property unsetProperty(final PropertyKey key)
175     throws NullPointerException, ReadOnlyPropertyException
176   {
177     return configuration.unsetProperty(key);
178   }
179 
180   @Override
181   public void flush()
182   {
183     configuration.flush();
184   }
185 
186   @Override
187   public ConfigurationPropertiesManagement toRepresentative()
188   {
189     return this;
190   }
191 
192   @Override
193   public SerializableConfigurationPropertiesManagement toSerializable()
194   {
195     return this;
196   }
197 
198   // --- object basics --------------------------------------------------------
199 
200   /**
201    * Reads the object from the given stream.
202    *
203    * @param in the stream to read from.
204    * @throws IOException on read problems.
205    * @throws ClassNotFoundException if a class cannot be found.
206    */
207   private void readObject(final ObjectInputStream in) throws IOException,
208     ClassNotFoundException
209   {
210     in.defaultReadObject();
211 
212     configuration = getConfiguration();
213   }
214 }