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.execution.MavenSession;
22  import org.apache.maven.plugin.PluginValidationManager;
23  import org.apache.maven.plugin.descriptor.MojoDescriptor;
24  import org.apache.maven.plugin.descriptor.Parameter;
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  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * Common implementations for plugin parameters configuration validation.
33   *
34   */
35  abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator {
36  
37      protected final PluginValidationManager pluginValidationManager;
38  
39      protected AbstractMavenPluginParametersValidator(PluginValidationManager pluginValidationManager) {
40          this.pluginValidationManager = requireNonNull(pluginValidationManager);
41      }
42  
43      protected boolean isValueSet(PlexusConfiguration config, ExpressionEvaluator expressionEvaluator) {
44          if (config == null) {
45              return false;
46          }
47  
48          // there are sub items ... so configuration is declared
49          if (config.getChildCount() > 0) {
50              return true;
51          }
52  
53          String strValue = config.getValue();
54  
55          if (strValue == null || strValue.isEmpty()) {
56              return false;
57          }
58  
59          if (isIgnoredProperty(strValue)) {
60              return false;
61          }
62  
63          // for declaration like @Parameter( property = "config.property" )
64          // the value will contain ${config.property}
65  
66          try {
67              return expressionEvaluator.evaluate(strValue) != null;
68          } catch (ExpressionEvaluationException e) {
69              // not important
70              // will be reported during Mojo fields populate
71          }
72  
73          // fallback - in case of error in expressionEvaluator
74          return false;
75      }
76  
77      @Override
78      public final void validate(
79              MavenSession mavenSession,
80              MojoDescriptor mojoDescriptor,
81              Class<?> mojoClass,
82              PlexusConfiguration pomConfiguration,
83              ExpressionEvaluator expressionEvaluator) {
84          doValidate(mavenSession, mojoDescriptor, mojoClass, pomConfiguration, expressionEvaluator);
85      }
86  
87      protected abstract void doValidate(
88              MavenSession mavenSession,
89              MojoDescriptor mojoDescriptor,
90              Class<?> mojoClass,
91              PlexusConfiguration pomConfiguration,
92              ExpressionEvaluator expressionEvaluator);
93  
94      protected boolean isIgnoredProperty(String strValue) {
95          return false;
96      }
97  
98      protected abstract String getParameterLogReason(Parameter parameter);
99  
100     protected String formatParameter(Parameter parameter) {
101         StringBuilder stringBuilder = new StringBuilder()
102                 .append("Parameter '")
103                 .append(parameter.getName())
104                 .append('\'');
105 
106         if (parameter.getExpression() != null) {
107             String userProperty = parameter.getExpression().replace("${", "'").replace('}', '\'');
108             stringBuilder.append(" (user property ").append(userProperty).append(")");
109         }
110 
111         stringBuilder.append(" ").append(getParameterLogReason(parameter));
112 
113         return stringBuilder.toString();
114     }
115 }