View Javadoc

1   package org.apache.maven.settings.merge;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
33   * @author Benjamin Bentmann
34   */
35  public class MavenSettingsMerger
36  {
37  
38      /**
39       * @param dominant
40       * @param recessive
41       * @param recessiveSourceLevel
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      * @param dominant
107      * @param recessive
108      * @param recessiveSourceLevel
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      * @param identifiables
128      * @return a map
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 }