View Javadoc

1   /*
2    * Copyright 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.admin.domain.model;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Set;
22  import java.util.TreeSet;
23  
24  import javax.annotation.CheckForNull;
25  
26  import de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader;
27  import de.smartics.properties.admin.domain.service.ConfigurationService;
28  import de.smartics.properties.admin.domain.service.RemoteConfigurationPropertiesManagementFactory;
29  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
30  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
31  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
32  import de.smartics.properties.api.core.security.PropertyValueSecurity;
33  import de.smartics.properties.impl.config.ds.DataSourceConfiguration;
34  import de.smartics.properties.impl.config.ds.DataSourceConfigurationLoader;
35  import de.smartics.properties.resource.domain.ArtifactId;
36  import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContext;
37  import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager;
38  
39  /**
40   * The application, managed by this REST service implementation.
41   */
42  public class ManagedApplication implements Serializable
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    /**
49     * The class version identifier.
50     * <p>
51     * The value of this constant is {@value}.
52     */
53    private static final long serialVersionUID = 1L;
54  
55    // --- members --------------------------------------------------------------
56  
57    /**
58     * The singleton. The REST API is only accessing properties of one
59     * application.
60     */
61    private static ManagedApplication instance;
62  
63    /**
64     * The identifier of the application managed.
65     *
66     * @serial
67     */
68    private final ArtifactId applicationId;
69  
70    /**
71     * The data source that contains the property definitions.
72     *
73     * @serial
74     */
75    private final DataSourceConfiguration dataSourceConfig;
76  
77    /**
78     * The factory to manage configurations for the application.
79     *
80     * @serial
81     */
82    private final RemoteConfigurationPropertiesManagementFactory factory;
83  
84    /**
85     * The set of keys that have been encountered in the applications lifetime.
86     */
87    @SuppressWarnings({ "unchecked", "rawtypes" })
88    private final Set<ConfigurationKey<?>> configurationKeys = Collections
89        .synchronizedSet(new TreeSet());
90  
91    // ****************************** Initializer *******************************
92  
93    private static void initialize()
94      throws CannotInitializeManagedApplicationException
95    {
96      try
97      {
98        final ArtifactId applicationId =
99            new ApplicationIdConfigurationLoader().load();
100       final DataSourceConfiguration dataSourceConfig =
101           loadDataSourceConfiguration();
102       final RemoteConfigurationPropertiesManagementFactory factory =
103           new ConfigurationService().createFactory(applicationId,
104               dataSourceConfig);
105       instance =
106           new ManagedApplication(applicationId, dataSourceConfig, factory);
107     }
108     catch (final Exception e)
109     {
110       throw new CannotInitializeManagedApplicationException(e);
111     }
112   }
113 
114   private static DataSourceConfiguration loadDataSourceConfiguration()
115   {
116     try
117     {
118       return new DataSourceConfigurationLoader().load();
119     }
120     catch (final IllegalStateException e)
121     {
122       return null;
123     }
124   }
125 
126   // ****************************** Constructors ******************************
127 
128   /**
129    * Default constructor.
130    *
131    * @param applicationId the identifier of the application managed.
132    * @param dataSourceConfig the data source that contains the property
133    *          definitions.
134    * @param factory the factory to manage configurations for the application.
135    */
136   public ManagedApplication(final ArtifactId applicationId,
137       final DataSourceConfiguration dataSourceConfig,
138       final RemoteConfigurationPropertiesManagementFactory factory)
139   {
140     this.applicationId = applicationId;
141     this.dataSourceConfig = dataSourceConfig;
142     this.factory = factory;
143     configurationKeys.addAll(factory.getRegisteredConfigurationKeys());
144   }
145 
146   // ****************************** Inner Classes *****************************
147 
148   // ********************************* Methods ********************************
149 
150   // --- init -----------------------------------------------------------------
151 
152   // --- get&set --------------------------------------------------------------
153 
154   /**
155    * Returns the identifier of the application managed.
156    *
157    * @return the identifier of the application managed.
158    */
159   public ArtifactId getApplicationId()
160   {
161     return applicationId;
162   }
163 
164   /**
165    * Returns the data source that contains the property definitions.
166    *
167    * @return the data source that contains the property definitions.
168    */
169   @CheckForNull
170   public DataSourceConfiguration getDataSourceConfig()
171   {
172     return dataSourceConfig;
173   }
174 
175   /**
176    * Returns the URL to the remote repository.
177    *
178    * @return the URL to the remote repository.
179    */
180   public String getRemoteRepositoryUrl()
181   {
182     return factory.getRemoteUrl();
183   }
184 
185   /**
186    * Returns the security component to encrypt and decrypt values.
187    *
188    * @return the security component to encrypt and decrypt values.
189    */
190   public PropertyValueSecurity getSecurity()
191   {
192     return factory.getFactoryConfiguration().getDecrypter();
193   }
194 
195   /**
196    * Returns the registry of known descriptors.
197    *
198    * @return the registry of known descriptors.
199    */
200   public PropertyDescriptorRegistry getDescriptorRegistry()
201   {
202     return factory.getRegistry();
203   }
204 
205   // --- business -------------------------------------------------------------
206 
207   /**
208    * Provides access to the managed application.
209    *
210    * @return the managed application.
211    * @throws CannotInitializeManagedApplicationException if the application was
212    *           not and cannot be initialized.
213    */
214   public static ManagedApplication getApplication()
215     throws CannotInitializeManagedApplicationException
216   {
217     if (instance == null)
218     {
219       initialize();
220     }
221     return instance;
222   }
223 
224   /**
225    * Provides access to configurations.
226    *
227    * @param key the key to the requested configuration.
228    * @return the requested configuration.
229    */
230   public ConfigurationPropertiesManagement getConfiguration(
231       final ConfigurationKey<?> key)
232   {
233     final ConfigurationPropertiesManagement config = factory.create(key);
234     configurationKeys.add(key);
235     return config;
236   }
237 
238   /**
239    * Provides access to configurations.
240    *
241    * @param keyString the string representation of the key to the requested
242    *          configuration.
243    * @return the requested configuration.
244    */
245   public ConfigurationPropertiesManagement getConfiguration(
246       final String keyString)
247   {
248     final ConfigurationKeyContext context =
249         ConfigurationKeyContextManager.INSTANCE.context();
250     final ConfigurationKey<?> key =
251         context.configurationKeyFactory().createKeyFromString(keyString);
252     final ConfigurationPropertiesManagement config = getConfiguration(key);
253     return config;
254   }
255 
256   /**
257    * Returns the collection of configuration keys.
258    *
259    * @return the collection of configuration keys.
260    */
261   public Collection<ConfigurationKey<?>> getConfigurationKeys()
262   {
263     return Collections.unmodifiableCollection(configurationKeys);
264   }
265 
266   /**
267    * Cleans the definitions from the factory.
268    */
269   public void clean()
270   {
271     for (final ConfigurationKey<?> key : factory
272         .getRegisteredConfigurationKeys())
273     {
274       factory.remove(key);
275     }
276 
277     configurationKeys.clear();
278 
279     factory.release();
280 
281     instance = null;
282   }
283   // --- object basics --------------------------------------------------------
284 
285 }