001/*
002 *  jDTAUS Core API
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.lang;
022
023import java.util.EventObject;
024
025/**
026 * Event holding an exception.
027 *
028 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
029 * @version $JDTAUS: ExceptionEvent.java 8641 2012-09-27 06:45:17Z schulte $
030 *
031 * @see ExceptionEventSource
032 */
033public class ExceptionEvent extends EventObject
034{
035    //--Constants---------------------------------------------------------------
036
037    /** Serial version UID for backwards compatibility with 1.4.x classes. */
038    private static final long serialVersionUID = 5909424199091260187L;
039
040    //---------------------------------------------------------------Constants--
041    //--Constructors------------------------------------------------------------
042
043    /**
044     * Creates a new {@code ExceptionEvent} instance taking an exception and
045     * a corresponding thread.
046     *
047     * @param source the source of the new event.
048     * @param thread the thread {@code throwable} occured in.
049     * @param throwable the exception which occured in {@code thread}.
050     *
051     * @throws NullPointerException if either {@code thread} or
052     * {@code throwable} is {@code null}.
053     */
054    public ExceptionEvent( final Object source, final Thread thread,
055                           final Throwable throwable )
056    {
057        super( source );
058
059        if ( thread == null )
060        {
061            throw new NullPointerException( "thread" );
062        }
063        if ( throwable == null )
064        {
065            throw new NullPointerException( "throwable" );
066        }
067
068        this.thread = thread;
069        this.throwable = throwable;
070    }
071
072    //------------------------------------------------------------Constructors--
073    //--ExceptionEvent----------------------------------------------------------
074
075    /** Thread {@code throwable} occured in. */
076    private transient Thread thread;
077
078    /**
079     * Exception which occured in {@code thread}.
080     * @serial
081     */
082    private Throwable throwable;
083
084    /**
085     * Getter for property {@code thread}.
086     *
087     * @return the thread of the event or {@code null}.
088     */
089    public Thread getThread()
090    {
091        return this.thread;
092    }
093
094    /**
095     * Getter for property {@code exception}.
096     *
097     * @return the exception of the event.
098     */
099    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}