KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.codehaus.plexus.PlexusConstants;
20 import org.codehaus.plexus.PlexusContainer;
21 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
22 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
23 import org.codehaus.plexus.context.Context;
24 import org.codehaus.plexus.context.ContextException;
25 import org.codehaus.plexus.logging.AbstractLogEnabled;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 public class ErrorDiagnostics
32     extends AbstractLogEnabled
33     implements Contextualizable
34 {
35     public static final String JavaDoc ROLE = ErrorDiagnostics.class.getName();
36
37     private PlexusContainer container;
38
39     private List JavaDoc errorDiagnosers;
40
41     public void setErrorDiagnosers( List JavaDoc errorDiagnosers )
42     {
43         this.errorDiagnosers = errorDiagnosers;
44     }
45
46     public String JavaDoc diagnose( Throwable JavaDoc error )
47     {
48         List JavaDoc diags = errorDiagnosers;
49
50         boolean releaseDiags = false;
51         boolean errorProcessed = false;
52
53         String JavaDoc message = null;
54
55         try
56         {
57             if ( diags == null )
58             {
59                 releaseDiags = true;
60
61                 try
62                 {
63                     diags = container.lookupList( ErrorDiagnoser.ROLE );
64                 }
65                 catch ( ComponentLookupException e )
66                 {
67                     getLogger().error( "Failed to lookup the list of error diagnosers.", e );
68                 }
69             }
70
71             if ( diags != null )
72             {
73                 for ( Iterator JavaDoc it = diags.iterator(); it.hasNext(); )
74                 {
75                     ErrorDiagnoser diagnoser = (ErrorDiagnoser) it.next();
76
77                     if ( diagnoser.canDiagnose( error ) )
78                     {
79                         errorProcessed = true;
80
81                         message = diagnoser.diagnose( error );
82
83                         break;
84                     }
85                 }
86             }
87         }
88         finally
89         {
90             if ( releaseDiags && diags != null )
91             {
92                 try
93                 {
94                     container.releaseAll( diags );
95                 }
96                 catch ( ComponentLifecycleException e )
97                 {
98                     getLogger().debug( "Failed to release error diagnoser list.", e );
99                 }
100             }
101
102             if ( !errorProcessed )
103             {
104                 message = new PuntErrorDiagnoser().diagnose( error );
105             }
106         }
107
108         return message;
109     }
110
111     public void contextualize( Context context )
112         throws ContextException
113     {
114         this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
115     }
116
117     private static class PuntErrorDiagnoser
118         implements ErrorDiagnoser
119     {
120
121         public boolean canDiagnose( Throwable JavaDoc error )
122         {
123             return true;
124         }
125
126         public String JavaDoc diagnose( Throwable JavaDoc error )
127         {
128             StringBuffer JavaDoc message = new StringBuffer JavaDoc();
129
130             message.append( error.getMessage() );
131
132             DiagnosisUtils.appendRootCauseIfPresentAndUnique( error, message, false );
133
134             return message.toString();
135         }
136
137     }
138 }
139
Popular Tags