View Javadoc
1   package org.apache.maven.plugin.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.maven.plugin.descriptor.MojoDescriptor;
27  import org.apache.maven.plugin.descriptor.Parameter;
28  import org.codehaus.plexus.component.configurator.ConfigurationListener;
29  
30  /**
31   * A configuration listener to help validate the plugin configuration. For instance, check for required but missing
32   * parameters.
33   *
34   * @author Benjamin Bentmann
35   */
36  class ValidatingConfigurationListener
37      implements ConfigurationListener
38  {
39      private final Object mojo;
40  
41      private final ConfigurationListener delegate;
42  
43      private final Map<String, Parameter> missingParameters;
44  
45      ValidatingConfigurationListener( Object mojo, MojoDescriptor mojoDescriptor, ConfigurationListener delegate )
46      {
47          this.mojo = mojo;
48          this.delegate = delegate;
49          this.missingParameters = new HashMap<>();
50  
51          if ( mojoDescriptor.getParameters() != null )
52          {
53              for ( Parameter param : mojoDescriptor.getParameters() )
54              {
55                  if ( param.isRequired() )
56                  {
57                      missingParameters.put( param.getName(), param );
58                  }
59              }
60          }
61      }
62  
63      public Collection<Parameter> getMissingParameters()
64      {
65          return missingParameters.values();
66      }
67  
68      public void notifyFieldChangeUsingSetter( String fieldName, Object value, Object target )
69      {
70          delegate.notifyFieldChangeUsingSetter( fieldName, value, target );
71  
72          if ( mojo == target )
73          {
74              notify( fieldName, value );
75          }
76      }
77  
78      public void notifyFieldChangeUsingReflection( String fieldName, Object value, Object target )
79      {
80          delegate.notifyFieldChangeUsingReflection( fieldName, value, target );
81  
82          if ( mojo == target )
83          {
84              notify( fieldName, value );
85          }
86      }
87  
88      private void notify( String fieldName, Object value )
89      {
90          if ( value != null )
91          {
92              missingParameters.remove( fieldName );
93          }
94      }
95  }