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.plugins.toolchain;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
25  import org.codehaus.plexus.component.configurator.ConfigurationListener;
26  import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
27  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  
31  /**
32   * Custom Plexus ConfigurationConverter to instantiate <code>ToolchainRequirement</code> from configuration.
33   *
34   * @author mkleint
35   * @see ToolchainsRequirement
36   */
37  public class ToolchainConverter extends AbstractConfigurationConverter {
38  
39      /**
40       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#canConvert(java.lang.Class)
41       */
42      @Override
43      public boolean canConvert(Class type) {
44          return ToolchainsRequirement.class.isAssignableFrom(type);
45      }
46  
47      /**
48       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#fromConfiguration(org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup, org.codehaus.plexus.configuration.PlexusConfiguration, java.lang.Class, java.lang.Class, java.lang.ClassLoader, org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator, org.codehaus.plexus.component.configurator.ConfigurationListener)
49       */
50      @Override
51      public Object fromConfiguration(
52              ConverterLookup converterLookup,
53              PlexusConfiguration configuration,
54              Class type,
55              Class baseType,
56              ClassLoader classLoader,
57              ExpressionEvaluator expressionEvaluator,
58              ConfigurationListener listener)
59              throws ComponentConfigurationException {
60          ToolchainsRequirement retValue = new ToolchainsRequirement();
61  
62          processConfiguration(retValue, configuration, expressionEvaluator);
63  
64          return retValue;
65      }
66  
67      private void processConfiguration(
68              ToolchainsRequirement requirement,
69              PlexusConfiguration configuration,
70              ExpressionEvaluator expressionEvaluator)
71              throws ComponentConfigurationException {
72          Map<String, Map<String, String>> map = new HashMap<>();
73  
74          PlexusConfiguration[] tools = configuration.getChildren();
75          for (PlexusConfiguration tool : tools) {
76              String type = tool.getName();
77              PlexusConfiguration[] params = tool.getChildren();
78  
79              Map<String, String> parameters = new HashMap<>();
80              for (PlexusConfiguration param : params) {
81                  parameters.put(param.getName(), param.getValue());
82              }
83              map.put(type, parameters);
84          }
85  
86          requirement.toolchains = map;
87      }
88  }