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.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
23  import de.smartics.properties.api.config.domain.DescribedProperty;
24  import de.smartics.properties.api.config.domain.ValidatedProperty;
25  import de.smartics.properties.api.core.domain.PropertyDescriptor;
26  import de.smartics.properties.api.core.domain.PropertyValidationException;
27  import de.smartics.properties.api.core.domain.PropertyValueConversionException;
28  
29  /**
30   * The configuration of properties associated with a unique key.
31   */
32  public final class PropertiesConfiguration
33  {
34    // ******************************** Fields ********************************
35  
36    // --- constants ----------------------------------------------------------
37  
38    // --- members ------------------------------------------------------------
39  
40    /**
41     * The application the keys are part of.
42     */
43    private final ManagedApplication application;
44  
45    /**
46     * The unique identifier of the configuration.
47     */
48    private final String key;
49  
50    /**
51     * The properties of the configuration.
52     */
53    private final ConfigurationPropertiesManagement configurationProperties;
54  
55    // ***************************** Initializer ******************************
56  
57    // ***************************** Constructors *****************************
58  
59    /**
60     * Default constructor.
61     *
62     * @param application the application the keys are part of.
63     * @param key the unique identifier of the configuration.
64     * @param configurationProperties the properties of the configuration.
65     */
66    public PropertiesConfiguration(final ManagedApplication application,
67        final String key,
68        final ConfigurationPropertiesManagement configurationProperties)
69    {
70      this.application = application;
71      this.key = key;
72      this.configurationProperties = configurationProperties;
73    }
74  
75    // ***************************** Inner Classes ****************************
76  
77    // ******************************** Methods *******************************
78  
79    // --- init ---------------------------------------------------------------
80  
81    // --- get&set ------------------------------------------------------------
82  
83    /**
84     * Returns the unique identifier of the configuration.
85     *
86     * @return the unique identifier of the configuration.
87     */
88    public String getKey()
89    {
90      return key;
91    }
92  
93    /**
94     * Returns the properties of the configuration.
95     *
96     * @return the properties of the configuration.
97     */
98    public ConfigurationPropertiesManagement getConfigurationProperties()
99    {
100     return configurationProperties;
101   }
102 
103   /**
104    * Returns the application the keys are part of.
105    *
106    * @return the application the keys are part of.
107    */
108   public ManagedApplication getApplication()
109   {
110     return application;
111   }
112 
113   // --- business -----------------------------------------------------------
114 
115   /**
116    * Returns the list of properties. If the property can be resolved, an
117    * instance of {@link ValidatedProperty} is added. If it cannot be resolved or
118    * its value is <code>null</code>, an instance of {@link DescribedProperty} is
119    * added.
120    *
121    * @return list of properties.
122    */
123   public List<DescribedProperty> getProperties()
124   {
125     final List<DescribedProperty> properties =
126         new ArrayList<DescribedProperty>();
127 
128     for (final PropertyDescriptor descriptor : this.configurationProperties
129         .getRegistry())
130     {
131       final DescribedProperty property =
132           this.configurationProperties.getProperty(descriptor);
133       if (null != property.getValue())
134       {
135         try
136         {
137           final ValidatedProperty validatedProperty =
138               this.configurationProperties.getValidatedProperty(descriptor,
139                   null);
140           properties.add(validatedProperty);
141           continue;
142         }
143         catch (final PropertyValidationException e)
144         {
145           // Ignore and add property later.
146         }
147         catch (final PropertyValueConversionException e)
148         {
149           // Ignore and add property later.
150         }
151       }
152 
153       properties.add(property);
154     }
155 
156     Collections.sort(properties);
157 
158     return properties;
159   }
160 
161   // --- object basics ------------------------------------------------------
162 }