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