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