1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
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.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import org.apache.maven.api.annotations.Nonnull;
32 import org.apache.maven.api.services.BuilderProblem;
33 import org.apache.maven.api.services.Source;
34 import org.apache.maven.api.services.ToolchainsBuilder;
35 import org.apache.maven.api.services.ToolchainsBuilderException;
36 import org.apache.maven.api.services.ToolchainsBuilderRequest;
37 import org.apache.maven.api.services.ToolchainsBuilderResult;
38 import org.apache.maven.api.toolchain.PersistedToolchains;
39 import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
40 import org.apache.maven.toolchain.building.ToolchainsBuildingException;
41 import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
42
43 @Named
44 @Singleton
45 public class DefaultToolchainsBuilder implements ToolchainsBuilder {
46
47 private final org.apache.maven.toolchain.building.ToolchainsBuilder builder;
48
49 @Inject
50 public DefaultToolchainsBuilder(org.apache.maven.toolchain.building.ToolchainsBuilder builder) {
51 this.builder = builder;
52 }
53
54 @Nonnull
55 @Override
56 public ToolchainsBuilderResult build(ToolchainsBuilderRequest request)
57 throws ToolchainsBuilderException, IllegalArgumentException {
58 DefaultSession session = (DefaultSession) request.getSession();
59 try {
60 DefaultToolchainsBuildingRequest req = new DefaultToolchainsBuildingRequest();
61 if (request.getGlobalToolchainsSource().isPresent()) {
62 req.setGlobalToolchainsSource(new MappedToolchainsSource(
63 request.getGlobalToolchainsSource().get()));
64 } else if (request.getGlobalToolchainsPath().isPresent()) {
65 req.setGlobalToolchainsSource(new org.apache.maven.building.FileSource(
66 request.getGlobalToolchainsPath().get().toFile()));
67 }
68 if (request.getUserToolchainsSource().isPresent()) {
69 req.setUserToolchainsSource(new MappedToolchainsSource(
70 request.getUserToolchainsSource().get()));
71 } else if (request.getUserToolchainsPath().isPresent()) {
72 req.setUserToolchainsSource(new org.apache.maven.building.FileSource(
73 request.getUserToolchainsPath().get().toFile()));
74 }
75 ToolchainsBuildingResult result = builder.build(req);
76 return new ToolchainsBuilderResult() {
77 @Override
78 public PersistedToolchains getEffectiveToolchains() {
79 return result.getEffectiveToolchains().getDelegate();
80 }
81
82 @Override
83 public List<BuilderProblem> getProblems() {
84 return new MappedList<>(result.getProblems(), MappedBuilderProblem::new);
85 }
86 };
87 } catch (ToolchainsBuildingException e) {
88 throw new ToolchainsBuilderException("Unable to build Toolchains", e);
89 }
90 }
91
92 private Properties toProperties(Map<String, String> map) {
93 Properties properties = new Properties();
94 properties.putAll(map);
95 return properties;
96 }
97
98 private static class MappedToolchainsSource implements org.apache.maven.building.Source {
99 private final Source source;
100
101 MappedToolchainsSource(Source source) {
102 this.source = source;
103 }
104
105 @Override
106 public InputStream getInputStream() throws IOException {
107 return source.getInputStream();
108 }
109
110 @Override
111 public String getLocation() {
112 return source.getLocation();
113 }
114 }
115
116 private static class MappedBuilderProblem implements BuilderProblem {
117 private final org.apache.maven.building.Problem problem;
118
119 MappedBuilderProblem(org.apache.maven.building.Problem problem) {
120 this.problem = problem;
121 }
122
123 @Override
124 public String getSource() {
125 return problem.getSource();
126 }
127
128 @Override
129 public int getLineNumber() {
130 return problem.getLineNumber();
131 }
132
133 @Override
134 public int getColumnNumber() {
135 return problem.getColumnNumber();
136 }
137
138 @Override
139 public String getLocation() {
140 return problem.getLocation();
141 }
142
143 @Override
144 public Exception getException() {
145 return problem.getException();
146 }
147
148 @Override
149 public String getMessage() {
150 return problem.getMessage();
151 }
152
153 @Override
154 public Severity getSeverity() {
155 return Severity.valueOf(problem.getSeverity().name());
156 }
157 }
158 }