View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author mkleint
40   */
41  @Named
42  @Singleton
43  public class DefaultToolchainManager implements ToolchainManager {
44      protected final Logger logger; // TODO this class is extended, needs refactoring
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       * Ctor needed for UT.
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                 // TODO why is this using the context
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; // NOI18N
133     }
134 }