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.settings.building;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.lang.reflect.Method;
29  import java.nio.file.Path;
30  import java.util.List;
31  import java.util.stream.Collectors;
32  
33  import org.apache.maven.api.Session;
34  import org.apache.maven.api.services.BuilderProblem;
35  import org.apache.maven.api.services.SettingsBuilderException;
36  import org.apache.maven.api.services.SettingsBuilderRequest;
37  import org.apache.maven.api.services.SettingsBuilderResult;
38  import org.apache.maven.api.services.xml.SettingsXmlFactory;
39  import org.apache.maven.building.FileSource;
40  import org.apache.maven.building.Source;
41  import org.apache.maven.settings.Settings;
42  
43  /**
44   * Builds the effective settings from a user settings file and/or a global settings file.
45   *
46   */
47  @Named
48  @Singleton
49  public class DefaultSettingsBuilder implements SettingsBuilder {
50  
51      private final org.apache.maven.internal.impl.DefaultSettingsBuilder builder;
52      private final org.apache.maven.internal.impl.DefaultSettingsXmlFactory settingsXmlFactory;
53  
54      @Inject
55      public DefaultSettingsBuilder(
56              org.apache.maven.internal.impl.DefaultSettingsBuilder builder,
57              org.apache.maven.internal.impl.DefaultSettingsXmlFactory settingsXmlFactory) {
58          this.builder = builder;
59          this.settingsXmlFactory = settingsXmlFactory;
60      }
61  
62      @Override
63      public SettingsBuildingResult build(SettingsBuildingRequest request) throws SettingsBuildingException {
64  
65          try {
66              SettingsBuilderResult result = builder.build(SettingsBuilderRequest.builder()
67                      .session((Session) java.lang.reflect.Proxy.newProxyInstance(
68                              Session.class.getClassLoader(),
69                              new Class[] {Session.class},
70                              (Object proxy, Method method, Object[] args) -> {
71                                  if ("getSystemProperties".equals(method.getName())) {
72                                      return request.getSystemProperties().entrySet().stream()
73                                              .collect(Collectors.toMap(
74                                                      e -> e.getKey().toString(),
75                                                      e -> e.getValue().toString()));
76                                  } else if ("getUserProperties".equals(method.getName())) {
77                                      return request.getUserProperties().entrySet().stream()
78                                              .collect(Collectors.toMap(
79                                                      e -> e.getKey().toString(),
80                                                      e -> e.getValue().toString()));
81                                  } else if ("getService".equals(method.getName())) {
82                                      if (args[0] == SettingsXmlFactory.class) {
83                                          return settingsXmlFactory;
84                                      }
85                                  }
86                                  return null;
87                              }))
88                      .installationSettingsSource(
89                              toSource(request.getGlobalSettingsFile(), request.getGlobalSettingsSource()))
90                      .projectSettingsSource(
91                              toSource(request.getProjectSettingsFile(), request.getProjectSettingsSource()))
92                      .userSettingsSource(toSource(request.getUserSettingsFile(), request.getUserSettingsSource()))
93                      .build());
94  
95              return new DefaultSettingsBuildingResult(
96                      new Settings(result.getEffectiveSettings()), convert(result.getProblems()));
97          } catch (SettingsBuilderException e) {
98              throw new SettingsBuildingException(convert(e.getProblems()));
99          }
100     }
101 
102     private org.apache.maven.api.services.Source toSource(File file, Source source) {
103         if (file != null && file.exists()) {
104             return org.apache.maven.api.services.Source.fromPath(file.toPath());
105         } else if (source instanceof FileSource fs) {
106             return org.apache.maven.api.services.Source.fromPath(fs.getPath());
107         } else if (source != null) {
108             return new org.apache.maven.api.services.Source() {
109                 @Override
110                 public Path getPath() {
111                     return null;
112                 }
113 
114                 @Override
115                 public InputStream openStream() throws IOException {
116                     return source.getInputStream();
117                 }
118 
119                 @Override
120                 public String getLocation() {
121                     return source.getLocation();
122                 }
123 
124                 @Override
125                 public org.apache.maven.api.services.Source resolve(String relative) {
126                     return null;
127                 }
128             };
129         } else {
130             return null;
131         }
132     }
133 
134     private List<SettingsProblem> convert(List<BuilderProblem> problems) {
135         return problems.stream().map(this::convert).toList();
136     }
137 
138     private SettingsProblem convert(BuilderProblem problem) {
139         return new DefaultSettingsProblem(
140                 problem.getMessage(),
141                 SettingsProblem.Severity.valueOf(problem.getSeverity().name()),
142                 problem.getSource(),
143                 problem.getLineNumber(),
144                 problem.getColumnNumber(),
145                 problem.getException());
146     }
147 }