KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > XMPPException


1 /**
2  * $RCSfile$
3  * $Revision: 2408 $
4  * $Date: 2004-11-02 20:53:30 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack;
22
23 import org.jivesoftware.smack.packet.XMPPError;
24
25 import java.io.PrintStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * A generic exception that is thrown when an error occurs performing an
30  * XMPP operation. XMPP servers can respond to error conditions with an error code
31  * and textual description of the problem, which are encapsulated in the XMPPError
32  * class. When appropriate, an XMPPError instance is attached instances of this exception.
33  *
34  * @see XMPPError
35  * @author Matt Tucker
36  */

37 public class XMPPException extends Exception JavaDoc {
38
39     private XMPPError error = null;
40     private Throwable JavaDoc wrappedThrowable = null;
41
42     /**
43      * Creates a new XMPPException.
44      */

45     public XMPPException() {
46         super();
47     }
48
49     /**
50      * Creates a new XMPPException with a description of the exception.
51      *
52      * @param message description of the exception.
53      */

54     public XMPPException(String JavaDoc message) {
55         super(message);
56     }
57
58     /**
59      * Creates a new XMPPException with the Throwable that was the root cause of the
60      * exception.
61      *
62      * @param wrappedThrowable the root cause of the exception.
63      */

64     public XMPPException(Throwable JavaDoc wrappedThrowable) {
65         super();
66         this.wrappedThrowable = wrappedThrowable;
67     }
68
69     /**
70      * Cretaes a new XMPPException with the XMPPError that was the root case of the
71      * exception.
72      *
73      * @param error the root cause of the exception.
74      */

75     public XMPPException(XMPPError error) {
76         super();
77         this.error = error;
78     }
79
80     /**
81      * Creates a new XMPPException with a description of the exception and the
82      * Throwable that was the root cause of the exception.
83      *
84      * @param message a description of the exception.
85      * @param wrappedThrowable the root cause of the exception.
86      */

87     public XMPPException(String JavaDoc message, Throwable JavaDoc wrappedThrowable) {
88         super(message);
89         this.wrappedThrowable = wrappedThrowable;
90     }
91
92     /**
93      * Creates a new XMPPException with a description of the exception, an XMPPError,
94      * and the Throwable that was the root cause of the exception.
95      *
96      * @param message a description of the exception.
97      * @param error the root cause of the exception.
98      * @param wrappedThrowable the root cause of the exception.
99      */

100     public XMPPException(String JavaDoc message, XMPPError error, Throwable JavaDoc wrappedThrowable) {
101         super(message);
102         this.error = error;
103         this.wrappedThrowable = wrappedThrowable;
104     }
105
106     /**
107      * Creates a new XMPPException with a description of the exception and the
108      * XMPPException that was the root cause of the exception.
109      *
110      * @param message a description of the exception.
111      * @param error the root cause of the exception.
112      */

113     public XMPPException(String JavaDoc message, XMPPError error) {
114         super(message);
115         this.error = error;
116     }
117
118     /**
119      * Returns the XMPPError asscociated with this exception, or <tt>null</tt> if there
120      * isn't one.
121      *
122      * @return the XMPPError asscociated with this exception.
123      */

124     public XMPPError getXMPPError() {
125         return error;
126     }
127
128     /**
129      * Returns the Throwable asscociated with this exception, or <tt>null</tt> if there
130      * isn't one.
131      *
132      * @return the Throwable asscociated with this exception.
133      */

134     public Throwable JavaDoc getWrappedThrowable() {
135         return wrappedThrowable;
136     }
137
138     public void printStackTrace() {
139         printStackTrace(System.err);
140     }
141
142     public void printStackTrace(PrintStream JavaDoc out) {
143         super.printStackTrace(out);
144         if (wrappedThrowable != null) {
145             out.println("Nested Exception: ");
146             wrappedThrowable.printStackTrace(out);
147         }
148     }
149
150     public void printStackTrace(PrintWriter JavaDoc out) {
151         super.printStackTrace(out);
152         if (wrappedThrowable != null) {
153             out.println("Nested Exception: ");
154             wrappedThrowable.printStackTrace(out);
155         }
156     }
157
158     public String JavaDoc getMessage() {
159         String JavaDoc msg = super.getMessage();
160         // If the message was not set, but there is an XMPPError, return the
161
// XMPPError as the message.
162
if (msg == null && error != null) {
163             return error.toString();
164         }
165         return msg;
166     }
167
168     public String JavaDoc toString() {
169         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
170         String JavaDoc message = super.getMessage();
171         if (message != null) {
172             buf.append(message).append(": ");
173         }
174         if (error != null) {
175             buf.append(error);
176         }
177         if (wrappedThrowable != null) {
178             buf.append("\n -- caused by: ").append(wrappedThrowable);
179         }
180
181         return buf.toString();
182     }
183 }
Popular Tags