KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > usability > diagnostics > DiagnosisUtils


1 package org.apache.maven.usability.diagnostics;
2
3 /*
4 * Copyright 2001-2005 The Apache Software Foundation.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */

18
19 public final class DiagnosisUtils
20 {
21     private DiagnosisUtils()
22     {
23     }
24
25     public static boolean containsInCausality( Throwable JavaDoc error, Class JavaDoc test )
26     {
27         Throwable JavaDoc cause = error;
28
29         while ( cause != null )
30         {
31             if ( test.isInstance( cause ) )
32             {
33                 return true;
34             }
35
36             cause = cause.getCause();
37         }
38
39         return false;
40     }
41
42     public static Throwable JavaDoc getRootCause( Throwable JavaDoc error )
43     {
44         Throwable JavaDoc cause = error;
45
46         while ( true )
47         {
48             Throwable JavaDoc nextCause = cause.getCause();
49
50             if ( nextCause == null )
51             {
52                 break;
53             }
54             else
55             {
56                 cause = nextCause;
57             }
58         }
59
60         return cause;
61     }
62
63     public static Throwable JavaDoc getFromCausality( Throwable JavaDoc error, Class JavaDoc targetClass )
64     {
65         Throwable JavaDoc cause = error;
66
67         while ( cause != null )
68         {
69             if ( targetClass.isInstance( cause ) )
70             {
71                 return cause;
72             }
73
74             cause = cause.getCause();
75         }
76
77         return null;
78     }
79
80     public static void appendRootCauseIfPresentAndUnique( Throwable JavaDoc error, StringBuffer JavaDoc message,
81                                                           boolean includeTypeInfo )
82     {
83         if ( error == null )
84         {
85             return;
86         }
87         
88         Throwable JavaDoc root = getRootCause( error );
89
90         if ( root != null && !root.equals( error ) )
91         {
92             String JavaDoc rootMsg = root.getMessage();
93
94             if ( rootMsg != null && ( error.getMessage() == null || error.getMessage().indexOf( rootMsg ) < 0 ) )
95             {
96                 message.append( "\n" ).append( rootMsg );
97
98                 if ( includeTypeInfo )
99                 {
100                     message.append( "\nRoot error type: " ).append( root.getClass().getName() );
101                 }
102             }
103         }
104     }
105 }
106
Popular Tags