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.api.config.domain;
17  
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.Locale;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import de.smartics.exceptions.i18n.message.MessageParam;
25  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
26  import de.smartics.properties.api.core.domain.PropertyDescriptor;
27  import de.smartics.properties.api.core.domain.PropertyException;
28  
29  /**
30   * Signals that the configuration is invalid or at least properties cannot be
31   * validated because there are no properties descriptors registered for them.
32   *
33   * @impl Note that the message text is not part of the public API and may change
34   *       without notice.
35   */
36  public class ConfigurationValidationException extends ConfigurationException
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * The class version identifier.
44     * <p>
45     * The value of this constant is {@value}.
46     * </p>
47     */
48    private static final long serialVersionUID = 1L;
49  
50    // --- members --------------------------------------------------------------
51  
52    /**
53     * The list of violations.
54     *
55     * @serial
56     */
57    private final List<PropertyException> propertyViolations;
58  
59    /**
60     * The list of mandatory properties for which no value has been provided.
61     *
62     * @serial
63     */
64    private final List<PropertyDescriptor> mandatoryProperties;
65  
66    /**
67     * The list of unknown properties.
68     *
69     * @serial
70     */
71    private final UnknownProperties unknownProperties;
72  
73    // ****************************** Initializer *******************************
74  
75    // ****************************** Constructors ******************************
76  
77    /**
78     * Convenience constructor without root cause.
79     *
80     * @param key the key to the configuration that signaled problems.
81     * @param propertyViolations the list of violations.
82     * @param mandatoryProperties the list of mandatory properties for which no
83     *          value has been provided.
84     * @param unknownProperties the list of unknown properties.
85     */
86    public ConfigurationValidationException(final ConfigurationKey<?> key,
87        final List<PropertyException> propertyViolations,
88        final List<PropertyDescriptor> mandatoryProperties,
89        final UnknownProperties unknownProperties)
90    {
91      this(null, key, propertyViolations, mandatoryProperties, unknownProperties);
92    }
93  
94    /**
95     * Default Constructor.
96     *
97     * @param cause the cause (which is saved for later retrieval by the
98     *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
99     *          and indicates that the cause is nonexistent or unknown.)
100    * @param key the key to the configuration that signaled problems.
101    * @param propertyViolations the list of violations.
102    * @param mandatoryProperties the list of mandatory properties for which no
103    *          value has been provided.
104    * @param unknownProperties the list of unknown properties.
105    */
106   public ConfigurationValidationException(final Throwable cause,
107       final ConfigurationKey<?> key,
108       final List<PropertyException> propertyViolations,
109       final List<PropertyDescriptor> mandatoryProperties,
110       final UnknownProperties unknownProperties)
111   {
112     super(ConfigurationCode.INVALID_CONFIGURATION, cause, key);
113     this.propertyViolations = propertyViolations;
114     this.mandatoryProperties = mandatoryProperties;
115     this.unknownProperties = unknownProperties;
116   }
117 
118   // ****************************** Inner Classes *****************************
119 
120   // ********************************* Methods ********************************
121 
122   // --- init -----------------------------------------------------------------
123 
124   // --- get&set --------------------------------------------------------------
125 
126   /**
127    * Returns the list of violations.
128    *
129    * @return the list of violations.
130    */
131   public final List<PropertyException> getPropertyViolations()
132   {
133     return Collections.unmodifiableList(propertyViolations);
134   }
135 
136   /**
137    * Returns the list of mandatory properties for which no value has been
138    * provided.
139    *
140    * @return the list of mandatory properties for which no value has been
141    *         provided.
142    */
143   public final List<PropertyDescriptor> getMandatoryProperties()
144   {
145     return Collections.unmodifiableList(mandatoryProperties);
146   }
147 
148   /**
149    * Returns the list of unknown properties.
150    *
151    * @return the list of unknown properties.
152    */
153   public final UnknownProperties getUnknownProperties()
154   {
155     return unknownProperties;
156   }
157 
158   // --- business -------------------------------------------------------------
159 
160   /**
161    * Returns the violations as message text.
162    *
163    * @param locale the locale to use.
164    * @return the violations as message text.
165    */
166   @MessageParam("violations")
167   public final String getViolationsAsText(final Locale locale)
168   {
169     final StringBuilder buffer = new StringBuilder();
170     if (propertyViolations != null && !propertyViolations.isEmpty())
171     {
172       for (final PropertyException violation : propertyViolations)
173       {
174         buffer.append("  ").append(violation.getLocalizedMessage(locale))
175             .append('\n');
176       }
177     }
178     return StringUtils.chomp(buffer.toString());
179   }
180 
181   /**
182    * Returns the count of violations.
183    *
184    * @return the count of violations.
185    */
186   @MessageParam("violationCount")
187   public final int getViolationCount()
188   {
189     return propertyViolations != null ? propertyViolations.size() : 0;
190   }
191 
192   /**
193    * Returns the missing mandatory properties as message text.
194    *
195    * @return the missing mandatory properties as message text.
196    */
197   @MessageParam("mandatoryProperties")
198   public final String getMandatoryPropertiesAsText()
199   {
200     final StringBuilder buffer = new StringBuilder();
201     if (mandatoryProperties != null && !mandatoryProperties.isEmpty())
202     {
203       for (final PropertyDescriptor descriptor : mandatoryProperties)
204       {
205         buffer.append("  ").append(descriptor.getKey()).append('\n');
206       }
207     }
208     return StringUtils.chomp(buffer.toString());
209   }
210 
211   /**
212    * Returns the count of missing mandatory properties.
213    *
214    * @return the count of missing mandatory properties.
215    */
216   @MessageParam("mandatoryPropertiesCount")
217   public final int getMandatoryPropertiesCount()
218   {
219     return mandatoryProperties != null ? mandatoryProperties.size() : 0;
220   }
221 
222   /**
223    * Returns the unknown properties as message text.
224    *
225    * @return the unknown mandatory properties as message text.
226    */
227   @MessageParam("unknownProperties")
228   public final String getUnknownPropertiesAsText()
229   {
230     final StringBuilder buffer = new StringBuilder();
231     if (unknownProperties != null && !unknownProperties.isEmpty())
232     {
233       for (final Property property : unknownProperties.getProperties())
234       {
235         buffer.append("  ").append(property).append('\n');
236       }
237     }
238     return StringUtils.chomp(buffer.toString());
239   }
240 
241   /**
242    * Returns the count of unknown properties.
243    *
244    * @return the count of unknown mandatory properties.
245    */
246   @MessageParam("unknownPropertiesCount")
247   public final int getUnknownPropertiesCount()
248   {
249     return unknownProperties != null ? unknownProperties.getProperties().size()
250         : 0;
251   }
252 
253   // --- object basics --------------------------------------------------------
254 }