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  
40      private final Object mojo;
41  
42      private final ConfigurationListener delegate;
43  
44      private final Map<String, Parameter> missingParameters;
45  
46      public ValidatingConfigurationListener( Object mojo, MojoDescriptor mojoDescriptor, ConfigurationListener delegate )
47      {
48          this.mojo = mojo;
49          this.delegate = delegate;
50          this.missingParameters = new HashMap<>();
51  
52          if ( mojoDescriptor.getParameters() != null )
53          {
54              for ( Parameter param : mojoDescriptor.getParameters() )
55              {
56                  if ( param.isRequired() )
57                  {
58                      missingParameters.put( param.getName(), param );
59                  }
60              }
61          }
62      }
63  
64      public Collection<Parameter> getMissingParameters()
65      {
66          return missingParameters.values();
67      }
68  
69      public void notifyFieldChangeUsingSetter( String fieldName, Object value, Object target )
70      {
71          delegate.notifyFieldChangeUsingSetter( fieldName, value, target );
72  
73          if ( mojo == target )
74          {
75              notify( fieldName, value );
76          }
77      }
78  
79      public void notifyFieldChangeUsingReflection( String fieldName, Object value, Object target )
80      {
81          delegate.notifyFieldChangeUsingReflection( fieldName, value, target );
82  
83          if ( mojo == target )
84          {
85              notify( fieldName, value );
86          }
87      }
88  
89      private void notify( String fieldName, Object value )
90      {
91          if ( value != null )
92          {
93              missingParameters.remove( fieldName );
94          }
95      }
96  
97  }