1 | /* |
2 | * jDTAUS Core API |
3 | * Copyright (C) 2005 Christian Schulte |
4 | * <cs@schulte.it> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * |
20 | */ |
21 | package org.jdtaus.core.lang; |
22 | |
23 | import java.util.EventObject; |
24 | |
25 | /** |
26 | * Event holding an exception. |
27 | * |
28 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
29 | * @version $JDTAUS: ExceptionEvent.java 8641 2012-09-27 06:45:17Z schulte $ |
30 | * |
31 | * @see ExceptionEventSource |
32 | */ |
33 | public class ExceptionEvent extends EventObject |
34 | { |
35 | //--Constants--------------------------------------------------------------- |
36 | |
37 | /** Serial version UID for backwards compatibility with 1.4.x classes. */ |
38 | private static final long serialVersionUID = 5909424199091260187L; |
39 | |
40 | //---------------------------------------------------------------Constants-- |
41 | //--Constructors------------------------------------------------------------ |
42 | |
43 | /** |
44 | * Creates a new {@code ExceptionEvent} instance taking an exception and |
45 | * a corresponding thread. |
46 | * |
47 | * @param source the source of the new event. |
48 | * @param thread the thread {@code throwable} occured in. |
49 | * @param throwable the exception which occured in {@code thread}. |
50 | * |
51 | * @throws NullPointerException if either {@code thread} or |
52 | * {@code throwable} is {@code null}. |
53 | */ |
54 | public ExceptionEvent( final Object source, final Thread thread, |
55 | final Throwable throwable ) |
56 | { |
57 | super( source ); |
58 | |
59 | if ( thread == null ) |
60 | { |
61 | throw new NullPointerException( "thread" ); |
62 | } |
63 | if ( throwable == null ) |
64 | { |
65 | throw new NullPointerException( "throwable" ); |
66 | } |
67 | |
68 | this.thread = thread; |
69 | this.throwable = throwable; |
70 | } |
71 | |
72 | //------------------------------------------------------------Constructors-- |
73 | //--ExceptionEvent---------------------------------------------------------- |
74 | |
75 | /** Thread {@code throwable} occured in. */ |
76 | private transient Thread thread; |
77 | |
78 | /** |
79 | * Exception which occured in {@code thread}. |
80 | * @serial |
81 | */ |
82 | private Throwable throwable; |
83 | |
84 | /** |
85 | * Getter for property {@code thread}. |
86 | * |
87 | * @return the thread of the event or {@code null}. |
88 | */ |
89 | public Thread getThread() |
90 | { |
91 | return this.thread; |
92 | } |
93 | |
94 | /** |
95 | * Getter for property {@code exception}. |
96 | * |
97 | * @return the exception of the event. |
98 | */ |
99 | public Throwable getException() |
100 | { |
101 | return this.throwable; |
102 | } |
103 | |
104 | /** |
105 | * Gets the root cause of the event's exception by traversing up the chained |
106 | * exception hierarchy. |
107 | * |
108 | * @return the root cause of the event's exception. |
109 | */ |
110 | public final Throwable getRootCause() |
111 | { |
112 | Throwable current = this.getException(); |
113 | Throwable root = current; |
114 | |
115 | while ( ( current = current.getCause() ) != null ) |
116 | { |
117 | root = current; |
118 | } |
119 | |
120 | return root; |
121 | } |
122 | |
123 | /** |
124 | * Creates a string representing the properties of the instance. |
125 | * |
126 | * @return a string representing the properties of the instance. |
127 | */ |
128 | private String internalString() |
129 | { |
130 | return new StringBuffer( 500 ).append( '{' ). |
131 | append( "thread=" ).append( this.thread ). |
132 | append( ", throwable=" ).append( this.throwable ). |
133 | append( '}' ).toString(); |
134 | |
135 | } |
136 | |
137 | //----------------------------------------------------------ExceptionEvent-- |
138 | //--Object------------------------------------------------------------------ |
139 | |
140 | /** |
141 | * Returns a string representation of the object. |
142 | * |
143 | * @return a string representation of the object. |
144 | */ |
145 | public String toString() |
146 | { |
147 | return super.toString() + this.internalString(); |
148 | } |
149 | |
150 | //------------------------------------------------------------------Object-- |
151 | } |