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