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.util.Arrays;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Optional;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.inject.Singleton;
28 import org.apache.maven.api.Session;
29 import org.apache.maven.api.Toolchain;
30 import org.apache.maven.api.services.ToolchainManager;
31 import org.apache.maven.api.services.ToolchainManagerException;
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.toolchain.DefaultToolchainManagerPrivate;
34 import org.apache.maven.toolchain.MisconfiguredToolchainException;
35 import org.apache.maven.toolchain.ToolchainPrivate;
36
37 @Named
38 @Singleton
39 public class DefaultToolchainManager implements ToolchainManager {
40 private final DefaultToolchainManagerPrivate toolchainManagerPrivate;
41
42 @Inject
43 public DefaultToolchainManager(DefaultToolchainManagerPrivate toolchainManagerPrivate) {
44 this.toolchainManagerPrivate = toolchainManagerPrivate;
45 }
46
47 @Override
48 public List<Toolchain> getToolchains(Session session, String type, Map<String, String> requirements)
49 throws ToolchainManagerException {
50 MavenSession s = ((DefaultSession) session).getMavenSession();
51 List<org.apache.maven.toolchain.Toolchain> toolchains =
52 toolchainManagerPrivate.getToolchains(s, type, requirements);
53 return new MappedList<>(toolchains, this::toToolchain);
54 }
55
56 @Override
57 public Optional<Toolchain> getToolchainFromBuildContext(Session session, String type)
58 throws ToolchainManagerException {
59 MavenSession s = ((DefaultSession) session).getMavenSession();
60 return Optional.ofNullable(toolchainManagerPrivate.getToolchainFromBuildContext(type, s))
61 .map(this::toToolchain);
62 }
63
64 @Override
65 public List<Toolchain> getToolchainsForType(Session session, String type) throws ToolchainManagerException {
66 try {
67 MavenSession s = ((DefaultSession) session).getMavenSession();
68 ToolchainPrivate[] toolchains = toolchainManagerPrivate.getToolchainsForType(type, s);
69 return new MappedList<>(Arrays.asList(toolchains), this::toToolchain);
70 } catch (MisconfiguredToolchainException e) {
71 throw new ToolchainManagerException("Unable to get toochains for type " + type, e);
72 }
73 }
74
75 @Override
76 public void storeToolchainToBuildContext(Session session, Toolchain toolchain) throws ToolchainManagerException {
77 MavenSession s = ((DefaultSession) session).getMavenSession();
78 org.apache.maven.toolchain.ToolchainPrivate tc =
79 (org.apache.maven.toolchain.ToolchainPrivate) ((ToolchainWrapper) toolchain).toolchain;
80 toolchainManagerPrivate.storeToolchainToBuildContext(tc, s);
81 }
82
83 private Toolchain toToolchain(org.apache.maven.toolchain.Toolchain toolchain) {
84 return new ToolchainWrapper(toolchain);
85 }
86
87 private static class ToolchainWrapper implements Toolchain {
88 private final org.apache.maven.toolchain.Toolchain toolchain;
89
90 ToolchainWrapper(org.apache.maven.toolchain.Toolchain toolchain) {
91 this.toolchain = toolchain;
92 }
93
94 @Override
95 public String getType() {
96 return toolchain.getType();
97 }
98
99 @Override
100 public String findTool(String toolName) {
101 return toolchain.findTool(toolName);
102 }
103
104 @Override
105 public boolean matchesRequirements(Map<String, String> requirements) {
106 return ((ToolchainPrivate) toolchain).matchesRequirements(requirements);
107 }
108 }
109 }