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.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Optional;
29  
30  import org.apache.maven.api.Session;
31  import org.apache.maven.api.Toolchain;
32  import org.apache.maven.api.services.ToolchainManager;
33  import org.apache.maven.api.services.ToolchainManagerException;
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.toolchain.DefaultToolchainManagerPrivate;
36  import org.apache.maven.toolchain.MisconfiguredToolchainException;
37  import org.apache.maven.toolchain.ToolchainPrivate;
38  
39  @Named
40  @Singleton
41  public class DefaultToolchainManager implements ToolchainManager {
42      private final DefaultToolchainManagerPrivate toolchainManagerPrivate;
43  
44      @Inject
45      public DefaultToolchainManager(DefaultToolchainManagerPrivate toolchainManagerPrivate) {
46          this.toolchainManagerPrivate = toolchainManagerPrivate;
47      }
48  
49      @Override
50      public List<Toolchain> getToolchains(Session session, String type, Map<String, String> requirements)
51              throws ToolchainManagerException {
52          MavenSession s = ((DefaultSession) session).getMavenSession();
53          List<org.apache.maven.toolchain.Toolchain> toolchains =
54                  toolchainManagerPrivate.getToolchains(s, type, requirements);
55          return new MappedList<>(toolchains, this::toToolchain);
56      }
57  
58      @Override
59      public Optional<Toolchain> getToolchainFromBuildContext(Session session, String type)
60              throws ToolchainManagerException {
61          MavenSession s = ((DefaultSession) session).getMavenSession();
62          return Optional.ofNullable(toolchainManagerPrivate.getToolchainFromBuildContext(type, s))
63                  .map(this::toToolchain);
64      }
65  
66      @Override
67      public List<Toolchain> getToolchainsForType(Session session, String type) throws ToolchainManagerException {
68          try {
69              MavenSession s = ((DefaultSession) session).getMavenSession();
70              ToolchainPrivate[] toolchains = toolchainManagerPrivate.getToolchainsForType(type, s);
71              return new MappedList<>(Arrays.asList(toolchains), this::toToolchain);
72          } catch (MisconfiguredToolchainException e) {
73              throw new ToolchainManagerException("Unable to get toochains for type " + type, e);
74          }
75      }
76  
77      @Override
78      public void storeToolchainToBuildContext(Session session, Toolchain toolchain) throws ToolchainManagerException {
79          MavenSession s = ((DefaultSession) session).getMavenSession();
80          org.apache.maven.toolchain.ToolchainPrivate tc =
81                  (org.apache.maven.toolchain.ToolchainPrivate) ((ToolchainWrapper) toolchain).toolchain;
82          toolchainManagerPrivate.storeToolchainToBuildContext(tc, s);
83      }
84  
85      private Toolchain toToolchain(org.apache.maven.toolchain.Toolchain toolchain) {
86          return new ToolchainWrapper(toolchain);
87      }
88  
89      private static class ToolchainWrapper implements Toolchain {
90          private final org.apache.maven.toolchain.Toolchain toolchain;
91  
92          ToolchainWrapper(org.apache.maven.toolchain.Toolchain toolchain) {
93              this.toolchain = toolchain;
94          }
95  
96          @Override
97          public String getType() {
98              return toolchain.getType();
99          }
100 
101         @Override
102         public String findTool(String toolName) {
103             return toolchain.findTool(toolName);
104         }
105 
106         @Override
107         public boolean matchesRequirements(Map<String, String> requirements) {
108             return ((ToolchainPrivate) toolchain).matchesRequirements(requirements);
109         }
110     }
111 }