001package org.apache.maven.settings.merge;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.maven.settings.IdentifiableBase;
028import org.apache.maven.settings.Settings;
029import org.codehaus.plexus.util.StringUtils;
030
031/**
032 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
033 * @author Benjamin Bentmann
034 */
035public class MavenSettingsMerger
036{
037
038    /**
039     * @param dominant
040     * @param recessive
041     * @param recessiveSourceLevel
042     */
043    public void merge( Settings dominant, Settings recessive, String recessiveSourceLevel )
044    {
045        if ( dominant == null || recessive == null )
046        {
047            return;
048        }
049
050        recessive.setSourceLevel( recessiveSourceLevel );
051
052        List<String> dominantActiveProfiles = dominant.getActiveProfiles();
053        List<String> recessiveActiveProfiles = recessive.getActiveProfiles();
054
055        if ( recessiveActiveProfiles != null )
056        {
057            if ( dominantActiveProfiles == null )
058            {
059                dominantActiveProfiles = new ArrayList<>();
060                dominant.setActiveProfiles( dominantActiveProfiles );
061            }
062
063            for ( String profileId : recessiveActiveProfiles )
064            {
065                if ( !dominantActiveProfiles.contains( profileId ) )
066                {
067                    dominantActiveProfiles.add( profileId );
068                }
069            }
070        }
071
072        List<String> dominantPluginGroupIds = dominant.getPluginGroups();
073
074        List<String> recessivePluginGroupIds = recessive.getPluginGroups();
075
076        if ( recessivePluginGroupIds != null )
077        {
078            if ( dominantPluginGroupIds == null )
079            {
080                dominantPluginGroupIds = new ArrayList<>();
081                dominant.setPluginGroups( dominantPluginGroupIds );
082            }
083
084            for ( String pluginGroupId : recessivePluginGroupIds )
085            {
086                if ( !dominantPluginGroupIds.contains( pluginGroupId ) )
087                {
088                    dominantPluginGroupIds.add( pluginGroupId );
089                }
090            }
091        }
092
093        if ( StringUtils.isEmpty( dominant.getLocalRepository() ) )
094        {
095            dominant.setLocalRepository( recessive.getLocalRepository() );
096        }
097
098        shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel );
099        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<>();
133
134        for ( T identifiable : identifiables )
135        {
136            byId.put( identifiable.getId(), identifiable );
137        }
138
139        return byId;
140    }
141
142}