1 package org.apache.maven.usability.diagnostics; 2 3 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 ; 29 import java.util.List ; 30 31 public class ErrorDiagnostics 32 extends AbstractLogEnabled 33 implements Contextualizable 34 { 35 public static final String ROLE = ErrorDiagnostics.class.getName(); 36 37 private PlexusContainer container; 38 39 private List errorDiagnosers; 40 41 public void setErrorDiagnosers( List errorDiagnosers ) 42 { 43 this.errorDiagnosers = errorDiagnosers; 44 } 45 46 public String diagnose( Throwable error ) 47 { 48 List diags = errorDiagnosers; 49 50 boolean releaseDiags = false; 51 boolean errorProcessed = false; 52 53 String 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 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 error ) 122 { 123 return true; 124 } 125 126 public String diagnose( Throwable error ) 127 { 128 StringBuffer message = new StringBuffer (); 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 |