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.merge;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.settings.IdentifiableBase;
27  import org.apache.maven.settings.Settings;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  /**
31   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
32   * @author Benjamin Bentmann
33   */
34  public class MavenSettingsMerger {
35  
36      /**
37       * @param dominant
38       * @param recessive
39       * @param recessiveSourceLevel
40       */
41      public void merge(Settings dominant, Settings recessive, String recessiveSourceLevel) {
42          if (dominant == null || recessive == null) {
43              return;
44          }
45  
46          recessive.setSourceLevel(recessiveSourceLevel);
47  
48          List<String> dominantActiveProfiles = dominant.getActiveProfiles();
49          List<String> recessiveActiveProfiles = recessive.getActiveProfiles();
50  
51          if (recessiveActiveProfiles != null) {
52              if (dominantActiveProfiles == null) {
53                  dominantActiveProfiles = new ArrayList<>();
54                  dominant.setActiveProfiles(dominantActiveProfiles);
55              }
56  
57              for (String profileId : recessiveActiveProfiles) {
58                  if (!dominantActiveProfiles.contains(profileId)) {
59                      dominantActiveProfiles.add(profileId);
60                  }
61              }
62          }
63  
64          List<String> dominantPluginGroupIds = dominant.getPluginGroups();
65  
66          List<String> recessivePluginGroupIds = recessive.getPluginGroups();
67  
68          if (recessivePluginGroupIds != null) {
69              if (dominantPluginGroupIds == null) {
70                  dominantPluginGroupIds = new ArrayList<>();
71                  dominant.setPluginGroups(dominantPluginGroupIds);
72              }
73  
74              for (String pluginGroupId : recessivePluginGroupIds) {
75                  if (!dominantPluginGroupIds.contains(pluginGroupId)) {
76                      dominantPluginGroupIds.add(pluginGroupId);
77                  }
78              }
79          }
80  
81          if (StringUtils.isEmpty(dominant.getLocalRepository())) {
82              dominant.setLocalRepository(recessive.getLocalRepository());
83          }
84  
85          shallowMergeById(dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel);
86          shallowMergeById(dominant.getServers(), recessive.getServers(), recessiveSourceLevel);
87          shallowMergeById(dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel);
88          shallowMergeById(dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel);
89      }
90  
91      /**
92       * @param dominant
93       * @param recessive
94       * @param recessiveSourceLevel
95       */
96      private static <T extends IdentifiableBase> void shallowMergeById(
97              List<T> dominant, List<T> recessive, String recessiveSourceLevel) {
98          Map<String, T> dominantById = mapById(dominant);
99  
100         for (T identifiable : recessive) {
101             if (!dominantById.containsKey(identifiable.getId())) {
102                 identifiable.setSourceLevel(recessiveSourceLevel);
103 
104                 dominant.add(identifiable);
105             }
106         }
107     }
108 
109     /**
110      * @param identifiables
111      * @return a map
112      */
113     private static <T extends IdentifiableBase> Map<String, T> mapById(List<T> identifiables) {
114         Map<String, T> byId = new HashMap<>();
115 
116         for (T identifiable : identifiables) {
117             byId.put(identifiable.getId(), identifiable);
118         }
119 
120         return byId;
121     }
122 }