View Javadoc

1   /*
2    * Copyright 2007-2011 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.exceptions;
17  
18  import java.util.Date;
19  
20  import de.smartics.exceptions.core.Code;
21  import de.smartics.exceptions.core.ExceptionId;
22  import de.smartics.exceptions.core.ThrowableHandleMode;
23  
24  /**
25   * The base implementation of the core runtime exception that supports unique
26   * identifying and exception codes for unchecked exceptions.
27   *
28   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
29   * @version $Revision$
30   */
31  public abstract class AbstractCoreRuntimeException extends RuntimeException
32      implements CoreException
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    /**
39     * The class version identifier.
40     */
41    private static final long serialVersionUID = 4L;
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * Reference to common exception information. Currently this is the exception
47     * identifier and the exception code.
48     *
49     * @serial
50     */
51    protected final ExceptionInfo info; // NOPMD
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Constructor.
59     *
60     * @param code the error or exception code of the exception.
61     * @see AbstractCoreRuntimeException#AbstractCoreRuntimeException(String,
62     *      Throwable, Code)
63     */
64    protected AbstractCoreRuntimeException(final Code code)
65    {
66      this(null, null, code);
67    }
68  
69    /**
70     * Constructor.
71     *
72     * @param message the detail message (which is saved for later retrieval by
73     *          the {@link #getMessage()} method). Only used if there is no
74     *          localised message in the bundle.
75     * @param code the error or exception code of the exception.
76     * @see AbstractCoreRuntimeException#AbstractCoreRuntimeException(String,
77     *      Throwable, Code)
78     */
79    protected AbstractCoreRuntimeException(final String message, final Code code)
80    {
81      this(message, null, code);
82    }
83  
84    /**
85     * Constructor.
86     *
87     * @param cause the cause (which is saved for later retrieval by the
88     *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
89     *          and indicates that the cause is nonexistent or unknown.)
90     * @param code the error or exception code of the exception.
91     * @see AbstractCoreRuntimeException#AbstractCoreRuntimeException(String,
92     *      Throwable, Code)
93     */
94    protected AbstractCoreRuntimeException(final Throwable cause, final Code code)
95    {
96      this(null, cause, code);
97    }
98  
99    /**
100    * Constructor.
101    *
102    * @param message the detail message (which is saved for later retrieval by
103    *          the {@link #getMessage()} method). Only used if there is no
104    *          localised message in the bundle.
105    * @param cause the cause (which is saved for later retrieval by the
106    *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
107    *          and indicates that the cause is nonexistent or unknown.)
108    * @param code the error or exception code of the exception.
109    * @throws NullPointerException if the code is <code>null</code>.
110    * @see java.lang.RuntimeException#RuntimeException(java.lang.String,java.lang.Throwable)
111    */
112   protected AbstractCoreRuntimeException(final String message,
113       final Throwable cause, final Code code) throws NullPointerException
114   {
115     super(message, Helper.provideException(cause));
116     this.info = new ExceptionInfo(cause, code);
117   }
118 
119   // ****************************** Inner Classes *****************************
120 
121   // ********************************* Methods ********************************
122 
123   // --- init -----------------------------------------------------------------
124 
125   // --- get&set --------------------------------------------------------------
126 
127   /**
128    * Returns the unique identifier of the exception. The identifier is used to
129    * track an exception in different tiers.
130    * <p>
131    * This identifier must never be <code>null</code>.
132    *
133    * @return the unique identifier of the exception.
134    */
135   public ExceptionId<?> getId()
136   {
137     return info.getId();
138   }
139 
140   /**
141    * Returns the error or exception code of the exception.
142    * <p>
143    * This code must never be <code>null</code>.
144    *
145    * @return the error or exception code of the exception.
146    */
147   public Code getCode()
148   {
149     return info.getCode();
150   }
151 
152   /**
153    * {@inheritDoc}
154    *
155    * @see de.smartics.exceptions.CoreException#getTime()
156    */
157   public Date getTime()
158   {
159     return info.getTime();
160   }
161 
162   /**
163    * {@inheritDoc}
164    *
165    * @see java.lang.Throwable#getCause()
166    */
167   @Override
168   public Throwable getCause()
169   {
170     if (Helper.getThrowableHandleMode() == ThrowableHandleMode.NORMAL)
171     {
172       return super.getCause();
173     }
174     else
175     {
176       return info.removeableCause;
177     }
178   }
179 
180   // --- business -------------------------------------------------------------
181 
182   /**
183    * {@inheritDoc}
184    *
185    * @see de.smartics.exceptions.CoreException#truncateCause()
186    */
187   public void truncateCause()
188   {
189     Helper.truncateCause(this, this.info);
190   }
191 
192   // --- object basics --------------------------------------------------------
193 
194   /**
195    * Returns the string representation of the object for debugging.
196    *
197    * @return the string representation of the object.
198    */
199   public String toString()
200   {
201     final StringBuilder buffer = new StringBuilder();
202     final String message = getLocalizedMessage();
203     buffer.append(this.info);
204     if (message != null)
205     {
206       buffer.append(": ").append(message);
207     }
208 
209     return buffer.toString();
210   }
211 }