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   * @deprecated use {@link org.apache.maven.settings.v4.SettingsMerger}
34   */
35  @Deprecated
36  public class MavenSettingsMerger {
37  
38      /**
39       * @param dominant
40       * @param recessive
41       * @param recessiveSourceLevel
42       */
43      public void merge(Settings dominant, Settings recessive, String recessiveSourceLevel) {
44          if (dominant == null || recessive == null) {
45              return;
46          }
47  
48          recessive.setSourceLevel(recessiveSourceLevel);
49  
50          List<String> dominantActiveProfiles = dominant.getActiveProfiles();
51          List<String> recessiveActiveProfiles = recessive.getActiveProfiles();
52  
53          if (recessiveActiveProfiles != null) {
54              if (dominantActiveProfiles == null) {
55                  dominantActiveProfiles = new ArrayList<>();
56                  dominant.setActiveProfiles(dominantActiveProfiles);
57              }
58  
59              for (String profileId : recessiveActiveProfiles) {
60                  if (!dominantActiveProfiles.contains(profileId)) {
61                      dominantActiveProfiles.add(profileId);
62                  }
63              }
64          }
65  
66          List<String> dominantPluginGroupIds = dominant.getPluginGroups();
67  
68          List<String> recessivePluginGroupIds = recessive.getPluginGroups();
69  
70          if (recessivePluginGroupIds != null) {
71              if (dominantPluginGroupIds == null) {
72                  dominantPluginGroupIds = new ArrayList<>();
73                  dominant.setPluginGroups(dominantPluginGroupIds);
74              }
75  
76              for (String pluginGroupId : recessivePluginGroupIds) {
77                  if (!dominantPluginGroupIds.contains(pluginGroupId)) {
78                      dominantPluginGroupIds.add(pluginGroupId);
79                  }
80              }
81          }
82  
83          if (StringUtils.isEmpty(dominant.getLocalRepository())) {
84              dominant.setLocalRepository(recessive.getLocalRepository());
85          }
86  
87          shallowMergeById(dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel);
88          shallowMergeById(dominant.getServers(), recessive.getServers(), recessiveSourceLevel);
89          shallowMergeById(dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel);
90          shallowMergeById(dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel);
91      }
92  
93      /**
94       * @param dominant
95       * @param recessive
96       * @param recessiveSourceLevel
97       */
98      private static <T extends IdentifiableBase> void shallowMergeById(
99              List<T> dominant, List<T> recessive, String recessiveSourceLevel) {
100         Map<String, T> dominantById = mapById(dominant);
101         final List<T> identifiables = new ArrayList<>(recessive.size());
102 
103         for (T identifiable : recessive) {
104             if (!dominantById.containsKey(identifiable.getId())) {
105                 identifiable.setSourceLevel(recessiveSourceLevel);
106 
107                 identifiables.add(identifiable);
108             }
109         }
110 
111         dominant.addAll(0, identifiables);
112     }
113 
114     /**
115      * @param identifiables
116      * @return a map
117      */
118     private static <T extends IdentifiableBase> Map<String, T> mapById(List<T> identifiables) {
119         Map<String, T> byId = new HashMap<>();
120 
121         for (T identifiable : identifiables) {
122             byId.put(identifiable.getId(), identifiable);
123         }
124 
125         return byId;
126     }
127 }