KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > KilimException


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.kilim;
19
20     import java.io.PrintStream JavaDoc;
21     import java.io.PrintWriter JavaDoc;
22
23 /**
24  * KilimException is the exception generated inthe Kilim environment to notify the user error
25  * (bugs in Kilim implementation are notified through Internal exceptions). KilimException provides a simple
26  * mechanism to wrap exceptions of an other type so that the stack trace and
27  * the message returned by such an exception correspond to that of the wrapped exception.
28  * @author horn
29  */

30 public class KilimException extends Exception JavaDoc {
31    private Exception JavaDoc actual;
32
33    /**
34     * Constructs a new KilimException with no detail message.
35     */

36    public KilimException() {
37       super();
38       actual = null;
39    }
40    /**
41     * Builds a KilimException with a detail message.
42     * @param s : a string to be used as a message
43     */

44    public KilimException(String JavaDoc s) {
45       super(s);
46       actual = null;
47    }
48
49    /**
50     * Builds a KilimException that wraps another exception.
51     * @param exception : an exception to be wrapped.
52     */

53    public KilimException(Exception JavaDoc exception) {
54       actual = (exception instanceof KilimException) ? ((KilimException) exception).represents() : exception;
55    }
56    /**
57     * Returns the detail message of this KilimException.
58     * <p>
59     * If this exception represents another exception, the returned message is
60     * that of the represented exception.
61     *
62     * @return the detail message of this 'KilimException',
63     * or 'null' if this 'KilimException' does not
64     * have a detail message.
65     */

66    public String JavaDoc getMessage() {
67       return (actual != null) ? actual.getMessage() : super.getMessage();
68    }
69
70    /**
71     * Returns a short description of this KilimException.
72     * <p>
73     * If this exception represents another exception, the returned description
74     * is that of the represented exception.
75     *
76     * @return a string representation of this 'KilimException'.
77     */

78    public String JavaDoc toString() {
79       return (actual != null) ? actual.toString() : super.toString();
80    }
81
82    /**
83     * Prints this KilimException and its backtrace to the
84     * standard error stream.
85     * <p>
86     * If this exception represents another exception, the printed description
87     * and backtrace are that of the represented exception.
88     */

89    public void printStackTrace() {
90       if (actual != null) {
91         actual.printStackTrace();
92       } else {
93         super.printStackTrace();
94       }
95    }
96
97    /**
98     * Prints this KilimException and its backtrace to the
99     * specified print stream.
100     * <p>
101     * If this exception represents another exception, the printed description
102     * and backtrace are that of the represented exception.
103     *
104     * @param s the print stream.
105     */

106    public void printStackTrace(PrintStream JavaDoc s) {
107       if (actual != null) {
108         actual.printStackTrace(s);
109       } else {
110         super.printStackTrace(s);
111       }
112    }
113    
114    /**
115     * Prints this 'Throwable' and its backtrace to the specified
116     * print writer.
117     * <p>
118     * If this exception represents another exception, the printed description
119     * and backtrace are that of the represented exception.
120     *
121     * @param s the print writer.
122     */

123    public void printStackTrace(PrintWriter JavaDoc s) {
124       if (actual != null) {
125         actual.printStackTrace(s);
126       } else {
127         super.printStackTrace(s);
128       }
129    }
130    /**
131     * Returns the exception wrapped by this KilimException.
132     * <p>
133     * If this exception doesn't wrap any other exception, it returns itself.
134     *
135     * @return the represented exception.
136     */

137    public Exception JavaDoc represents() {
138       return (actual != null) ? actual : this;
139    }
140
141     private Exception JavaDoc encapsulated;
142 }
143
Popular Tags