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