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 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; // TODO this class is extended, needs refactoring
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       * Ctor needed for UT.
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                 // TODO why is this using the context
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; // NOI18N
134     }
135 }