Coverage Report - org.apache.maven.settings.SettingsUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SettingsUtils
28 %
34/122
28 %
14/50
4,25
 
 1  
 package org.apache.maven.settings;
 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 org.apache.maven.model.ActivationFile;
 23  
 import org.codehaus.plexus.util.StringUtils;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.HashMap;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * Several convenience methods to handle settings
 33  
  *
 34  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 35  
  * @version $Id: SettingsUtils.java 682323 2008-08-04 11:14:28Z vsiveton $
 36  
  */
 37  
 public final class SettingsUtils
 38  
 {
 39  
     private SettingsUtils()
 40  0
     {
 41  
         // don't allow construction.
 42  0
     }
 43  
 
 44  
     /**
 45  
      * @param dominant
 46  
      * @param recessive
 47  
      * @param recessiveSourceLevel
 48  
      */
 49  
     public static void merge( Settings dominant, Settings recessive, String recessiveSourceLevel )
 50  
     {
 51  4
         if ( dominant == null || recessive == null )
 52  
         {
 53  0
             return;
 54  
         }
 55  
 
 56  4
         recessive.setSourceLevel( recessiveSourceLevel );
 57  
 
 58  4
         List dominantActiveProfiles = dominant.getActiveProfiles();
 59  4
         List recessiveActiveProfiles = recessive.getActiveProfiles();
 60  
 
 61  4
         if ( recessiveActiveProfiles != null )
 62  
         {
 63  4
             if ( dominantActiveProfiles == null )
 64  
             {
 65  0
                 dominantActiveProfiles = new ArrayList();
 66  0
                 dominant.setActiveProfiles( dominantActiveProfiles );
 67  
             }
 68  
 
 69  4
             for ( Iterator it = recessiveActiveProfiles.iterator(); it.hasNext(); )
 70  
             {
 71  0
                 String profileId = (String) it.next();
 72  
 
 73  0
                 if ( !dominantActiveProfiles.contains( profileId ) )
 74  
                 {
 75  0
                     dominantActiveProfiles.add( profileId );
 76  
 
 77  0
                     dominant.getRuntimeInfo().setActiveProfileSourceLevel( profileId, recessiveSourceLevel );
 78  
                 }
 79  0
             }
 80  
         }
 81  
 
 82  4
         List dominantPluginGroupIds = dominant.getPluginGroups();
 83  4
         List recessivePluginGroupIds = recessive.getPluginGroups();
 84  
 
 85  4
         if ( recessivePluginGroupIds != null )
 86  
         {
 87  4
             if ( dominantPluginGroupIds == null )
 88  
             {
 89  0
                 dominantPluginGroupIds = new ArrayList();
 90  0
                 dominant.setPluginGroups( dominantPluginGroupIds );
 91  
             }
 92  
 
 93  4
             for ( Iterator it = recessivePluginGroupIds.iterator(); it.hasNext(); )
 94  
             {
 95  1
                 String pluginGroupId = (String) it.next();
 96  
 
 97  1
                 if ( !dominantPluginGroupIds.contains( pluginGroupId ) )
 98  
                 {
 99  1
                     dominantPluginGroupIds.add( pluginGroupId );
 100  
 
 101  1
                     dominant.getRuntimeInfo().setPluginGroupIdSourceLevel( pluginGroupId, recessiveSourceLevel );
 102  
                 }
 103  1
             }
 104  
         }
 105  
 
 106  4
         if ( StringUtils.isEmpty( dominant.getLocalRepository() ) )
 107  
         {
 108  4
             dominant.setLocalRepository( recessive.getLocalRepository() );
 109  
 
 110  4
             dominant.getRuntimeInfo().setLocalRepositorySourceLevel( recessiveSourceLevel );
 111  
         }
 112  
 
 113  4
         shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel );
 114  4
         shallowMergeById( dominant.getServers(), recessive.getServers(), recessiveSourceLevel );
 115  4
         shallowMergeById( dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel );
 116  4
         shallowMergeById( dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel );
 117  4
     }
 118  
 
 119  
     /**
 120  
      * @param dominant
 121  
      * @param recessive
 122  
      * @param recessiveSourceLevel
 123  
      */
 124  
     private static void shallowMergeById( List dominant, List recessive, String recessiveSourceLevel )
 125  
     {
 126  16
         Map dominantById = mapById( dominant );
 127  
 
 128  16
         for ( Iterator it = recessive.iterator(); it.hasNext(); )
 129  
         {
 130  0
             IdentifiableBase identifiable = (IdentifiableBase) it.next();
 131  
 
 132  0
             if ( !dominantById.containsKey( identifiable.getId() ) )
 133  
             {
 134  0
                 identifiable.setSourceLevel( recessiveSourceLevel );
 135  
 
 136  0
                 dominant.add( identifiable );
 137  
             }
 138  0
         }
 139  16
     }
 140  
 
 141  
     /**
 142  
      * @param identifiables
 143  
      * @return a map
 144  
      */
 145  
     private static Map mapById( List identifiables )
 146  
     {
 147  16
         Map byId = new HashMap();
 148  
 
 149  16
         for ( Iterator it = identifiables.iterator(); it.hasNext(); )
 150  
         {
 151  2
             IdentifiableBase identifiable = (IdentifiableBase) it.next();
 152  
 
 153  2
             byId.put( identifiable.getId(), identifiable );
 154  2
         }
 155  
 
 156  16
         return byId;
 157  
     }
 158  
 
 159  
     /**
 160  
      * @param settingsProfile
 161  
      * @return a profile
 162  
      */
 163  
     public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile )
 164  
     {
 165  0
         org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile();
 166  
 
 167  0
         profile.setId( settingsProfile.getId() );
 168  
 
 169  0
         profile.setSource( "settings.xml" );
 170  
 
 171  0
         Activation settingsActivation = settingsProfile.getActivation();
 172  
 
 173  0
         if ( settingsActivation != null )
 174  
         {
 175  0
             org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
 176  
 
 177  0
             activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
 178  
 
 179  0
             activation.setJdk( settingsActivation.getJdk() );
 180  
 
 181  0
             ActivationProperty settingsProp = settingsActivation.getProperty();
 182  
 
 183  0
             if ( settingsProp != null )
 184  
             {
 185  0
                 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty();
 186  
 
 187  0
                 prop.setName( settingsProp.getName() );
 188  0
                 prop.setValue( settingsProp.getValue() );
 189  
 
 190  0
                 activation.setProperty( prop );
 191  
             }
 192  
 
 193  0
             ActivationOS settingsOs = settingsActivation.getOs();
 194  
 
 195  0
             if ( settingsOs != null )
 196  
             {
 197  0
                 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS();
 198  
 
 199  0
                 os.setArch( settingsOs.getArch() );
 200  0
                 os.setFamily( settingsOs.getFamily() );
 201  0
                 os.setName( settingsOs.getName() );
 202  0
                 os.setVersion( settingsOs.getVersion() );
 203  
 
 204  0
                 activation.setOs( os );
 205  
             }
 206  
 
 207  0
             org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile();
 208  
 
 209  0
             if ( settingsFile != null )
 210  
             {
 211  0
                 ActivationFile file = new ActivationFile();
 212  
 
 213  0
                 file.setExists( settingsFile.getExists() );
 214  0
                 file.setMissing( settingsFile.getMissing() );
 215  
 
 216  0
                 activation.setFile( file );
 217  
             }
 218  
 
 219  0
             profile.setActivation( activation );
 220  
         }
 221  
 
 222  0
         profile.setProperties( settingsProfile.getProperties() );
 223  
 
 224  0
         List repos = settingsProfile.getRepositories();
 225  0
         if ( repos != null )
 226  
         {
 227  0
             for ( Iterator it = repos.iterator(); it.hasNext(); )
 228  
             {
 229  0
                 profile.addRepository( convertFromSettingsRepository( (Repository) it.next() ) );
 230  
             }
 231  
         }
 232  
 
 233  0
         List pluginRepos = settingsProfile.getPluginRepositories();
 234  0
         if ( pluginRepos != null )
 235  
         {
 236  0
             for ( Iterator it = pluginRepos.iterator(); it.hasNext(); )
 237  
             {
 238  0
                 profile.addPluginRepository( convertFromSettingsRepository( (Repository) it.next() ) );
 239  
             }
 240  
         }
 241  
 
 242  0
         return profile;
 243  
     }
 244  
 
 245  
     /**
 246  
      * @param settingsRepo
 247  
      * @return a repository
 248  
      */
 249  
     private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo )
 250  
     {
 251  0
         org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
 252  
 
 253  0
         repo.setId( settingsRepo.getId() );
 254  0
         repo.setLayout( settingsRepo.getLayout() );
 255  0
         repo.setName( settingsRepo.getName() );
 256  0
         repo.setUrl( settingsRepo.getUrl() );
 257  
 
 258  0
         if ( settingsRepo.getSnapshots() != null )
 259  
         {
 260  0
             repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) );
 261  
         }
 262  0
         if ( settingsRepo.getReleases() != null )
 263  
         {
 264  0
             repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) );
 265  
         }
 266  
 
 267  0
         return repo;
 268  
     }
 269  
 
 270  
     /**
 271  
      * @param settingsPolicy
 272  
      * @return a RepositoryPolicy
 273  
      */
 274  
     private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy )
 275  
     {
 276  0
         org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy();
 277  0
         policy.setEnabled( settingsPolicy.isEnabled() );
 278  0
         policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() );
 279  0
         policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() );
 280  0
         return policy;
 281  
     }
 282  
 
 283  
     /**
 284  
      * @param settings could be null
 285  
      * @return a new instance of settings or null if settings was null.
 286  
      */
 287  
     public static Settings copySettings( Settings settings )
 288  
     {
 289  0
         if ( settings == null )
 290  
         {
 291  0
             return null;
 292  
         }
 293  
 
 294  0
         Settings clone = new Settings();
 295  0
         clone.setActiveProfiles( settings.getActiveProfiles() );
 296  0
         clone.setInteractiveMode( settings.isInteractiveMode() );
 297  0
         clone.setLocalRepository( settings.getLocalRepository() );
 298  0
         clone.setMirrors( settings.getMirrors() );
 299  0
         clone.setModelEncoding( settings.getModelEncoding() );
 300  0
         clone.setOffline( settings.isOffline() );
 301  0
         clone.setPluginGroups( settings.getPluginGroups() );
 302  0
         clone.setProfiles( settings.getProfiles() );
 303  0
         clone.setProxies( settings.getProxies() );
 304  0
         clone.setRuntimeInfo( settings.getRuntimeInfo() );
 305  0
         clone.setServers( settings.getServers() );
 306  0
         clone.setSourceLevel( settings.getSourceLevel() );
 307  0
         clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
 308  
 
 309  0
         return clone;
 310  
     }
 311  
 }