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 @Named
43 @Singleton
44 public class DefaultToolchainManager implements ToolchainManager {
45 protected final Logger logger;
46
47 final Map<String, ToolchainFactory> factories;
48
49 @Inject
50 public DefaultToolchainManager(Map<String, ToolchainFactory> factories) {
51 this.factories = factories;
52 this.logger = LoggerFactory.getLogger(DefaultToolchainManager.class);
53 }
54
55
56
57
58 DefaultToolchainManager(Map<String, ToolchainFactory> factories, Logger logger) {
59 this.factories = factories;
60 this.logger = requireNonNull(logger);
61 }
62
63 @Override
64 public Toolchain getToolchainFromBuildContext(String type, MavenSession session) {
65 Map<String, Object> context = retrieveContext(session);
66
67 ToolchainModel model = (ToolchainModel) context.get(getStorageKey(type));
68
69 if (model != null) {
70 List<Toolchain> toolchains = selectToolchains(Collections.singletonList(model), type, null);
71
72 if (!toolchains.isEmpty()) {
73 return toolchains.get(0);
74 }
75 }
76
77 return null;
78 }
79
80 @Override
81 public List<Toolchain> getToolchains(MavenSession session, String type, Map<String, String> requirements) {
82 List<ToolchainModel> models = session.getRequest().getToolchains().get(type);
83
84 return selectToolchains(models, type, requirements);
85 }
86
87 private List<Toolchain> selectToolchains(
88 List<ToolchainModel> models, String type, Map<String, String> requirements) {
89 List<Toolchain> toolchains = new ArrayList<>();
90
91 if (models != null) {
92 ToolchainFactory fact = factories.get(type);
93
94 if (fact == null) {
95 logger.error(
96 "Missing toolchain factory for type: " + type + ". Possibly caused by misconfigured project.");
97 } else {
98 for (ToolchainModel model : models) {
99 try {
100 ToolchainPrivate toolchain = fact.createToolchain(model);
101 if (requirements == null || toolchain.matchesRequirements(requirements)) {
102 toolchains.add(toolchain);
103 }
104 } catch (MisconfiguredToolchainException ex) {
105 logger.error("Misconfigured toolchain.", ex);
106 }
107 }
108 }
109 }
110 return toolchains;
111 }
112
113 Map<String, Object> retrieveContext(MavenSession session) {
114 Map<String, Object> context = null;
115
116 if (session != null) {
117 PluginDescriptor desc = new PluginDescriptor();
118 desc.setGroupId(PluginDescriptor.getDefaultPluginGroupId());
119 desc.setArtifactId(PluginDescriptor.getDefaultPluginArtifactId("toolchains"));
120
121 MavenProject current = session.getCurrentProject();
122
123 if (current != null) {
124
125 context = session.getPluginContext(desc, current);
126 }
127 }
128
129 return (context != null) ? context : new HashMap<>();
130 }
131
132 public static final String getStorageKey(String type) {
133 return "toolchain-" + type;
134 }
135 }