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