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