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