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