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.ds;
17  
18  import javax.annotation.concurrent.ThreadSafe;
19  
20  import de.smartics.properties.api.config.domain.Property;
21  import de.smartics.properties.api.config.domain.PropertyCollection;
22  import de.smartics.properties.api.config.domain.PropertyProvider;
23  import de.smartics.properties.api.config.domain.PropertyStoreAccessor;
24  import de.smartics.properties.api.config.domain.PropertyStoreCode;
25  import de.smartics.properties.api.config.domain.PropertyStoreException;
26  import de.smartics.properties.api.config.domain.PropertyStoreMessageBean;
27  import de.smartics.properties.api.config.domain.SerializableConfigurationPropertiesManagement;
28  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
30  import de.smartics.properties.api.core.security.PropertyValueSecurity;
31  import de.smartics.properties.spi.config.ds.DataSourceException;
32  import de.smartics.properties.spi.config.ds.PropertiesStore;
33  import de.smartics.properties.spi.config.support.AbstractExternalConfigurationPropertiesManagement;
34  import de.smartics.properties.spi.config.support.SerializableConfigurationPropertiesManagementSpi;
35  import de.smartics.util.lang.NullArgumentException;
36  
37  /**
38   * Implementation based on a data source.
39   */
40  @ThreadSafe
41  public final class DataSourceConfigurationProperties extends
42      AbstractExternalConfigurationPropertiesManagement implements
43      SerializableConfigurationPropertiesManagement,
44      SerializableConfigurationPropertiesManagementSpi
45  { // NOPMD
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    /**
51     * The class version identifier.
52     */
53    private static final long serialVersionUID = 1L;
54  
55    // --- members --------------------------------------------------------------
56  
57    /**
58     * The store containing properties.
59     *
60     * @serial
61     */
62    private final PropertiesStore store;
63  
64    /**
65     * The helper to access properties in a property store directly.
66     *
67     * @serial
68     */
69    private final PropertyStoreAccessor propertyStoreAccessor =
70        new DsPropertyStoreAccessor();
71  
72    // ****************************** Initializer *******************************
73  
74    // ****************************** Constructors ******************************
75  
76    /**
77     * Default constructor.
78     *
79     * @param key the key that identifies the configuration.
80     * @param registry the registry to resolve property descriptors.
81     * @param store the store containing properties.
82     * @param decrypter the helper to decrypt secured property values.
83     * @throws NullArgumentException if {@code key}, {@code registry} or
84     *           {@code decrypter} is <code>null</code>.
85     */
86    public DataSourceConfigurationProperties(final ConfigurationKey<?> key,
87        final PropertyDescriptorRegistry registry, final PropertiesStore store,
88        final PropertyValueSecurity decrypter) throws NullArgumentException
89    {
90      super(key, registry, decrypter);
91  
92      this.store = store;
93    }
94  
95    // ****************************** Inner Classes *****************************
96  
97    /**
98     * Provides direct access to the properties in the property store.
99     */
100   private final class DsPropertyStoreAccessor implements PropertyStoreAccessor
101   {
102     /**
103      * The class version identifier.
104      * <p>
105      * The value of this constant is {@value}.
106      * </p>
107      */
108     private static final long serialVersionUID = 1L;
109 
110     @Override
111     public Property getPropertyFromStore(final String name)
112       throws PropertyStoreException
113     {
114       try
115       {
116         final String config = getKey().toString();
117         return store.getProperty(config, name);
118       }
119       catch (final DataSourceException e)
120       {
121         throw new PropertyStoreException(new PropertyStoreMessageBean(
122             PropertyStoreCode.CANNOT_GET_PROPERTY, e, getKey(), name));
123       }
124     }
125 
126     @Override
127     public Property setPropertyToStore(final String name, final String value)
128       throws PropertyStoreException
129     {
130       try
131       {
132         final String config = getKey().toString();
133         final Property oldProperty = getPropertyFromStore(name);
134         store.setProperty(config, name, value);
135         return oldProperty;
136       }
137       catch (final PropertyStoreException e)
138       {
139         throw new PropertyStoreException(new PropertyStoreMessageBean(
140             PropertyStoreCode.CANNOT_SET_PROPERTY, e, getKey(), name));
141       }
142       catch (final DataSourceException e)
143       {
144         throw new PropertyStoreException(new PropertyStoreMessageBean(
145             PropertyStoreCode.CANNOT_SET_PROPERTY, e, getKey(), name));
146       }
147     }
148 
149     @Override
150     public Property deletePropertyInStore(final String name)
151       throws PropertyStoreException
152     {
153       try
154       {
155         final String config = getKey().toString();
156         final Property oldValue = getPropertyFromStore(name);
157         store.deleteProperty(config, name);
158         return oldValue;
159       }
160       catch (final DataSourceException e)
161       {
162         throw new PropertyStoreException(new PropertyStoreMessageBean(
163             PropertyStoreCode.CANNOT_DELETE_PROPERTY, e, getKey(), name));
164       }
165     }
166 
167     @Override
168     public PropertyCollection getPropertyCollectionFromStore()
169       throws PropertyStoreException
170     {
171       try
172       {
173         final String config = getKey().toString();
174         return store.getCollection(config);
175       }
176       catch (final DataSourceException e)
177       {
178         throw new PropertyStoreException(PropertyStoreMessageBean.collection(e,
179             getKey()));
180       }
181     }
182   }
183 
184   // ********************************* Methods ********************************
185 
186   // --- init -----------------------------------------------------------------
187 
188   // --- get&set --------------------------------------------------------------
189 
190   @Override
191   public PropertyStoreAccessor getPropertyStoreAccessor()
192   {
193     return propertyStoreAccessor;
194   }
195 
196   // --- business -------------------------------------------------------------
197 
198   @Override
199   protected void setPropertiesToStore(final PropertyProvider provider)
200   {
201     store.setProperties(provider);
202   }
203 
204   @Override
205   public SerializableConfigurationPropertiesManagement toSerializable()
206   {
207     return this;
208   }
209 
210   // --- object basics --------------------------------------------------------
211 
212 }