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.core.domain;
17  
18  import java.io.Serializable;
19  
20  import de.smartics.exceptions.i18n.message.MessageParam;
21  import de.smartics.properties.api.core.app.AbstractBaseMessageBean;
22  
23  /**
24   * Provides information about problems encountered when properties definitions
25   * cannot be parsed.
26   */
27  public final class ConfigMessageBean extends AbstractBaseMessageBean
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    /**
34     * The class version identifier.
35     * <p>
36     * The value of this constant is {@value}.
37     * </p>
38     */
39    private static final long serialVersionUID = 1L;
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The resource that cannot be read.
45     *
46     * @serial
47     */
48    @MessageParam
49    private final String systemId; // NOPMD
50  
51    /**
52     * The two configuration keys where only one is allowed.
53     *
54     * @serial
55     */
56    @MessageParam(":currentDefaultKey")
57    private final DuplicateKeys duplicateKeys; // NOPMD
58  
59    /**
60     * The namespaces that do not match.
61     *
62     * @serial
63     */
64    @MessageParam(":expectedNamespace,:actualNamespace")
65    private final InvalidNamespace invalidNamespace; // NOPMD
66  
67    // ****************************** Initializer *******************************
68  
69    // ****************************** Constructors ******************************
70  
71    /**
72     * Constructor with error code and no cause for exceptions that do no provide
73     * its context as a message bean.
74     *
75     * @param code the error or exception code of the exception.
76     * @param systemId the resource that cannot be read.
77     * @param duplicateKeys the two configuration keys where only one is allowed.
78     * @param invalidNamespace the namespaces that do not match.
79     */
80    private ConfigMessageBean(final ConfigCode code, final String systemId,
81        final DuplicateKeys duplicateKeys, final InvalidNamespace invalidNamespace)
82    {
83      this(code, null, systemId, duplicateKeys, invalidNamespace);
84    }
85  
86    /**
87     * Constructor with cause and code for exceptions that do no provide its
88     * context as a message bean.
89     *
90     * @param code the error or exception code of the exception.
91     * @param cause the cause (which is saved for later retrieval by the
92     *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
93     *          and indicates that the cause is nonexistent or unknown.)
94     * @param systemId the resource that cannot be read.
95     * @param duplicateKeys the two configuration keys where only one is allowed.
96     * @param invalidNamespace the namespaces that do not match.
97     */
98    private ConfigMessageBean(final ConfigCode code, final Throwable cause,
99        final String systemId, final DuplicateKeys duplicateKeys,
100       final InvalidNamespace invalidNamespace)
101   {
102     super(code, cause);
103     this.systemId = systemId;
104     this.duplicateKeys = duplicateKeys;
105     this.invalidNamespace = invalidNamespace;
106   }
107 
108   // ****************************** Inner Classes *****************************
109 
110   /**
111    * Wrapper for duplicate key information.
112    */
113   @SuppressWarnings("unused")
114   private static final class DuplicateKeys implements Serializable
115   {
116     /**
117      * The class version identifier.
118      */
119     private static final long serialVersionUID = 1L;
120 
121     /**
122      * The key that has already been recognized as default.
123      *
124      * @serial
125      */
126     private final String currentDefaultKey;
127 
128     /**
129      * The second key that has been recognized as default. Only one default key
130      * is allowed.
131      *
132      * @serial
133      */
134     private final String duplicateDefaultKey;
135 
136     private DuplicateKeys()
137     {
138       this(null, null);
139     }
140 
141     private DuplicateKeys(final String currentDefaultKey,
142         final String duplicateDefaultKey)
143     {
144       this.currentDefaultKey = currentDefaultKey;
145       this.duplicateDefaultKey = duplicateDefaultKey;
146     }
147 
148     /**
149      * Returns the key that has already been recognized as default.
150      *
151      * @return the key that has already been recognized as default.
152      */
153     public String getCurrentDefaultKey()
154     {
155       return currentDefaultKey;
156     }
157 
158     /**
159      * Returns the second key that has been recognized as default. Only one
160      * default key is allowed.
161      *
162      * @return the second key that has been recognized as default.
163      */
164     public String getDuplicateDefaultKey()
165     {
166       return duplicateDefaultKey;
167     }
168   }
169 
170   /**
171    * Wrapper for clashing namespaces information.
172    */
173   @SuppressWarnings("unused")
174   private static final class InvalidNamespace implements Serializable
175   {
176 
177     /**
178      * The class version identifier.
179      */
180     private static final long serialVersionUID = 1L;
181 
182     /**
183      * The expected namespace.
184      *
185      * @serial
186      */
187     private final String expectedNamespace;
188 
189     /**
190      * The namespace that has actually been encountered.
191      *
192      * @serial
193      */
194     private final String actualNamespace;
195 
196     private InvalidNamespace()
197     {
198       this(null, null);
199     }
200 
201     private InvalidNamespace(final String expectedNamespace,
202         final String actualNamespace)
203     {
204       this.expectedNamespace = expectedNamespace;
205       this.actualNamespace = actualNamespace;
206     }
207 
208     /**
209      * Returns the expected namespace.
210      *
211      * @return the expected namespace.
212      */
213     public String getExpectedNamespace()
214     {
215       return expectedNamespace;
216     }
217 
218     /**
219      * Returns the namespace that has actually been encountered.
220      *
221      * @return the namespace that has actually been encountered.
222      */
223     public String getActualNamespace()
224     {
225       return actualNamespace;
226     }
227   }
228 
229   // ********************************* Methods ********************************
230 
231   // --- init -----------------------------------------------------------------
232 
233   // --- factory --------------------------------------------------------------
234 
235   /**
236    * Creates a message bean without cause and no namespace information.
237    *
238    * @param code the error or exception code of the exception.
239    * @param systemId the resource that cannot be read.
240    * @return a message bean without namespace information.
241    */
242   public static ConfigMessageBean systemId(final ConfigCode code,
243       final String systemId)
244   {
245     return systemId(code, null, systemId);
246   }
247 
248   /**
249    * Creates a message bean without namespace information.
250    *
251    * @param code the error or exception code of the exception.
252    * @param cause the cause (which is saved for later retrieval by the
253    *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
254    *          and indicates that the cause is nonexistent or unknown.)
255    * @param systemId the resource that cannot be read.
256    * @return a message bean without namespace information.
257    */
258   public static ConfigMessageBean systemId(final ConfigCode code,
259       final Throwable cause, final String systemId)
260   {
261     return new ConfigMessageBean(code, cause, systemId, new DuplicateKeys(),
262         new InvalidNamespace());
263   }
264 
265   /**
266    * Creates a message bean signaling a duplicate key problem.
267    *
268    * @param systemId the resource that cannot be read.
269    * @param currentDefaultKey the key that has already been recognized as
270    *          default.
271    * @param duplicateDefaultKey the second key that has been recognized as
272    *          default.
273    * @return a message bean signaling a duplicate key problem.
274    */
275   public static ConfigMessageBean duplicate(final String systemId,
276       final String currentDefaultKey, final String duplicateDefaultKey)
277   {
278     return new ConfigMessageBean(ConfigCode.DUPLICATE_DEFAULT_KEY, null,
279         systemId, new DuplicateKeys(currentDefaultKey, duplicateDefaultKey),
280         new InvalidNamespace());
281   }
282 
283   /**
284    * Creates a message bean signaling a namespace problem.
285    *
286    * @param systemId the resource that cannot be read.
287    * @param expectedNamespace the expected namespace.
288    * @param actualNamespace the namespace that has actually been encountered.
289    * @return a message bean signaling a namespace problem.
290    */
291   public static ConfigMessageBean namespace(final String systemId,
292       final String expectedNamespace, final String actualNamespace)
293   {
294     return new ConfigMessageBean(ConfigCode.WRONG_NAMESPACE, null, systemId,
295         new DuplicateKeys(), new InvalidNamespace(expectedNamespace,
296             actualNamespace));
297   }
298 
299   // --- get&set --------------------------------------------------------------
300 
301   // --- business -------------------------------------------------------------
302 
303   // --- object basics --------------------------------------------------------
304 
305 }