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.validation;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
26  import de.smartics.properties.api.config.domain.ConfigurationValidationException;
27  import de.smartics.properties.api.config.domain.Property;
28  import de.smartics.properties.api.config.domain.PropertyCollection;
29  import de.smartics.properties.api.config.domain.UnknownProperties;
30  import de.smartics.properties.api.config.domain.UnknownPropertyException;
31  import de.smartics.properties.api.core.domain.PropertyDescriptor;
32  import de.smartics.properties.api.core.domain.PropertyException;
33  import de.smartics.util.lang.Arguments;
34  import de.smartics.util.lang.NullArgumentException;
35  
36  /**
37   * Helper to validate sets of properties. Helpful for
38   * {@link de.smartics.properties.api.config.domain.ConfigurationProperties}.
39   */
40  public final class ConfigurationValidator implements Serializable
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    /**
47     * The class version identifier.
48     */
49    private static final long serialVersionUID = 1L;
50  
51    /**
52     * Reference to the logger for this class.
53     */
54    private static final Logger LOG = LoggerFactory
55        .getLogger(ConfigurationValidator.class);
56  
57    // --- members --------------------------------------------------------------
58  
59    /**
60     * The configuration to validate.
61     */
62    private final ConfigurationPropertiesManagement configuration;
63  
64    /**
65     * The lenient flag that tells the validation process to handle unknown
66     * properties gracefully if set to <code>true</code>. If the value is
67     * <code>false</code> unknown properties are reported as validation failures.
68     */
69    private final boolean lenient;
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    /**
76     * Default constructor for non-lenient validation.
77     *
78     * @param configuration the configuration to validate.
79     */
80    public ConfigurationValidator(
81        final ConfigurationPropertiesManagement configuration)
82    {
83      this(configuration, false);
84    }
85  
86    /**
87     * Default constructor.
88     *
89     * @param configuration the configuration to validate.
90     * @param lenient the lenient flag that tells the validation process to handle
91     *          unknown properties gracefully if set to <code>true</code>.
92     */
93    public ConfigurationValidator(
94        final ConfigurationPropertiesManagement configuration,
95        final boolean lenient)
96    {
97      this.configuration = configuration;
98      this.lenient = lenient;
99    }
100 
101   // ****************************** Inner Classes *****************************
102 
103   // ********************************* Methods ********************************
104 
105   // --- init -----------------------------------------------------------------
106 
107   // --- get&set --------------------------------------------------------------
108 
109   // --- business -------------------------------------------------------------
110 
111   // CHECKSTYLE:OFF
112   /**
113    * Validates the given properties.
114    *
115    * @param properties the properties to validate.
116    * @throws NullArgumentException if {@code properties} is <code>null</code>.
117    * @throws ConfigurationValidationException if validation fails.
118    */
119   public void validate(final PropertyCollection properties) // NOPMD
120     throws NullArgumentException, ConfigurationValidationException
121   {// CHECKSTYLE:ON
122     Arguments.checkNotNull("properties", properties);
123 
124     final List<PropertyException> propertyViolations =
125         new ArrayList<PropertyException>();
126     final UnknownProperties unvalidatedProperties = new UnknownProperties();
127     final List<PropertyDescriptor> mandatoryProperties =
128         configuration.getMandatoryPropertyDescriptors();
129 
130     for (final Property property : properties)
131     {
132       final String key = property.getName();
133 
134       try
135       {
136         final PropertyDescriptor descriptor = configuration.getDescriptor(key);
137 
138         try
139         {
140           configuration.getPropertyValue(descriptor);
141           mandatoryProperties.remove(descriptor);
142         }
143         catch (final PropertyException e)
144         {
145           if (property.getValue() != null)
146           {
147             propertyViolations.add(e);
148           }
149         }
150       }
151       catch (final UnknownPropertyException e)
152       {
153         unvalidatedProperties.add(property);
154       }
155     }
156 
157     if (isExceptionToBeThrown(propertyViolations, mandatoryProperties,
158         unvalidatedProperties))
159     {
160       throw new ConfigurationValidationException(configuration.getKey(),
161           propertyViolations, mandatoryProperties, unvalidatedProperties);
162     }
163 
164     if (!unvalidatedProperties.isEmpty() && !lenient)
165     {
166       LOG.warn("Encountered unknown properies: {}", unvalidatedProperties);
167     }
168   }
169 
170   private boolean isExceptionToBeThrown(
171       final List<PropertyException> propertyViolations,
172       final List<PropertyDescriptor> mandatoryProperties,
173       final UnknownProperties unvalidatedProperties)
174   {
175     boolean exceptional =
176         !(propertyViolations.isEmpty() && mandatoryProperties.isEmpty());
177     if (!exceptional)
178     {
179       exceptional = !(lenient || unvalidatedProperties.isEmpty());
180     }
181     return exceptional;
182   }
183 
184   // --- object basics --------------------------------------------------------
185 
186 }