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