1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }