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