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.List;
27  import java.util.Map;
28  
29  import org.apache.maven.api.toolchain.ToolchainModel;
30  import org.apache.maven.execution.MavenSession;
31  import org.slf4j.Logger;
32  
33  /**
34   * TODO: refactor this, component extending component is bad practice.
35   *
36   * @author mkleint
37   * @author Robert Scholte
38   */
39  @Named
40  @Singleton
41  public class DefaultToolchainManagerPrivate extends DefaultToolchainManager implements ToolchainManagerPrivate {
42      @Inject
43      public DefaultToolchainManagerPrivate(Map<String, ToolchainFactory> factories) {
44          super(factories);
45      }
46  
47      /**
48       * Ctor needed for UT.
49       */
50      DefaultToolchainManagerPrivate(Map<String, ToolchainFactory> factories, Logger logger) {
51          super(factories, logger);
52      }
53  
54      @Override
55      public ToolchainPrivate[] getToolchainsForType(String type, MavenSession session)
56              throws MisconfiguredToolchainException {
57          List<ToolchainPrivate> toRet = new ArrayList<>();
58  
59          ToolchainFactory fact = factories.get(type);
60          if (fact == null) {
61              logger.error("Missing toolchain factory for type: " + type + ". Possibly caused by misconfigured project.");
62          } else {
63              List<ToolchainModel> availableToolchains =
64                      org.apache.maven.toolchain.model.ToolchainModel.toolchainModelToApiV4(
65                              session.getRequest().getToolchains().get(type));
66  
67              if (availableToolchains != null) {
68                  for (ToolchainModel toolchainModel : availableToolchains) {
69                      org.apache.maven.toolchain.model.ToolchainModel tm =
70                              new org.apache.maven.toolchain.model.ToolchainModel(toolchainModel);
71                      toRet.add(fact.createToolchain(tm));
72                  }
73              }
74  
75              // add default toolchain
76              ToolchainPrivate tool = fact.createDefaultToolchain();
77              if (tool != null) {
78                  toRet.add(tool);
79              }
80          }
81  
82          return toRet.toArray(new ToolchainPrivate[0]);
83      }
84  
85      @Override
86      public void storeToolchainToBuildContext(ToolchainPrivate toolchain, MavenSession session) {
87          Map<String, Object> context = retrieveContext(session);
88          context.put(getStorageKey(toolchain.getType()), toolchain.getModel());
89      }
90  }