KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > ApplicationRuntimeException


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// 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
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind;
16
17 /**
18  * General wrapper for any exception (normal or runtime) that may occur during runtime processing
19  * for the application. This is exception is used when the intent is to communicate a low-level
20  * failure to the user or developer; it is not expected to be caught. The
21  * {@link #getRootCause() rootCause} property is a <em>nested</em> exception.
22  *
23  * @author Howard Lewis Ship
24  */

25
26 public class ApplicationRuntimeException extends RuntimeException JavaDoc implements Locatable
27 {
28     private static final long serialVersionUID = 1L;
29
30     private Throwable JavaDoc _rootCause;
31
32     private transient Location _location;
33
34     private transient Object JavaDoc _component;
35
36     public ApplicationRuntimeException(Throwable JavaDoc rootCause)
37     {
38         this(rootCause.getMessage(), rootCause);
39     }
40
41     public ApplicationRuntimeException(String JavaDoc message)
42     {
43         this(message, null, null, null);
44     }
45
46     public ApplicationRuntimeException(String JavaDoc message, Throwable JavaDoc rootCause)
47     {
48         this(message, null, null, rootCause);
49     }
50
51     public ApplicationRuntimeException(String JavaDoc message, Object JavaDoc component, Location location,
52             Throwable JavaDoc rootCause)
53     {
54         super(message);
55
56         _rootCause = rootCause;
57         _component = component;
58
59         _location = HiveMind.findLocation(new Object JavaDoc[]
60         { location, rootCause, component });
61     }
62
63     public ApplicationRuntimeException(String JavaDoc message, Location location, Throwable JavaDoc rootCause)
64     {
65         this(message, null, location, rootCause);
66     }
67
68     public Throwable JavaDoc getRootCause()
69     {
70         return _rootCause;
71     }
72
73     public Location getLocation()
74     {
75         return _location;
76     }
77
78     public Object JavaDoc getComponent()
79     {
80         return _component;
81     }
82
83     /**
84      * This method is for compatibility with JDK 1.4. Under 1.4, this will look like an override,
85      * allowing <code>printStackTrace()</code> to descending into the root cause exception and
86      * print its stack trace too.
87      */

88     public Throwable JavaDoc getCause()
89     {
90         return _rootCause;
91     }
92
93     /**
94      * Overrides the default implementation of <code>toString</code>, suffixing the normal result
95      * with the {@link #getLocation() location} of the exception (if non null). Example:
96      * <code>org.apache.hivemind.ApplicationRuntimeException: Exception Message [file:foo/bar/baz, line 13]</code>.
97      *
98      * @since 1.1
99      */

100     public String JavaDoc toString()
101     {
102         if (_location == null)
103             return super.toString();
104
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(super.toString());
106         buffer.append(" [");
107         buffer.append(_location);
108         buffer.append("]");
109
110         return buffer.toString();
111     }
112 }
Popular Tags