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 java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.execution.MavenSession;
28 import org.apache.maven.plugin.descriptor.PluginDescriptor;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.toolchain.model.ToolchainModel;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.component.annotations.Requirement;
33 import org.codehaus.plexus.logging.Logger;
34
35
36
37
38 @Component(role = ToolchainManager.class)
39 public class DefaultToolchainManager implements ToolchainManager {
40 @Requirement
41 Logger logger;
42
43 @Requirement(role = ToolchainFactory.class)
44 Map<String, ToolchainFactory> factories;
45
46 @Override
47 public Toolchain getToolchainFromBuildContext(String type, MavenSession session) {
48 Map<String, Object> context = retrieveContext(session);
49
50 ToolchainModel model = (ToolchainModel) context.get(getStorageKey(type));
51
52 if (model != null) {
53 List<Toolchain> toolchains = selectToolchains(Collections.singletonList(model), type, null);
54
55 if (!toolchains.isEmpty()) {
56 return toolchains.get(0);
57 }
58 }
59
60 return null;
61 }
62
63 @Override
64 public List<Toolchain> getToolchains(MavenSession session, String type, Map<String, String> requirements) {
65 List<ToolchainModel> models = session.getRequest().getToolchains().get(type);
66
67 return selectToolchains(models, type, requirements);
68 }
69
70 private List<Toolchain> selectToolchains(
71 List<ToolchainModel> models, String type, Map<String, String> requirements) {
72 List<Toolchain> toolchains = new ArrayList<>();
73
74 if (models != null) {
75 ToolchainFactory fact = factories.get(type);
76
77 if (fact == null) {
78 logger.error(
79 "Missing toolchain factory for type: " + type + ". Possibly caused by misconfigured project.");
80 } else {
81 for (ToolchainModel model : models) {
82 try {
83 ToolchainPrivate toolchain = fact.createToolchain(model);
84 if (requirements == null || toolchain.matchesRequirements(requirements)) {
85 toolchains.add(toolchain);
86 }
87 } catch (MisconfiguredToolchainException ex) {
88 logger.error("Misconfigured toolchain.", ex);
89 }
90 }
91 }
92 }
93 return toolchains;
94 }
95
96 Map<String, Object> retrieveContext(MavenSession session) {
97 Map<String, Object> context = null;
98
99 if (session != null) {
100 PluginDescriptor desc = new PluginDescriptor();
101 desc.setGroupId(PluginDescriptor.getDefaultPluginGroupId());
102 desc.setArtifactId(PluginDescriptor.getDefaultPluginArtifactId("toolchains"));
103
104 MavenProject current = session.getCurrentProject();
105
106 if (current != null) {
107
108 context = session.getPluginContext(desc, current);
109 }
110 }
111
112 return (context != null) ? context : new HashMap<String, Object>();
113 }
114
115 public static final String getStorageKey(String type) {
116 return "toolchain-" + type;
117 }
118 }