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  import java.util.Map;
29  import java.util.Properties;
30  
31  import org.apache.maven.api.annotations.Nonnull;
32  import org.apache.maven.api.services.BuilderProblem;
33  import org.apache.maven.api.services.SettingsBuilder;
34  import org.apache.maven.api.services.SettingsBuilderException;
35  import org.apache.maven.api.services.SettingsBuilderRequest;
36  import org.apache.maven.api.services.SettingsBuilderResult;
37  import org.apache.maven.api.services.Source;
38  import org.apache.maven.api.settings.Settings;
39  import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
40  import org.apache.maven.settings.building.SettingsBuildingException;
41  import org.apache.maven.settings.building.SettingsBuildingResult;
42  import org.apache.maven.settings.building.SettingsProblem;
43  import org.apache.maven.settings.building.SettingsSource;
44  
45  @Named
46  @Singleton
47  public class DefaultSettingsBuilder implements SettingsBuilder {
48  
49      private final org.apache.maven.settings.building.SettingsBuilder builder;
50  
51      @Inject
52      public DefaultSettingsBuilder(org.apache.maven.settings.building.SettingsBuilder builder) {
53          this.builder = builder;
54      }
55  
56      @Nonnull
57      @Override
58      public SettingsBuilderResult build(SettingsBuilderRequest request)
59              throws SettingsBuilderException, IllegalArgumentException {
60          InternalSession session = InternalSession.from(request.getSession());
61          try {
62              DefaultSettingsBuildingRequest req = new DefaultSettingsBuildingRequest();
63              req.setUserProperties(toProperties(session.getUserProperties()));
64              req.setSystemProperties(toProperties(session.getSystemProperties()));
65              if (request.getGlobalSettingsSource().isPresent()) {
66                  req.setGlobalSettingsSource(new MappedSettingsSource(
67                          request.getGlobalSettingsSource().get()));
68              }
69              if (request.getGlobalSettingsPath().isPresent()) {
70                  req.setGlobalSettingsFile(request.getGlobalSettingsPath().get().toFile());
71              }
72              if (request.getUserSettingsSource().isPresent()) {
73                  req.setUserSettingsSource(
74                          new MappedSettingsSource(request.getUserSettingsSource().get()));
75              }
76              if (request.getUserSettingsPath().isPresent()) {
77                  req.setUserSettingsFile(request.getUserSettingsPath().get().toFile());
78              }
79              SettingsBuildingResult result = builder.build(req);
80              return new SettingsBuilderResult() {
81                  @Override
82                  public Settings getEffectiveSettings() {
83                      return result.getEffectiveSettings().getDelegate();
84                  }
85  
86                  @Override
87                  public List<BuilderProblem> getProblems() {
88                      return new MappedList<>(result.getProblems(), MappedBuilderProblem::new);
89                  }
90              };
91          } catch (SettingsBuildingException e) {
92              throw new SettingsBuilderException("Unable to build settings", e);
93          }
94      }
95  
96      private Properties toProperties(Map<String, String> map) {
97          Properties properties = new Properties();
98          properties.putAll(map);
99          return properties;
100     }
101 
102     private static class MappedSettingsSource implements SettingsSource {
103         private final Source source;
104 
105         MappedSettingsSource(Source source) {
106             this.source = source;
107         }
108 
109         @Override
110         public InputStream getInputStream() throws IOException {
111             return source.openStream();
112         }
113 
114         @Override
115         public String getLocation() {
116             return source.getLocation();
117         }
118     }
119 
120     private static class MappedBuilderProblem implements BuilderProblem {
121         private final SettingsProblem problem;
122 
123         MappedBuilderProblem(SettingsProblem problem) {
124             this.problem = problem;
125         }
126 
127         @Override
128         public String getSource() {
129             return problem.getSource();
130         }
131 
132         @Override
133         public int getLineNumber() {
134             return problem.getLineNumber();
135         }
136 
137         @Override
138         public int getColumnNumber() {
139             return problem.getColumnNumber();
140         }
141 
142         @Override
143         public String getLocation() {
144             return problem.getLocation();
145         }
146 
147         @Override
148         public Exception getException() {
149             return problem.getException();
150         }
151 
152         @Override
153         public String getMessage() {
154             return problem.getMessage();
155         }
156 
157         @Override
158         public Severity getSeverity() {
159             return Severity.valueOf(problem.getSeverity().name());
160         }
161     }
162 }