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.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  @Deprecated
36  public class ExtensionConfigurationModule implements Module {
37  
38      private final CoreExtensionEntry extension;
39      private final Function<String, String> callback;
40      private final DefaultInterpolator interpolator = new DefaultInterpolator();
41  
42      public ExtensionConfigurationModule(CoreExtensionEntry extension, Function<String, String> callback) {
43          this.extension = extension;
44          this.callback = callback;
45      }
46  
47      @Override
48      public void configure(Binder binder) {
49          if (extension.getKey() != null) {
50              XmlNode configuration = extension.getConfiguration();
51              if (configuration == null) {
52                  configuration = new XmlNodeImpl("configuration");
53              }
54              Function<String, String> cb = Interpolator.memoize(callback);
55              Function<String, String> it = s -> interpolator.interpolate(s, cb);
56              configuration = new ExtensionInterpolator(it).transform(configuration);
57  
58              binder.bind(XmlNode.class)
59                      .annotatedWith(Names.named(extension.getKey()))
60                      .toInstance(configuration);
61              binder.bind(PlexusConfiguration.class)
62                      .annotatedWith(Names.named(extension.getKey()))
63                      .toInstance(XmlPlexusConfiguration.toPlexusConfiguration(configuration));
64          }
65      }
66  
67      static class ExtensionInterpolator extends MavenTransformer {
68          ExtensionInterpolator(Function<String, String> transformer) {
69              super(transformer);
70          }
71  
72          public XmlNode transform(XmlNode node) {
73              return super.transform(node);
74          }
75      }
76  }