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.cling.extensions;
20  
21  import java.util.function.Function;
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.services.Interpolator;
27  import org.apache.maven.api.xml.XmlNode;
28  import org.apache.maven.extension.internal.CoreExtensionEntry;
29  import org.apache.maven.internal.impl.model.DefaultInterpolator;
30  import org.apache.maven.internal.xml.XmlNodeImpl;
31  import org.apache.maven.internal.xml.XmlPlexusConfiguration;
32  import org.apache.maven.model.v4.MavenTransformer;
33  import org.codehaus.plexus.configuration.PlexusConfiguration;
34  
35  public class ExtensionConfigurationModule implements Module {
36  
37      private final CoreExtensionEntry extension;
38      private final Function<String, String> callback;
39      private final DefaultInterpolator interpolator = new DefaultInterpolator();
40  
41      public ExtensionConfigurationModule(CoreExtensionEntry extension, Function<String, String> callback) {
42          this.extension = extension;
43          this.callback = callback;
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              Function<String, String> cb = Interpolator.memoize(callback);
54              Function<String, String> it = s -> interpolator.interpolate(s, cb);
55              configuration = new ExtensionInterpolator(it).transform(configuration);
56  
57              binder.bind(XmlNode.class)
58                      .annotatedWith(Names.named(extension.getKey()))
59                      .toInstance(configuration);
60              binder.bind(PlexusConfiguration.class)
61                      .annotatedWith(Names.named(extension.getKey()))
62                      .toInstance(XmlPlexusConfiguration.toPlexusConfiguration(configuration));
63          }
64      }
65  
66      static class ExtensionInterpolator extends MavenTransformer {
67          ExtensionInterpolator(Function<String, String> transformer) {
68              super(transformer);
69          }
70  
71          public XmlNode transform(XmlNode node) {
72              return super.transform(node);
73          }
74      }
75  }