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