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.io.IOException;
26  import java.io.InputStream;
27  import java.util.List;
28  
29  import org.apache.maven.api.annotations.Nonnull;
30  import org.apache.maven.api.services.BuilderProblem;
31  import org.apache.maven.api.services.Source;
32  import org.apache.maven.api.services.ToolchainsBuilder;
33  import org.apache.maven.api.services.ToolchainsBuilderException;
34  import org.apache.maven.api.services.ToolchainsBuilderRequest;
35  import org.apache.maven.api.services.ToolchainsBuilderResult;
36  import org.apache.maven.api.toolchain.PersistedToolchains;
37  import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
38  import org.apache.maven.toolchain.building.ToolchainsBuildingException;
39  import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
40  
41  @Named
42  @Singleton
43  public class DefaultToolchainsBuilder implements ToolchainsBuilder {
44  
45      private final org.apache.maven.toolchain.building.ToolchainsBuilder builder;
46  
47      @Inject
48      public DefaultToolchainsBuilder(org.apache.maven.toolchain.building.ToolchainsBuilder builder) {
49          this.builder = builder;
50      }
51  
52      @Nonnull
53      @Override
54      public ToolchainsBuilderResult build(ToolchainsBuilderRequest request)
55              throws ToolchainsBuilderException, IllegalArgumentException {
56          try {
57              DefaultToolchainsBuildingRequest req = new DefaultToolchainsBuildingRequest();
58              if (request.getGlobalToolchainsSource().isPresent()) {
59                  req.setGlobalToolchainsSource(new MappedToolchainsSource(
60                          request.getGlobalToolchainsSource().get()));
61              } else if (request.getGlobalToolchainsPath().isPresent()) {
62                  req.setGlobalToolchainsSource(new org.apache.maven.building.FileSource(
63                          request.getGlobalToolchainsPath().get().toFile()));
64              }
65              if (request.getUserToolchainsSource().isPresent()) {
66                  req.setUserToolchainsSource(new MappedToolchainsSource(
67                          request.getUserToolchainsSource().get()));
68              } else if (request.getUserToolchainsPath().isPresent()) {
69                  req.setUserToolchainsSource(new org.apache.maven.building.FileSource(
70                          request.getUserToolchainsPath().get().toFile()));
71              }
72              ToolchainsBuildingResult result = builder.build(req);
73              return new ToolchainsBuilderResult() {
74                  @Override
75                  public PersistedToolchains getEffectiveToolchains() {
76                      return result.getEffectiveToolchains().getDelegate();
77                  }
78  
79                  @Override
80                  public List<BuilderProblem> getProblems() {
81                      return new MappedList<>(result.getProblems(), MappedBuilderProblem::new);
82                  }
83              };
84          } catch (ToolchainsBuildingException e) {
85              throw new ToolchainsBuilderException("Unable to build Toolchains", e);
86          }
87      }
88  
89      private static class MappedToolchainsSource implements org.apache.maven.building.Source {
90          private final Source source;
91  
92          MappedToolchainsSource(Source source) {
93              this.source = source;
94          }
95  
96          @Override
97          public InputStream getInputStream() throws IOException {
98              return source.openStream();
99          }
100 
101         @Override
102         public String getLocation() {
103             return source.getLocation();
104         }
105     }
106 
107     private static class MappedBuilderProblem implements BuilderProblem {
108         private final org.apache.maven.building.Problem problem;
109 
110         MappedBuilderProblem(org.apache.maven.building.Problem problem) {
111             this.problem = problem;
112         }
113 
114         @Override
115         public String getSource() {
116             return problem.getSource();
117         }
118 
119         @Override
120         public int getLineNumber() {
121             return problem.getLineNumber();
122         }
123 
124         @Override
125         public int getColumnNumber() {
126             return problem.getColumnNumber();
127         }
128 
129         @Override
130         public String getLocation() {
131             return problem.getLocation();
132         }
133 
134         @Override
135         public Exception getException() {
136             return problem.getException();
137         }
138 
139         @Override
140         public String getMessage() {
141             return problem.getMessage();
142         }
143 
144         @Override
145         public Severity getSeverity() {
146             return Severity.valueOf(problem.getSeverity().name());
147         }
148     }
149 }