1 package org.apache.maven.plugin; 2 3 18 19 import org.apache.maven.plugin.descriptor.PluginDescriptor; 20 import org.apache.maven.usability.plugin.Expression; 21 import org.apache.maven.usability.plugin.ExpressionDocumentationException; 22 import org.apache.maven.usability.plugin.ExpressionDocumenter; 23 import org.codehaus.plexus.component.configurator.ComponentConfigurationException; 24 import org.codehaus.plexus.configuration.PlexusConfiguration; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 import java.util.StringTokenizer ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 41 public class PluginConfigurationException 42 extends Exception 43 { 44 private final PluginDescriptor pluginDescriptor; 45 46 private String originalMessage; 47 48 private static final List UNMODIFIABLE_EXPRESSIONS = Arrays.asList( 49 new String []{"localRepository", "reactorProjects", "settings", "project", "session", "plugin", "basedir"} ); 50 51 public PluginConfigurationException( PluginDescriptor pluginDescriptor, String message ) 52 { 53 super( "Error configuring: " + pluginDescriptor.getPluginLookupKey() + ". Reason: " + message ); 54 this.pluginDescriptor = pluginDescriptor; 55 this.originalMessage = message; 56 } 57 58 public PluginConfigurationException( PluginDescriptor pluginDescriptor, Throwable cause ) 59 { 60 super( "Error configuring: " + pluginDescriptor.getPluginLookupKey() + ".", cause ); 61 this.pluginDescriptor = pluginDescriptor; 62 } 63 64 public PluginConfigurationException( PluginDescriptor pluginDescriptor, String message, Throwable cause ) 65 { 66 super( "Error configuring: " + pluginDescriptor.getPluginLookupKey() + ". Reason: " + message, cause ); 67 this.pluginDescriptor = pluginDescriptor; 68 this.originalMessage = message; 69 } 70 71 public PluginDescriptor getPluginDescriptor() 72 { 73 return pluginDescriptor; 74 } 75 76 public String getOriginalMessage() 77 { 78 return originalMessage; 79 } 80 81 protected static void addParameterUsageInfo( String expression, StringBuffer messageBuffer ) 82 { 83 StringBuffer expressionMessageBuffer = new StringBuffer (); 84 85 Matcher exprMatcher = Pattern.compile( "\\$\\{(.+)\\}" ).matcher( expression ); 86 87 boolean unmodifiableElementsFound = false; 88 boolean activeElementsFound = false; 89 90 int elementCount = 0; 91 92 while ( exprMatcher.find() ) 93 { 94 elementCount++; 95 96 activeElementsFound = true; 97 98 String subExpression = exprMatcher.group( 1 ); 99 100 StringTokenizer expressionParts = new StringTokenizer ( subExpression, "." ); 101 102 String firstPart = expressionParts.nextToken(); 103 104 Map expressions = null; 105 try 106 { 107 expressions = ExpressionDocumenter.load(); 108 } 109 catch ( ExpressionDocumentationException e ) 110 { 111 expressionMessageBuffer.append( "\n\nERROR!! Failed to load expression documentation!" ); 112 113 StringWriter sWriter = new StringWriter (); 114 PrintWriter pWriter = new PrintWriter ( sWriter ); 115 116 e.printStackTrace( pWriter ); 117 118 expressionMessageBuffer.append( "\n\nException:\n\n" ).append( sWriter.toString() ); 119 } 120 121 if ( expressions != null ) 122 { 123 Expression expr = (Expression) expressions.get( subExpression ); 124 125 if ( expr != null ) 126 { 127 if ( !expr.isEditable() ) 128 { 129 unmodifiableElementsFound = true; 130 } 131 else 132 { 133 addParameterConfigDocumentation( firstPart, exprMatcher.group( 0 ), subExpression, 134 expressionMessageBuffer, expressions ); 135 } 136 } 137 else if ( UNMODIFIABLE_EXPRESSIONS.contains( subExpression ) ) 138 { 139 unmodifiableElementsFound = true; 140 } 141 else 142 { 143 expressionMessageBuffer.append( "on the command line, specify: \'-D" ).append( subExpression ) 144 .append( "=VALUE\'" ); 145 } 146 } 147 } 148 149 if ( activeElementsFound ) 150 { 151 messageBuffer.append( expressionMessageBuffer ); 152 } 153 else 154 { 155 messageBuffer.append( 156 " (found static expression: \'" + expression + "\' which may act as a default value).\n" ); 157 } 158 159 if ( unmodifiableElementsFound ) 160 { 161 if ( elementCount > 1 ) 162 { 163 messageBuffer.append( " " ); 164 } 165 166 messageBuffer 167 .append( "NOTE: One or more purely derived expression elements were detected in \'" + expression + 168 "\'.\n If you continue to get this error after any other expression elements are specified correctly," + 169 "\n please report this issue to the Maven development team.\n" ); 170 } 171 } 172 173 private static void addParameterConfigDocumentation( String firstPart, String wholeExpression, String subExpression, 174 StringBuffer expressionMessageBuffer, Map expressionDoco ) 175 { 176 Expression expr = (Expression) expressionDoco.get( subExpression ); 177 178 if ( expr != null ) 179 { 180 expressionMessageBuffer.append( "check that the following section of " ); 181 if ( "project".equals( firstPart ) ) 182 { 183 expressionMessageBuffer.append( "the pom.xml " ); 184 } 185 else if ( "settings".equals( firstPart ) ) 186 { 187 expressionMessageBuffer.append( "your ~/.m2/settings.xml file " ); 188 } 189 190 expressionMessageBuffer.append( "is present and correct:\n\n" ); 191 192 String message = expr.getConfiguration(); 193 194 if ( message == null ) 195 { 196 message = expr.getDescription(); 197 } 198 199 expressionMessageBuffer.append( message ); 200 201 Properties cliConfig = expr.getCliOptions(); 202 203 if ( cliConfig != null && !cliConfig.isEmpty() ) 204 { 205 expressionMessageBuffer.append( "\n\n-OR-\n\nUse the following command-line switches:\n" ); 206 207 prettyPrintCommandLineSwitches( cliConfig, '.', expressionMessageBuffer ); 208 } 209 } 210 else 211 { 212 expressionMessageBuffer.append( "ensure that the expression: \'" + wholeExpression + "\' is satisfied" ); 213 } 214 } 215 216 private static void prettyPrintCommandLineSwitches( Properties switches, char filler, 217 StringBuffer expressionMessageBuffer ) 218 { 219 int maxKeyLen = 0; 220 221 for ( Iterator it = switches.entrySet().iterator(); it.hasNext(); ) 222 { 223 Map.Entry entry = (Map.Entry ) it.next(); 224 225 String key = (String ) entry.getKey(); 226 227 int keyLen = key.length(); 228 if ( keyLen > maxKeyLen ) 229 { 230 maxKeyLen = keyLen; 231 } 232 } 233 234 final int minFillerCount = 4; 235 236 for ( Iterator it = switches.entrySet().iterator(); it.hasNext(); ) 237 { 238 Map.Entry entry = (Map.Entry ) it.next(); 239 240 String key = (String ) entry.getKey(); 241 242 int keyLen = key.length(); 243 244 int fillerCount = maxKeyLen - keyLen + minFillerCount; 245 246 expressionMessageBuffer.append( '\n' ).append( key ).append( ' ' ); 247 248 for ( int i = 0; i < fillerCount; i++ ) 249 { 250 expressionMessageBuffer.append( filler ); 251 } 252 253 expressionMessageBuffer.append( ' ' ).append( entry.getValue() ); 254 } 255 256 expressionMessageBuffer.append( '\n' ); 257 } 258 259 public String buildConfigurationDiagnosticMessage( ComponentConfigurationException cce ) 260 { 261 StringBuffer message = new StringBuffer (); 262 263 PluginDescriptor descriptor = getPluginDescriptor(); 264 265 PlexusConfiguration failedConfiguration = cce.getFailedConfiguration(); 266 267 message.append( "Failed to configure plugin parameters for: " + descriptor.getId() + "\n\n" ); 268 269 if ( failedConfiguration != null ) 270 { 271 String value = failedConfiguration.getValue( null ); 272 if ( value != null ) 273 { 274 addParameterUsageInfo( value, message ); 275 } 276 } 277 278 message.append( "\n\nCause: " ).append( cce.getMessage() ); 279 280 return message.toString(); 281 } 282 } 283 | Popular Tags |