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
53 @Named
54 @Singleton
55 @Deprecated(since = "4.0.0")
56 public class DefaultToolchainsBuilder implements ToolchainsBuilder {
57 private final org.apache.maven.api.services.ToolchainsBuilder builder;
58 private final ToolchainsXmlFactory toolchainsXmlFactory;
59
60 @Inject
61 public DefaultToolchainsBuilder(
62 org.apache.maven.api.services.ToolchainsBuilder builder, ToolchainsXmlFactory toolchainsXmlFactory) {
63 this.builder = builder;
64 this.toolchainsXmlFactory = toolchainsXmlFactory;
65 }
66
67 @Override
68 public ToolchainsBuildingResult build(ToolchainsBuildingRequest request) throws ToolchainsBuildingException {
69 try {
70 ToolchainsBuilderResult result = builder.build(ToolchainsBuilderRequest.builder()
71 .session((Session) java.lang.reflect.Proxy.newProxyInstance(
72 Session.class.getClassLoader(),
73 new Class[] {Session.class},
74 (Object proxy, Method method, Object[] args) -> {
75 if ("getSystemProperties".equals(method.getName())) {
76 Map<String, String> properties = new HashMap<>();
77 Properties env = OperatingSystemUtils.getSystemEnvVars();
78 env.stringPropertyNames()
79 .forEach(k -> properties.put("env." + k, env.getProperty(k)));
80 return properties;
81 } else if ("getUserProperties".equals(method.getName())) {
82 return Map.of();
83 } else if ("getService".equals(method.getName())) {
84 if (args[0] == ToolchainsXmlFactory.class) {
85 return toolchainsXmlFactory;
86 }
87 }
88 return null;
89 }))
90 .installationToolchainsSource(convert(request.getGlobalToolchainsSource()))
91 .userToolchainsSource(convert(request.getUserToolchainsSource()))
92 .build());
93
94 return new DefaultToolchainsBuildingResult(
95 new PersistedToolchains(result.getEffectiveToolchains()), convert(result.getProblems()));
96 } catch (ToolchainsBuilderException e) {
97 throw new ToolchainsBuildingException(convert(e.getProblems()));
98 }
99 }
100
101 private Source convert(org.apache.maven.building.Source source) {
102 if (source instanceof FileSource fs) {
103 return Source.fromPath(fs.getPath());
104 } else if (source != null) {
105 return new Source() {
106 @Override
107 public Path getPath() {
108 return null;
109 }
110
111 @Override
112 public InputStream openStream() throws IOException {
113 return source.getInputStream();
114 }
115
116 @Override
117 public String getLocation() {
118 return source.getLocation();
119 }
120
121 @Override
122 public Source resolve(String relative) {
123 return null;
124 }
125 };
126 } else {
127 return null;
128 }
129 }
130
131 private List<Problem> convert(List<BuilderProblem> problems) {
132 ProblemCollector collector = ProblemCollectorFactory.newInstance(null);
133 problems.forEach(p -> collector.add(
134 Problem.Severity.valueOf(p.getSeverity().name()),
135 p.getMessage(),
136 p.getLineNumber(),
137 p.getColumnNumber(),
138 p.getException()));
139 return collector.getProblems();
140 }
141 }