View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.internal;
20  
21  import org.apache.maven.plugin.descriptor.MojoDescriptor;
22  import org.apache.maven.plugin.descriptor.Parameter;
23  import org.apache.maven.shared.utils.logging.MessageBuilder;
24  import org.apache.maven.shared.utils.logging.MessageUtils;
25  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
26  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
27  import org.codehaus.plexus.configuration.PlexusConfiguration;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Common implementations for plugin parameters configuration validation.
33   *
34   * @author Slawomir Jaranowski
35   */
36  abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator {
37  
38      protected final Logger logger = LoggerFactory.getLogger(getClass());
39  
40      protected boolean isValueSet(PlexusConfiguration config, ExpressionEvaluator expressionEvaluator) {
41          if (config == null) {
42              return false;
43          }
44  
45          // there are sub items ... so configuration is declared
46          if (config.getChildCount() > 0) {
47              return true;
48          }
49  
50          String strValue = config.getValue();
51  
52          if (strValue == null || strValue.isEmpty()) {
53              return false;
54          }
55  
56          if (isIgnoredProperty(strValue)) {
57              return false;
58          }
59  
60          // for declaration like @Parameter( property = "config.property" )
61          // the value will contain ${config.property}
62  
63          try {
64              return expressionEvaluator.evaluate(strValue) != null;
65          } catch (ExpressionEvaluationException e) {
66              // not important
67              // will be reported during Mojo fields populate
68          }
69  
70          // fallback - in case of error in expressionEvaluator
71          return false;
72      }
73  
74      @Override
75      public final void validate(
76              MojoDescriptor mojoDescriptor,
77              PlexusConfiguration pomConfiguration,
78              ExpressionEvaluator expressionEvaluator) {
79          if (!logger.isWarnEnabled()) {
80              return;
81          }
82  
83          doValidate(mojoDescriptor, pomConfiguration, expressionEvaluator);
84      }
85  
86      protected abstract void doValidate(
87              MojoDescriptor mojoDescriptor,
88              PlexusConfiguration pomConfiguration,
89              ExpressionEvaluator expressionEvaluator);
90  
91      protected boolean isIgnoredProperty(String strValue) {
92          return false;
93      }
94  
95      protected abstract String getParameterLogReason(Parameter parameter);
96  
97      protected void logParameter(Parameter parameter) {
98          MessageBuilder messageBuilder = MessageUtils.buffer()
99                  .warning("Parameter '")
100                 .warning(parameter.getName())
101                 .warning('\'');
102 
103         if (parameter.getExpression() != null) {
104             String userProperty = parameter.getExpression().replace("${", "'").replace('}', '\'');
105             messageBuilder.warning(" (user property ").warning(userProperty).warning(")");
106         }
107 
108         messageBuilder.warning(" ").warning(getParameterLogReason(parameter));
109 
110         logger.warn(messageBuilder.toString());
111     }
112 }