KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > ChannelException


1 // $Id: ChannelException.java,v 1.4 2004/08/04 14:26:34 belaban Exp $
2

3 package org.jgroups;
4
5 import java.io.PrintStream JavaDoc;
6 import java.io.PrintWriter JavaDoc;
7
8 import java.util.StringTokenizer JavaDoc;
9
10 /**
11  * This class represents the super class for all exception types thrown by
12  * JGroups.
13  */

14 public class ChannelException extends Exception JavaDoc {
15     // Class-level implementation.
16
private static final boolean IS_JAVA_13;
17
18     static {
19         // Check to see if we are in a 1.3 VM. If so, we need to change how
20
// we print stack traces.
21
String JavaDoc javaSpecVersion=
22                 System.getProperty("java.specification.version");
23
24         StringTokenizer JavaDoc tokenizer=new StringTokenizer JavaDoc(javaSpecVersion, ".");
25
26         int majorVersion=Integer.parseInt(tokenizer.nextToken());
27         int minorVersion=Integer.parseInt(tokenizer.nextToken());
28
29         if(majorVersion == 1 && minorVersion == 3) {
30             IS_JAVA_13=true;
31         }
32         else {
33             IS_JAVA_13=false;
34         }
35     }
36
37     // Instance-level implementation.
38
private Throwable JavaDoc _cause;
39
40     public ChannelException() {
41         super();
42     }
43
44     public ChannelException(String JavaDoc reason) {
45         super(reason);
46     }
47
48     public ChannelException(String JavaDoc reason, Throwable JavaDoc cause) {
49         super(reason);
50         _cause = cause;
51     }
52
53     public String JavaDoc toString() {
54         return "ChannelException: " + getMessage();
55     }
56
57     /**
58      * Retrieves the cause of this exception as passed to the constructor.
59      * <p>
60      * This method is provided so that in the case that a 1.3 VM is used,
61      * 1.4-like exception chaining functionality is possible. If a 1.4 VM is
62      * used, this method will override <code>Throwable.getCause()</code> with a
63      * version that does exactly the same thing.
64      *
65      * @return the cause of this exception.
66      */

67     public Throwable JavaDoc getCause() {
68         return _cause;
69     }
70
71     /*
72      * Throwable implementation.
73      */

74
75     /**
76      * Prints this exception's stack trace to standard error.
77      * <p>
78      * This method is provided so that in the case that a 1.3 VM is used, calls
79      * to <code>printStackTrace</code> can be intercepted so that 1.4-like
80      * exception chaining functionality is possible.
81      */

82     public void printStackTrace() {
83         printStackTrace(System.err);
84     }
85
86     /**
87      * Prints this exception's stack trace to the provided stream.
88      * <p>
89      * This method implements the 1.4-like exception chaining functionality when
90      * printing stack traces for 1.3 VMs. If a 1.4 VM is used, this call is
91      * delegated only to the super class.
92      *
93      * @param ps the stream to which the stack trace will be "printed".
94      */

95     public void printStackTrace(PrintStream JavaDoc ps) {
96         synchronized (ps) {
97             super.printStackTrace(ps);
98
99             if (IS_JAVA_13) {
100                 printCauseStackTrace(ps);
101             }
102         }
103     }
104
105     /**
106      * Prints this exception's stack trace to the provided writer.
107      * <p>
108      * This method implements the 1.4-like exception chaining functionality when
109      * printing stack traces for 1.3 VMs. If a 1.4 VM is used, this call is
110      * delegated only to the super class.
111      *
112      * @param pw the writer to which the stack trace will be "printed".
113      */

114     public void printStackTrace(PrintWriter JavaDoc pw) {
115         synchronized (pw) {
116             super.printStackTrace(pw);
117
118             if (IS_JAVA_13) {
119                 printCauseStackTrace(pw);
120             }
121         }
122     }
123
124     private void printCauseStackTrace(PrintStream JavaDoc ps) {
125         if (_cause != null) {
126             ps.print("Caused by: ");
127             _cause.printStackTrace(ps);
128         }
129     }
130
131     private void printCauseStackTrace(PrintWriter JavaDoc pw) {
132         if (_cause != null) {
133             pw.print("Caused by: ");
134             _cause.printStackTrace(pw);
135         }
136     }
137 }
138
Popular Tags