1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.toolchain;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.maven.execution.MavenSession;
32 import org.apache.maven.plugin.descriptor.PluginDescriptor;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.toolchain.model.ToolchainModel;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import static java.util.Objects.requireNonNull;
39
40
41
42
43 @Named
44 @Singleton
45 public class DefaultToolchainManager implements ToolchainManager {
46 protected final Logger logger;
47
48 final Map<String, ToolchainFactory> factories;
49
50 @Inject
51 public DefaultToolchainManager(Map<String, ToolchainFactory> factories) {
52 this.factories = factories;
53 this.logger = LoggerFactory.getLogger(DefaultToolchainManager.class);
54 }
55
56
57
58
59 DefaultToolchainManager(Map<String, ToolchainFactory> factories, Logger logger) {
60 this.factories = factories;
61 this.logger = requireNonNull(logger);
62 }
63
64 @Override
65 public Toolchain getToolchainFromBuildContext(String type, MavenSession session) {
66 Map<String, Object> context = retrieveContext(session);
67
68 ToolchainModel model = (ToolchainModel) context.get(getStorageKey(type));
69
70 if (model != null) {
71 List<Toolchain> toolchains = selectToolchains(Collections.singletonList(model), type, null);
72
73 if (!toolchains.isEmpty()) {
74 return toolchains.get(0);
75 }
76 }
77
78 return null;
79 }
80
81 @Override
82 public List<Toolchain> getToolchains(MavenSession session, String type, Map<String, String> requirements) {
83 List<ToolchainModel> models = session.getRequest().getToolchains().get(type);
84
85 return selectToolchains(models, type, requirements);
86 }
87
88 private List<Toolchain> selectToolchains(
89 List<ToolchainModel> models, String type, Map<String, String> requirements) {
90 List<Toolchain> toolchains = new ArrayList<>();
91
92 if (models != null) {
93 ToolchainFactory fact = factories.get(type);
94
95 if (fact == null) {
96 logger.error(
97 "Missing toolchain factory for type: " + type + ". Possibly caused by misconfigured project.");
98 } else {
99 for (ToolchainModel model : models) {
100 try {
101 ToolchainPrivate toolchain = fact.createToolchain(model);
102 if (requirements == null || toolchain.matchesRequirements(requirements)) {
103 toolchains.add(toolchain);
104 }
105 } catch (MisconfiguredToolchainException ex) {
106 logger.error("Misconfigured toolchain.", ex);
107 }
108 }
109 }
110 }
111 return toolchains;
112 }
113
114 Map<String, Object> retrieveContext(MavenSession session) {
115 Map<String, Object> context = null;
116
117 if (session != null) {
118 PluginDescriptor desc = new PluginDescriptor();
119 desc.setGroupId(PluginDescriptor.getDefaultPluginGroupId());
120 desc.setArtifactId(PluginDescriptor.getDefaultPluginArtifactId("toolchains"));
121
122 MavenProject current = session.getCurrentProject();
123
124 if (current != null) {
125
126 context = session.getPluginContext(desc, current);
127 }
128 }
129
130 return (context != null) ? context : new HashMap<>();
131 }
132
133 public static final String getStorageKey(String type) {
134 return "toolchain-" + type;
135 }
136 }