001    package echopoint.util.throwable;
002    
003    /*
004     * This file is part of the Echo Point Project.  This project is a collection
005     * of Components that have extended the Echo Web Application Framework.
006     *
007     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
008     *
009     * The contents of this file are subject to the Mozilla Public License Version
010     * 1.1 (the "License"); you may not use this file except in compliance with
011     * the License. You may obtain a copy of the License at
012     * http://www.mozilla.org/MPL/
013     *
014     * Software distributed under the License is distributed on an "AS IS" basis,
015     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
016     * for the specific language governing rights and limitations under the
017     * License.
018     *
019     * Alternatively, the contents of this file may be used under the terms of
020     * either the GNU General Public License Version 2 or later (the "GPL"), or
021     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022     * in which case the provisions of the GPL or the LGPL are applicable instead
023     * of those above. If you wish to allow use of your version of this file only
024     * under the terms of either the GPL or the LGPL, and not to allow others to
025     * use your version of this file under the terms of the MPL, indicate your
026     * decision by deleting the provisions above and replace them with the notice
027     * and other provisions required by the GPL or the LGPL. If you do not delete
028     * the provisions above, a recipient may use your version of this file under
029     * the terms of any one of the MPL, the GPL or the LGPL.
030     */
031    
032    /**
033     * <code>ThrowableDescriptor</code> describes an Throwable
034     * and lists if properties, causes and its stack trace.
035     *
036     */
037    public class ThrowableDescriptor extends ThrowablePropertyDescriptor {
038    
039            private Throwable                               throwable;
040            private String                                  message;
041            private ThrowableDescriptor[]   causes;
042            private ThrowablePropertyDescriptor[] properties;
043            private String[]                                stackTrace;
044    
045            ThrowableDescriptor(Throwable throwable) {
046                    setThrowable(throwable);
047                    setMessage(throwable.getLocalizedMessage());
048                    setType(throwable.getClass());
049                    setValue(this.message);
050                    setName(throwable.getClass().getName());
051    
052                    setCauses(new ThrowableDescriptor[0]);
053                    setStackTrace(new String[0]);
054                    setProperties(new ThrowablePropertyDescriptor[0]);
055                    setModifiers(throwable.getClass().getModifiers());
056            }
057    
058    
059            /**
060             * Returns the causes of the Throwable or a 0 length
061             * array if they are not known.
062             *
063             * @return the causes of the Throwable or a 0 length
064             * array if they are not known.
065             */
066            public ThrowableDescriptor[] getCauses() {
067                    return causes;
068            }
069    
070            /**
071             * Returns the localised message of the Throwable
072             * @return the localised message of the Throwable
073             */
074            public String getMessage() {
075                    return message;
076            }
077    
078            /**
079             * Returns an array of stack tace messages for the Throwable.
080             * @return an array of stack tace messages for the Throwable.
081             */
082            public String[] getStackTrace() {
083                    return stackTrace;
084            }
085    
086            /**
087             * Returns the actual Throwable that this ThrowableDescriptor describes.
088             * @return the actual Throwable that this ThrowableDescriptor describes.
089             */
090            public Throwable getThrowable() {
091                    return throwable;
092            }
093    
094            /**
095             * Returns an array of properties of the Throwable.
096             * @return an array of properties of the Throwable.
097             */
098            public ThrowablePropertyDescriptor[] getProperties() {
099                    return properties;
100            }
101    
102    
103            /**
104             * @see java.lang.Object#toString()
105             */
106            public String toString() {
107                    StringBuffer sb = new  StringBuffer();
108                    sb.append(getType().getName());
109                    sb.append(':');
110                    sb.append(message);
111                    for (int i = 0; i < properties.length; i++) {
112                            sb.append(' ');
113                            sb.append(properties[i].getName());
114                            sb.append(':');
115                            sb.append(properties[i].getValueAsString());
116                    }
117                    for (int i = 0; i < causes.length; i++) {
118                            sb.append(' ');
119                            sb.append(causes[i].getName());
120                            sb.append(':');
121                            sb.append(causes[i].getValueAsString());
122                    }
123                    return sb.toString();
124            }
125    
126            void setCauses(ThrowableDescriptor[] descriptors) {
127                    causes = descriptors;
128            }
129    
130            void setMessage(String string) {
131                    message = string;
132            }
133    
134            void setProperties(ThrowablePropertyDescriptor[] descriptors) {
135                    properties = descriptors;
136            }
137    
138            public void setStackTrace(String[] strings) {
139                    stackTrace = strings;
140            }
141    
142            void setThrowable(Throwable throwable) {
143                    this.throwable = throwable;
144            }
145    
146    }
147