1   package org.apache.maven.settings.merge;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.settings.IdentifiableBase;
28  import org.apache.maven.settings.Settings;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  
32  
33  
34  
35  public class MavenSettingsMerger
36  {
37  
38      
39  
40  
41  
42  
43      public void merge( Settings dominant, Settings recessive, String recessiveSourceLevel )
44      {
45          if ( dominant == null || recessive == null )
46          {
47              return;
48          }
49  
50          recessive.setSourceLevel( recessiveSourceLevel );
51  
52          List<String> dominantActiveProfiles = dominant.getActiveProfiles();
53          List<String> recessiveActiveProfiles = recessive.getActiveProfiles();
54  
55          if ( recessiveActiveProfiles != null )
56          {
57              if ( dominantActiveProfiles == null )
58              {
59                  dominantActiveProfiles = new ArrayList<String>();
60                  dominant.setActiveProfiles( dominantActiveProfiles );
61              }
62  
63              for ( String profileId : recessiveActiveProfiles )
64              {
65                  if ( !dominantActiveProfiles.contains( profileId ) )
66                  {
67                      dominantActiveProfiles.add( profileId );
68                  }
69              }
70          }
71  
72          List<String> dominantPluginGroupIds = dominant.getPluginGroups();
73  
74          List<String> recessivePluginGroupIds = recessive.getPluginGroups();
75  
76          if ( recessivePluginGroupIds != null )
77          {
78              if ( dominantPluginGroupIds == null )
79              {
80                  dominantPluginGroupIds = new ArrayList<String>();
81                  dominant.setPluginGroups( dominantPluginGroupIds );
82              }
83  
84              for ( String pluginGroupId : recessivePluginGroupIds )
85              {
86                  if ( !dominantPluginGroupIds.contains( pluginGroupId ) )
87                  {
88                      dominantPluginGroupIds.add( pluginGroupId );
89                  }
90              }
91          }
92  
93          if ( StringUtils.isEmpty( dominant.getLocalRepository() ) )
94          {
95              dominant.setLocalRepository( recessive.getLocalRepository() );
96          }
97  
98          shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel );
99          shallowMergeById( dominant.getServers(), recessive.getServers(), recessiveSourceLevel );
100         shallowMergeById( dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel );
101         shallowMergeById( dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel );
102 
103     }
104 
105     
106 
107 
108 
109 
110     private static <T extends IdentifiableBase> void shallowMergeById( List<T> dominant, List<T> recessive,
111                                                                        String recessiveSourceLevel )
112     {
113         Map<String, T> dominantById = mapById( dominant );
114 
115         for ( T identifiable : recessive )
116         {
117             if ( !dominantById.containsKey( identifiable.getId() ) )
118             {
119                 identifiable.setSourceLevel( recessiveSourceLevel );
120 
121                 dominant.add( identifiable );
122             }
123         }
124     }
125 
126     
127 
128 
129 
130     private static <T extends IdentifiableBase> Map<String, T> mapById( List<T> identifiables )
131     {
132         Map<String, T> byId = new HashMap<String, T>();
133 
134         for ( T identifiable : identifiables )
135         {
136             byId.put( identifiable.getId(), identifiable );
137         }
138 
139         return byId;
140     }
141 
142 }