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