View Javadoc

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