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 java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.maven.plugin.descriptor.MojoDescriptor;
26  import org.apache.maven.plugin.descriptor.Parameter;
27  import org.codehaus.plexus.component.configurator.ConfigurationListener;
28  
29  /**
30   * A configuration listener to help validate the plugin configuration. For instance, check for required but missing
31   * parameters.
32   *
33   * @author Benjamin Bentmann
34   */
35  class ValidatingConfigurationListener implements ConfigurationListener {
36      private final Object mojo;
37  
38      private final ConfigurationListener delegate;
39  
40      private final Map<String, Parameter> missingParameters;
41  
42      ValidatingConfigurationListener(Object mojo, MojoDescriptor mojoDescriptor, ConfigurationListener delegate) {
43          this.mojo = mojo;
44          this.delegate = delegate;
45          this.missingParameters = new HashMap<>();
46  
47          if (mojoDescriptor.getParameters() != null) {
48              for (Parameter param : mojoDescriptor.getParameters()) {
49                  if (param.isRequired()) {
50                      missingParameters.put(param.getName(), param);
51                  }
52              }
53          }
54      }
55  
56      public Collection<Parameter> getMissingParameters() {
57          return missingParameters.values();
58      }
59  
60      public void notifyFieldChangeUsingSetter(String fieldName, Object value, Object target) {
61          delegate.notifyFieldChangeUsingSetter(fieldName, value, target);
62  
63          if (mojo == target) {
64              notify(fieldName, value);
65          }
66      }
67  
68      public void notifyFieldChangeUsingReflection(String fieldName, Object value, Object target) {
69          delegate.notifyFieldChangeUsingReflection(fieldName, value, target);
70  
71          if (mojo == target) {
72              notify(fieldName, value);
73          }
74      }
75  
76      private void notify(String fieldName, Object value) {
77          if (value != null) {
78              missingParameters.remove(fieldName);
79          }
80      }
81  }