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.io.Serializable;
19  import java.util.Date;
20  
21  import de.smartics.exceptions.core.Code;
22  import de.smartics.exceptions.core.ExceptionId;
23  import de.smartics.exceptions.core.IdFactory;
24  import de.smartics.exceptions.core.ThrowableHandleMode;
25  import de.smartics.exceptions.runtime.ExceptionContextManager;
26  
27  /**
28   * Contains the information commonly used by runtime and checked exceptions.
29   *
30   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
31   * @version $Revision:591 $
32   */
33  class ExceptionInfo implements Serializable
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * The class version identifier.
41     */
42    private static final long serialVersionUID = 3L;
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The reference to the cause is stored to be able to remove it later. This is
48     * useful to strip all Exceptions from the cause that are not suited to be
49     * transfered to a remote destination. Especially exceptions of server side
50     * packages can be removed to be not sent to the client.
51     *
52     * @note Instances of this class are supposed to be referenced by exception
53     *       instances and should therefore be constant value objects. The feature
54     *       of truncation requires this field to be none-final. It is protected
55     *       to allow only members of the package to manipulate it.
56     * @serial
57     */
58    protected Throwable removeableCause;
59  
60    /**
61     * The unique identifier of the exception. The identifier is used to track an
62     * exception in different tiers.
63     * <p>
64     * This identifier must never be <code>null</code>.
65     *
66     * @serial
67     */
68    protected final ExceptionId<?> id;
69  
70    /**
71     * The error or exception code of the exception.
72     * <p>
73     * This code must never be <code>null</code>.
74     *
75     * @serial
76     */
77    protected final Code code;
78  
79    /**
80     * The time the exception has been raised. The time instance is created and
81     * stored as soon as the exception is constructed.
82     *
83     * @serial
84     */
85    private final Date time;
86  
87    // ****************************** Initializer *******************************
88  
89    // ****************************** Constructors ******************************
90  
91    /**
92     * Default constructor.
93     *
94     * @param cause the cause (which is saved for later retrieval by the
95     *          {@link #getCause()} method). (A <tt>null</tt> value is permitted,
96     *          and indicates that the cause is nonexistent or unknown.)
97     * @param code the error or exception code of the exception.
98     * @throws NullPointerException if the code is <code>null</code>.
99     */
100   protected ExceptionInfo(final Throwable cause, final Code code)
101       throws NullPointerException
102   {
103     if (code == null)
104     {
105       throw new NullPointerException("The code must not be 'null'!");
106     }
107     this.removeableCause = Helper.getThrowableHandleMode() != ThrowableHandleMode.NORMAL
108         ? cause : null;
109     this.time = new Date();
110     this.id = provideId(cause);
111     this.code = code;
112   }
113 
114   // ****************************** Inner Classes *****************************
115 
116   // ********************************* Methods ********************************
117 
118   // --- init -----------------------------------------------------------------
119 
120   /**
121    * Checks if the cause already has an UUID and returns it if present.
122    * Otherwise it returns a new generated UUID.
123    *
124    * @param cause the cause to check for an identifier.
125    * @return the UUID of the given cause if it has one, a new UUID otherwise.
126    */
127   private static ExceptionId<?> provideId(final Throwable cause)
128   {
129     ExceptionId<?> id = Helper.determineParentId(cause);
130 
131     if (id == null)
132     {
133       final IdFactory factory = ExceptionContextManager.getFactory(Thread
134           .currentThread().getContextClassLoader());
135       id = factory.createId();
136     }
137 
138     return id;
139   }
140 
141   // --- get&set --------------------------------------------------------------
142 
143   /**
144    * Returns the unique identifier of the exception. The identifier is used to
145    * track an exception in different tiers.
146    * <p>
147    * This identifier must never be <code>null</code>.
148    *
149    * @return the unique identifier of the exception.
150    */
151   public ExceptionId<?> getId()
152   {
153     return this.id;
154   }
155 
156   /**
157    * Returns the error or exception code of the exception.
158    * <p>
159    * This code must never be <code>null</code>.
160    *
161    * @return the error or exception code of the exception.
162    */
163   public Code getCode()
164   {
165     return this.code;
166   }
167 
168   /**
169    * Returns the time the exception has been raised. The time instance is
170    * created and stored as soon as the exception is constructed.
171    *
172    * @return the time the exception has been raised.
173    */
174   public Date getTime()
175   {
176     return time;
177   }
178 
179   // --- business -------------------------------------------------------------
180 
181   // --- object basics --------------------------------------------------------
182 
183   /**
184    * {@inheritDoc}
185    * <p>
186    * Returns the identifier, code and time information as a {@link String}.
187    */
188   @Override
189   public String toString()
190   {
191     return this.id + " (" + this.code + ") - " + time;
192   }
193 }