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