Debian Codegraph @

ssl-proxies/src/main/java/org/globus/common/ChainedIOException.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.common;
16
17 /**
18 *
19 */
20 public class ChainedIOException extends java.io.IOException {
21 /**
22 * The possibly null root cause exception.
23 * @serial
24 */
25 private Throwable exception;
26
27 /**
28 * Constructs a new instance of <tt>ChainedIOException</tt>.
29 * The root exception and the detailed message are null.
30 */
31 public ChainedIOException () {
32 super();
33 }
34
35 /**
36 * Constructs a new instance of <tt>ChainedIOException</tt> with a
37 * detailed message. The root exception is null.
38 *
39 * @param detail A possibly null string containing details of the
40 * exception.
41 *
42 * @see java.lang.Throwable#getMessage
43 */
44 public ChainedIOException (String detail) {
45 super(detail);
46 }
47
48 /**
49 * Constructs a new instance of <tt>ChainedIOException</tt> with a
50 * detailed message and a root exception.
51 *
52 * @param detail A possibly null string containing details of the
53 * exception.
54 * @param ex A possibly null root exception that caused this exception.
55 *
56 * @see java.lang.Throwable#getMessage
57 * @see #getException
58 */
59 public ChainedIOException (String detail, Throwable ex) {
60 super(detail, ex);
61 exception = ex;
62 }
63
64 /**
65 * Returns the root exception that caused this exception.
66 * @return The possibly null root exception that caused this exception.
67 */
68 public Throwable getException() {
69 return exception;
70 }
71
72 /**
73 * Prints this exception's stack trace to <tt>System.err</tt>.
74 * If this exception has a root exception; the stack trace of the
75 * root exception is printed to <tt>System.err</tt> instead.
76 */
77 public void printStackTrace() {
78 printStackTrace( System.err );
79 }
80
81 /**
82 * Prints this exception's stack trace to a print stream.
83 * If this exception has a root exception; the stack trace of the
84 * root exception is printed to the print stream instead.
85 * @param ps The non-null print stream to which to print.
86 */
87 public void printStackTrace(java.io.PrintStream ps) {
88 if ( exception != null ) {
89 String superString = getLocalMessage();
90 synchronized ( ps ) {
91 ps.print(superString);
92 ps.print((superString.endsWith(".") ?
93 " Caused by " : ". Caused by "));
94 exception.printStackTrace( ps );
95 }
96 } else {
97 super.printStackTrace( ps );
98 }
99 }
100
101 /**
102 * Prints this exception's stack trace to a print writer.
103 * If this exception has a root exception; the stack trace of the
104 * root exception is printed to the print writer instead.
105 * @param pw The non-null print writer to which to print.
106 */
107 public void printStackTrace(java.io.PrintWriter pw) {
108 if ( exception != null ) {
109 String superString = getLocalMessage();
110 synchronized (pw) {
111 pw.print(superString);
112 pw.print((superString.endsWith(".") ?
113 " Caused by " : ". Caused by "));
114 exception.printStackTrace( pw );
115 }
116 } else {
117 super.printStackTrace( pw );
118 }
119 }
120
121 public String getMessage() {
122 String answer = super.getMessage();
123 if (exception != null && exception != this) {
124 String msg = exception.getMessage();
125 if (msg == null) {
126 msg = exception.getClass().getName();
127 }
128 answer += " [Caused by: " + msg + "]";
129 }
130 return answer;
131 }
132
133 private String getLocalMessage() {
134 String message = super.getMessage();
135 return (message == null) ? getClass().getName() : message;
136 }
137
138 }