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