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;
20  
21  /*
22   * Licensed to the Apache Software Foundation (ASF) under one
23   * or more contributor license agreements.  See the NOTICE file
24   * distributed with this work for additional information
25   * regarding copyright ownership.  The ASF licenses this file
26   * to you under the Apache License, Version 2.0 (the
27   * "License"); you may not use this file except in compliance
28   * with the License.  You may obtain a copy of the License at
29   *
30   *  http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing,
33   * software distributed under the License is distributed on an
34   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35   * KIND, either express or implied.  See the License for the
36   * specific language governing permissions and limitations
37   * under the License.
38   */
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.stream.Collectors;
45  
46  import org.apache.maven.api.model.ActivationFile;
47  import org.apache.maven.api.model.InputLocation;
48  import org.apache.maven.api.settings.Activation;
49  import org.apache.maven.api.settings.ActivationOS;
50  import org.apache.maven.api.settings.ActivationProperty;
51  import org.apache.maven.api.settings.Profile;
52  import org.apache.maven.api.settings.Repository;
53  import org.apache.maven.api.settings.RepositoryPolicy;
54  import org.apache.maven.api.settings.Settings;
55  import org.apache.maven.settings.v4.SettingsMerger;
56  
57  /**
58   * Several convenience methods to handle settings
59   *
60   */
61  public final class SettingsUtilsV4 {
62  
63      private SettingsUtilsV4() {
64          // don't allow construction.
65      }
66  
67      /**
68       * @param dominant
69       * @param recessive
70       */
71      public static Settings merge(Settings dominant, Settings recessive) {
72          return new SettingsMerger().merge(dominant, recessive, true, Collections.emptyMap());
73      }
74  
75      /**
76       * @param modelProfile
77       * @return a profile
78       */
79      public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profile modelProfile) {
80          Profile.Builder profile = Profile.newBuilder();
81  
82          profile.id(modelProfile.getId());
83  
84          org.apache.maven.api.model.Activation modelActivation = modelProfile.getActivation();
85  
86          if (modelActivation != null) {
87              Activation.Builder activation = Activation.newBuilder();
88  
89              activation.activeByDefault(modelActivation.isActiveByDefault());
90  
91              activation.jdk(modelActivation.getJdk());
92  
93              org.apache.maven.api.model.ActivationProperty modelProp = modelActivation.getProperty();
94  
95              if (modelProp != null) {
96                  ActivationProperty prop = ActivationProperty.newBuilder()
97                          .name(modelProp.getName())
98                          .value(modelProp.getValue())
99                          .build();
100                 activation.property(prop);
101             }
102 
103             org.apache.maven.api.model.ActivationOS modelOs = modelActivation.getOs();
104 
105             if (modelOs != null) {
106                 ActivationOS os = ActivationOS.newBuilder()
107                         .arch(modelOs.getArch())
108                         .family(modelOs.getFamily())
109                         .name(modelOs.getName())
110                         .version(modelOs.getVersion())
111                         .build();
112 
113                 activation.os(os);
114             }
115 
116             org.apache.maven.api.model.ActivationFile modelFile = modelActivation.getFile();
117 
118             if (modelFile != null) {
119                 org.apache.maven.api.settings.ActivationFile file =
120                         org.apache.maven.api.settings.ActivationFile.newBuilder()
121                                 .exists(modelFile.getExists())
122                                 .missing(modelFile.getMissing())
123                                 .build();
124 
125                 activation.file(file);
126             }
127 
128             profile.activation(activation.build());
129         }
130 
131         profile.properties(modelProfile.getProperties().entrySet().stream()
132                 .collect(Collectors.toMap(
133                         e -> e.getKey().toString(), e -> e.getValue().toString())));
134 
135         List<org.apache.maven.api.model.Repository> repos = modelProfile.getRepositories();
136         if (repos != null) {
137             List<Repository> repositories = new ArrayList<>();
138             for (org.apache.maven.api.model.Repository repo : repos) {
139                 repositories.add(convertToSettingsRepository(repo));
140             }
141             profile.repositories(repositories);
142         }
143 
144         List<org.apache.maven.api.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
145         if (pluginRepos != null) {
146             List<Repository> repositories = new ArrayList<>();
147             for (org.apache.maven.api.model.Repository pluginRepo : pluginRepos) {
148                 repositories.add(convertToSettingsRepository(pluginRepo));
149             }
150             profile.pluginRepositories(repositories);
151         }
152 
153         return profile.build();
154     }
155 
156     /**
157      * @param settingsProfile
158      * @return a profile
159      */
160     public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
161         org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
162 
163         profile.id(settingsProfile.getId());
164 
165         Activation settingsActivation = settingsProfile.getActivation();
166 
167         if (settingsActivation != null) {
168             org.apache.maven.api.model.Activation.Builder activation =
169                     org.apache.maven.api.model.Activation.newBuilder();
170 
171             activation.activeByDefault(settingsActivation.isActiveByDefault());
172             activation.location("activeByDefault", toLocation(settingsActivation.getLocation("activeByDefault")));
173 
174             activation.jdk(settingsActivation.getJdk());
175             activation.location("jdk", toLocation(settingsActivation.getLocation("jdk")));
176 
177             ActivationProperty settingsProp = settingsActivation.getProperty();
178             if (settingsProp != null) {
179                 activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
180                         .name(settingsProp.getName())
181                         .value(settingsProp.getValue())
182                         .location("name", toLocation(settingsProp.getLocation("name")))
183                         .location("value", toLocation(settingsProp.getLocation("value")))
184                         .build());
185             }
186 
187             ActivationOS settingsOs = settingsActivation.getOs();
188             if (settingsOs != null) {
189                 activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
190                         .arch(settingsOs.getArch())
191                         .family(settingsOs.getFamily())
192                         .name(settingsOs.getName())
193                         .version(settingsOs.getVersion())
194                         .location("arch", toLocation(settingsOs.getLocation("arch")))
195                         .location("family", toLocation(settingsOs.getLocation("family")))
196                         .location("name", toLocation(settingsOs.getLocation("name")))
197                         .location("version", toLocation(settingsOs.getLocation("version")))
198                         .build());
199             }
200 
201             org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
202             if (settingsFile != null) {
203                 activation.file(ActivationFile.newBuilder()
204                         .exists(settingsFile.getExists())
205                         .missing(settingsFile.getMissing())
206                         .location("exists", toLocation(settingsFile.getLocation("exists")))
207                         .location("missing", toLocation(settingsFile.getLocation("missing")))
208                         .build());
209             }
210 
211             profile.activation(activation.build());
212         }
213 
214         profile.properties(settingsProfile.getProperties());
215         profile.location("properties", toLocation(settingsProfile.getLocation("properties")));
216 
217         List<Repository> repos = settingsProfile.getRepositories();
218         if (repos != null) {
219             profile.repositories(repos.stream()
220                     .map(SettingsUtilsV4::convertFromSettingsRepository)
221                     .collect(Collectors.toList()));
222         }
223 
224         List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
225         if (pluginRepos != null) {
226             profile.pluginRepositories(pluginRepos.stream()
227                     .map(SettingsUtilsV4::convertFromSettingsRepository)
228                     .collect(Collectors.toList()));
229         }
230 
231         org.apache.maven.api.model.Profile value = profile.build();
232         value.setSource("settings.xml");
233         return value;
234     }
235 
236     /**
237      * @param settingsRepo
238      * @return a repository
239      */
240     private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
241         org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
242 
243         repo.id(settingsRepo.getId());
244         repo.layout(settingsRepo.getLayout());
245         repo.name(settingsRepo.getName());
246         repo.url(settingsRepo.getUrl());
247 
248         repo.location("id", toLocation(settingsRepo.getLocation("id")));
249         repo.location("layout", toLocation(settingsRepo.getLocation("layout")));
250         repo.location("name", toLocation(settingsRepo.getLocation("name")));
251         repo.location("url", toLocation(settingsRepo.getLocation("url")));
252 
253         if (settingsRepo.getSnapshots() != null) {
254             repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
255         }
256         if (settingsRepo.getReleases() != null) {
257             repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
258         }
259 
260         return repo.build();
261     }
262 
263     /**
264      * @param settingsPolicy
265      * @return a RepositoryPolicy
266      */
267     private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
268             RepositoryPolicy settingsPolicy) {
269         org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
270                 .enabled(Boolean.toString(settingsPolicy.isEnabled()))
271                 .updatePolicy(settingsPolicy.getUpdatePolicy())
272                 .checksumPolicy(settingsPolicy.getChecksumPolicy())
273                 .location("enabled", toLocation(settingsPolicy.getLocation("enabled")))
274                 .location("updatePolicy", toLocation(settingsPolicy.getLocation("updatePolicy")))
275                 .location("checksumPolicy", toLocation(settingsPolicy.getLocation("checksumPolicy")))
276                 .build();
277         return policy;
278     }
279 
280     /**
281      * @param modelRepo
282      * @return a repository
283      */
284     private static Repository convertToSettingsRepository(org.apache.maven.api.model.Repository modelRepo) {
285         Repository repo = Repository.newBuilder()
286                 .id(modelRepo.getId())
287                 .layout(modelRepo.getLayout())
288                 .name(modelRepo.getName())
289                 .url(modelRepo.getUrl())
290                 .snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
291                 .releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
292                 .build();
293 
294         return repo;
295     }
296 
297     /**
298      * @param modelPolicy
299      * @return a RepositoryPolicy
300      */
301     private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.api.model.RepositoryPolicy modelPolicy) {
302         RepositoryPolicy policy = RepositoryPolicy.newBuilder()
303                 .enabled(modelPolicy.isEnabled())
304                 .updatePolicy(modelPolicy.getUpdatePolicy())
305                 .checksumPolicy(modelPolicy.getChecksumPolicy())
306                 .build();
307         return policy;
308     }
309 
310     /**
311      * @param settings could be null
312      * @return a new instance of settings or null if settings was null.
313      */
314     public static org.apache.maven.settings.Settings copySettings(org.apache.maven.settings.Settings settings) {
315         if (settings == null) {
316             return null;
317         }
318         return new org.apache.maven.settings.Settings(settings.getDelegate());
319     }
320 
321     private static org.apache.maven.api.model.InputLocation toLocation(
322             org.apache.maven.api.settings.InputLocation location) {
323         if (location != null) {
324             org.apache.maven.api.settings.InputSource source = location.getSource();
325             Map<Object, InputLocation> locs = location.getLocations().entrySet().stream()
326                     .collect(Collectors.toMap(Map.Entry::getKey, e -> toLocation(e.getValue())));
327             return new org.apache.maven.api.model.InputLocation(
328                     location.getLineNumber(),
329                     location.getColumnNumber(),
330                     source != null ? new org.apache.maven.api.model.InputSource("", source.getLocation()) : null,
331                     locs);
332         } else {
333             return null;
334         }
335     }
336 }