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.properties.spi.core.validate.PropertyValidator;
34  import de.smartics.util.lang.Arg;
35  import de.smartics.util.lang.NullArgumentException;
36  
37  /**
38   * Helper to validate sets of properties. Helpful for
39   * {@link de.smartics.properties.api.config.domain.ConfigurationProperties}.
40   */
41  public final class ConfigurationValidator implements Serializable
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    /**
48     * The class version identifier.
49     */
50    private static final long serialVersionUID = 1L;
51  
52    /**
53     * Reference to the logger for this class.
54     */
55    private static final Logger LOG = LoggerFactory
56        .getLogger(ConfigurationValidator.class);
57  
58    // --- members --------------------------------------------------------------
59  
60    /**
61     * The configuration to validate.
62     */
63    private final ConfigurationPropertiesManagement configuration;
64  
65    /**
66     * The lenient flag that tells the validation process to handle unknown
67     * properties gracefully if set to <code>true</code>. If the value is
68     * <code>false</code> unknown properties are reported as validation failures.
69     */
70    private final boolean lenient;
71  
72    // ****************************** Initializer *******************************
73  
74    // ****************************** Constructors ******************************
75  
76    /**
77     * Default constructor for non-lenient validation.
78     *
79     * @param configuration the configuration to validate.
80     */
81    public ConfigurationValidator(
82        final ConfigurationPropertiesManagement configuration)
83    {
84      this(configuration, false);
85    }
86  
87    /**
88     * Default constructor.
89     *
90     * @param configuration the configuration to validate.
91     * @param lenient the lenient flag that tells the validation process to handle
92     *          unknown properties gracefully if set to <code>true</code>.
93     */
94    public ConfigurationValidator(
95        final ConfigurationPropertiesManagement configuration,
96        final boolean lenient)
97    {
98      this.configuration = configuration;
99      this.lenient = lenient;
100   }
101 
102   // ****************************** Inner Classes *****************************
103 
104   // ********************************* Methods ********************************
105 
106   // --- init -----------------------------------------------------------------
107 
108   // --- get&set --------------------------------------------------------------
109 
110   // --- business -------------------------------------------------------------
111 
112   // CHECKSTYLE:OFF
113   /**
114    * Validates the given properties.
115    *
116    * @param properties the properties to validate.
117    * @throws NullArgumentException if {@code properties} is <code>null</code>.
118    * @throws ConfigurationValidationException if validation fails.
119    */
120   public void validate(final PropertyCollection properties,
121       final Class<?>... groups) // NOPMD
122     throws NullArgumentException, ConfigurationValidationException
123   {// CHECKSTYLE:ON
124     Arg.checkNotNull("properties", properties);
125 
126     final List<PropertyException> propertyViolations =
127         new ArrayList<PropertyException>();
128     final UnknownProperties unvalidatedProperties = new UnknownProperties();
129     final List<PropertyDescriptor> mandatoryProperties =
130         configuration.getMandatoryPropertyDescriptors();
131 
132     try
133     {
134       for (final Class<?> group : calcGroups(groups))
135       {
136         for (final Property property : properties)
137         {
138           final String key = property.getName();
139 
140           try
141           {
142             final PropertyDescriptor descriptor =
143                 configuration.getDescriptor(key);
144 
145             try
146             {
147               final Object value = configuration.getPropertyAsType(descriptor);
148               validate(descriptor, value, group);
149               mandatoryProperties.remove(descriptor);
150             }
151             catch (final PropertyException e)
152             {
153               if (property.getValue() != null)
154               {
155                 propertyViolations.add(e);
156               }
157             }
158           }
159           catch (final UnknownPropertyException e)
160           {
161             unvalidatedProperties.add(property);
162           }
163         }
164       }
165     }
166     finally
167     {
168       properties.close();
169     }
170 
171     if (isExceptionToBeThrown(propertyViolations, mandatoryProperties,
172         unvalidatedProperties))
173     {
174       throw new ConfigurationValidationException(configuration.getKey(),
175           propertyViolations, mandatoryProperties, unvalidatedProperties);
176     }
177 
178     if (!unvalidatedProperties.isEmpty() && !lenient)
179     {
180       LOG.warn("Encountered unknown properies: {}", unvalidatedProperties);
181     }
182   }
183 
184   private Class<?>[] calcGroups(final Class<?>[] groups)
185   {
186     if (groups != null && groups.length > 0)
187     {
188       return groups;
189     }
190     return new Class[] { null };
191   }
192 
193   /**
194    * Validates the given value for the given descriptor and groups.
195    *
196    * @param descriptor the descriptor to determine the constraints to check.
197    * @param value the value to check.
198    * @param groups the constraint groups to consider. If empty or
199    *          <code>null</code> only the constraints in the default are checked.
200    */
201   public void validate(final PropertyDescriptor descriptor, final Object value,
202       final Class<?>... groups)
203   {
204     final PropertyValidator validator =
205         new PropertyValidator(configuration.isInAdminMode());
206     validator.validate(descriptor, value, groups);
207   }
208 
209   private boolean isExceptionToBeThrown(
210       final List<PropertyException> propertyViolations,
211       final List<PropertyDescriptor> mandatoryProperties,
212       final UnknownProperties unvalidatedProperties)
213   {
214     boolean exceptional =
215         !(propertyViolations.isEmpty() && mandatoryProperties.isEmpty());
216     if (!exceptional)
217     {
218       exceptional = !(lenient || unvalidatedProperties.isEmpty());
219     }
220     return exceptional;
221   }
222 
223   // --- object basics --------------------------------------------------------
224 
225 }