1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.toolchain.building;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.lang.reflect.Method;
28 import java.nio.file.Path;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33
34 import org.apache.maven.api.Session;
35 import org.apache.maven.api.services.BuilderProblem;
36 import org.apache.maven.api.services.Source;
37 import org.apache.maven.api.services.ToolchainsBuilderException;
38 import org.apache.maven.api.services.ToolchainsBuilderRequest;
39 import org.apache.maven.api.services.ToolchainsBuilderResult;
40 import org.apache.maven.api.services.xml.ToolchainsXmlFactory;
41 import org.apache.maven.building.FileSource;
42 import org.apache.maven.building.Problem;
43 import org.apache.maven.building.ProblemCollector;
44 import org.apache.maven.building.ProblemCollectorFactory;
45 import org.apache.maven.toolchain.model.PersistedToolchains;
46 import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
47
48
49
50
51
52 @Named
53 @Singleton
54 public class DefaultToolchainsBuilder implements ToolchainsBuilder {
55 private final org.apache.maven.api.services.ToolchainsBuilder builder;
56 private final ToolchainsXmlFactory toolchainsXmlFactory;
57
58 @Inject
59 public DefaultToolchainsBuilder(
60 org.apache.maven.api.services.ToolchainsBuilder builder, ToolchainsXmlFactory toolchainsXmlFactory) {
61 this.builder = builder;
62 this.toolchainsXmlFactory = toolchainsXmlFactory;
63 }
64
65 @Override
66 public ToolchainsBuildingResult build(ToolchainsBuildingRequest request) throws ToolchainsBuildingException {
67 try {
68 ToolchainsBuilderResult result = builder.build(ToolchainsBuilderRequest.builder()
69 .session((Session) java.lang.reflect.Proxy.newProxyInstance(
70 Session.class.getClassLoader(),
71 new Class[] {Session.class},
72 (Object proxy, Method method, Object[] args) -> {
73 if ("getSystemProperties".equals(method.getName())) {
74 Map<String, String> properties = new HashMap<>();
75 Properties env = OperatingSystemUtils.getSystemEnvVars();
76 env.stringPropertyNames()
77 .forEach(k -> properties.put("env." + k, env.getProperty(k)));
78 return properties;
79 } else if ("getUserProperties".equals(method.getName())) {
80 return Map.of();
81 } else if ("getService".equals(method.getName())) {
82 if (args[0] == ToolchainsXmlFactory.class) {
83 return toolchainsXmlFactory;
84 }
85 }
86 return null;
87 }))
88 .installationToolchainsSource(convert(request.getGlobalToolchainsSource()))
89 .userToolchainsSource(convert(request.getUserToolchainsSource()))
90 .build());
91
92 return new DefaultToolchainsBuildingResult(
93 new PersistedToolchains(result.getEffectiveToolchains()), convert(result.getProblems()));
94 } catch (ToolchainsBuilderException e) {
95 throw new ToolchainsBuildingException(convert(e.getProblems()));
96 }
97 }
98
99 private Source convert(org.apache.maven.building.Source source) {
100 if (source instanceof FileSource fs) {
101 return Source.fromPath(fs.getPath());
102 } else if (source != null) {
103 return new Source() {
104 @Override
105 public Path getPath() {
106 return null;
107 }
108
109 @Override
110 public InputStream openStream() throws IOException {
111 return source.getInputStream();
112 }
113
114 @Override
115 public String getLocation() {
116 return source.getLocation();
117 }
118
119 @Override
120 public Source resolve(String relative) {
121 return null;
122 }
123 };
124 } else {
125 return null;
126 }
127 }
128
129 private List<Problem> convert(List<BuilderProblem> problems) {
130 ProblemCollector collector = ProblemCollectorFactory.newInstance(null);
131 problems.forEach(p -> collector.add(
132 Problem.Severity.valueOf(p.getSeverity().name()),
133 p.getMessage(),
134 p.getLineNumber(),
135 p.getColumnNumber(),
136 p.getException()));
137 return collector.getProblems();
138 }
139 }