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.cli;
20  
21  import java.util.Arrays;
22  
23  import com.google.inject.Binder;
24  import com.google.inject.Module;
25  import com.google.inject.name.Names;
26  import org.apache.maven.api.xml.XmlNode;
27  import org.apache.maven.extension.internal.CoreExtensionEntry;
28  import org.apache.maven.internal.xml.XmlNodeImpl;
29  import org.apache.maven.internal.xml.XmlPlexusConfiguration;
30  import org.apache.maven.model.v4.MavenTransformer;
31  import org.codehaus.plexus.configuration.PlexusConfiguration;
32  import org.codehaus.plexus.interpolation.InterpolationException;
33  import org.codehaus.plexus.interpolation.StringSearchInterpolator;
34  import org.codehaus.plexus.interpolation.ValueSource;
35  
36  public class ExtensionConfigurationModule implements Module {
37  
38      private final CoreExtensionEntry extension;
39      private final Iterable<ValueSource> valueSources;
40  
41      public ExtensionConfigurationModule(CoreExtensionEntry extension, ValueSource... valueSources) {
42          this.extension = extension;
43          this.valueSources = Arrays.asList(valueSources);
44      }
45  
46      @Override
47      public void configure(Binder binder) {
48          if (extension.getKey() != null) {
49              XmlNode configuration = extension.getConfiguration();
50              if (configuration == null) {
51                  configuration = new XmlNodeImpl("configuration");
52              }
53              configuration = new Interpolator().transform(configuration);
54  
55              binder.bind(XmlNode.class)
56                      .annotatedWith(Names.named(extension.getKey()))
57                      .toInstance(configuration);
58              binder.bind(PlexusConfiguration.class)
59                      .annotatedWith(Names.named(extension.getKey()))
60                      .toInstance(XmlPlexusConfiguration.toPlexusConfiguration(configuration));
61          }
62      }
63  
64      class Interpolator extends MavenTransformer {
65          final StringSearchInterpolator interpolator;
66  
67          Interpolator() {
68              super(null);
69              interpolator = new StringSearchInterpolator();
70              interpolator.setCacheAnswers(true);
71              valueSources.forEach(interpolator::addValueSource);
72          }
73  
74          public XmlNode transform(XmlNode node) {
75              return super.transform(node);
76          }
77  
78          protected String transform(String str) {
79              try {
80                  return interpolator.interpolate(str);
81              } catch (InterpolationException e) {
82                  throw new RuntimeException(e);
83              }
84          }
85      }
86  }