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 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   * @author mkleint
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                 // TODO why is this using the context
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; // NOI18N
117     }
118 }