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.List;
19  
20  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
21  
22  /**
23   * Signals multiple problems with a configuration.
24   */
25  public class CompoundConfigurationException extends ConfigurationException
26  {
27    // ********************************* Fields *********************************
28  
29    // --- constants ------------------------------------------------------------
30  
31    /**
32     * The class version identifier.
33     * <p>
34     * The value of this constant is {@value}.
35     */
36    private static final long serialVersionUID = 1L;
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The list of encountered exceptions.
42     */
43    private final List<ConfigurationException> exceptions;
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Convenience constructor without a message and root cause.
51     *
52     * @param key the key to the configuration that signaled problems.
53     * @param exceptions the list of encountered exceptions.
54     */
55    public CompoundConfigurationException(final ConfigurationKey key,
56        final List<ConfigurationException> exceptions)
57    {
58      this(createMessage(key, exceptions), null, key, exceptions);
59    }
60  
61    /**
62     * Convenience constructor without a root cause.
63     *
64     * @param message the detail message (which is saved for later retrieval by
65     *          the {@link #getMessage()} method).
66     * @param key the key to the configuration that signaled problems.
67     * @param exceptions the list of encountered exceptions.
68     */
69    public CompoundConfigurationException(final String message,
70        final ConfigurationKey key, final List<ConfigurationException> exceptions)
71    {
72      this(message, null, key, exceptions);
73    }
74  
75    /**
76     * Convenience constructor without a message.
77     *
78     * @param cause the cause (which is saved for later retrieval by the
79     *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
80     *          and indicates that the cause is nonexistent or unknown.)
81     * @param key the key to the configuration that signaled problems.
82     * @param exceptions the list of encountered exceptions.
83     */
84    public CompoundConfigurationException(final Throwable cause,
85        final ConfigurationKey key, final List<ConfigurationException> exceptions)
86    {
87      this(null, cause, key, exceptions);
88    }
89  
90    /**
91     * Default constructor.
92     *
93     * @param message the detail message (which is saved for later retrieval by
94     *          the {@link #getMessage()} method).
95     * @param cause the cause (which is saved for later retrieval by the
96     *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
97     *          and indicates that the cause is nonexistent or unknown.)
98     * @param key the key to the configuration that signaled problems.
99     * @param exceptions the list of encountered exceptions.
100    */
101   public CompoundConfigurationException(final String message,
102       final Throwable cause, final ConfigurationKey key,
103       final List<ConfigurationException> exceptions)
104   {
105     super(message, cause, key);
106 
107     this.exceptions = exceptions;
108   }
109 
110   // ****************************** Inner Classes *****************************
111 
112   // ********************************* Methods ********************************
113 
114   // --- init -----------------------------------------------------------------
115 
116   private static String createMessage(final ConfigurationKey key,
117       final List<ConfigurationException> exceptions)
118   {
119     final StringBuilder buffer = new StringBuilder(128);
120     buffer.append("Configuration '" + key + "' has the following problemes:\n");
121 
122     for (final ConfigurationException e : exceptions)
123     {
124       buffer.append(e.getLocalizedMessage()).append('\n');
125     }
126 
127     return buffer.toString();
128   }
129 
130   // --- get&set --------------------------------------------------------------
131 
132   /**
133    * Returns the list of encountered exceptions.
134    *
135    * @return the list of encountered exceptions.
136    */
137   public final List<ConfigurationException> getExceptions()
138   {
139     return exceptions;
140   }
141 
142   // --- business -------------------------------------------------------------
143 
144   // --- object basics --------------------------------------------------------
145 
146 }