Coverage Report - org.apache.maven.profiles.DefaultProfileManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultProfileManager
86 %
102/119
79 %
44/56
2,632
 
 1  
 package org.apache.maven.profiles;
 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.Activation;
 23  
 import org.apache.maven.model.Profile;
 24  
 import org.apache.maven.profiles.activation.ProfileActivationException;
 25  
 import org.apache.maven.profiles.activation.ProfileActivator;
 26  
 import org.apache.maven.settings.Settings;
 27  
 import org.apache.maven.settings.SettingsUtils;
 28  
 import org.codehaus.plexus.PlexusContainer;
 29  
 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
 30  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 31  
 
 32  
 import java.util.ArrayList;
 33  
 import java.util.Iterator;
 34  
 import java.util.LinkedHashMap;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 import java.util.Properties;
 38  
 import java.util.Map.Entry;
 39  
 
 40  
 public class DefaultProfileManager
 41  
     implements ProfileManager
 42  
 {
 43  
     private PlexusContainer container;
 44  
 
 45  283
     private List activatedIds = new ArrayList();
 46  
 
 47  283
     private List deactivatedIds = new ArrayList();
 48  
 
 49  283
     private List defaultIds = new ArrayList();
 50  
 
 51  283
     private Map profilesById = new LinkedHashMap();
 52  
 
 53  
     private Properties requestProperties;
 54  
 
 55  
     /**
 56  
      * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work correctly
 57  
      * in embedded envirnments.
 58  
      */
 59  
     public DefaultProfileManager( PlexusContainer container )
 60  
     {
 61  204
         this( container, (Settings)null);
 62  204
     }
 63  
 
 64  
     /**
 65  
      * the properties passed to the profile manager are the props that
 66  
      * are passed to maven, possibly containing profile activator properties
 67  
      *
 68  
      */
 69  
     public DefaultProfileManager( PlexusContainer container, Properties props )
 70  
     {
 71  78
         this( container, (Settings)null, props );
 72  
 
 73  78
     }
 74  
 
 75  
     /**
 76  
      * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work correctly
 77  
      * in embedded envirnments.
 78  
      */
 79  
     public DefaultProfileManager( PlexusContainer container, Settings settings )
 80  205
     {
 81  205
         this.container = container;
 82  
 
 83  205
         loadSettingsProfiles( settings );
 84  205
     }
 85  
 
 86  
     /**
 87  
      * the properties passed to the profile manager are the props that
 88  
      * are passed to maven, possibly containing profile activator properties
 89  
      *
 90  
      */
 91  
     public DefaultProfileManager( PlexusContainer container, Settings settings, Properties props )
 92  78
     {
 93  78
         this.container = container;
 94  
 
 95  78
         loadSettingsProfiles( settings );
 96  
 
 97  78
         if ( props != null )
 98  
         {
 99  2
             requestProperties = props;
 100  
         }
 101  78
     }
 102  
 
 103  
     public Properties getRequestProperties() {
 104  76
         return requestProperties;
 105  
     }
 106  
 
 107  
     public Map getProfilesById()
 108  
     {
 109  0
         return profilesById;
 110  
     }
 111  
 
 112  
     /* (non-Javadoc)
 113  
     * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
 114  
     */
 115  
     public void addProfile( Profile profile )
 116  
     {
 117  121
         String profileId = profile.getId();
 118  
 
 119  121
         Profile existing = (Profile) profilesById.get( profileId );
 120  121
         if ( existing != null )
 121  
         {
 122  0
             container.getLogger().warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource() +
 123  
                 ") with new instance from source: " + profile.getSource() );
 124  
         }
 125  
 
 126  121
         profilesById.put( profile.getId(), profile );
 127  
 
 128  121
         Activation activation = profile.getActivation();
 129  
 
 130  121
         if ( activation != null && activation.isActiveByDefault() )
 131  
         {
 132  2
             activateAsDefault( profileId );
 133  
         }
 134  121
     }
 135  
 
 136  
     /* (non-Javadoc)
 137  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
 138  
     */
 139  
     public void explicitlyActivate( String profileId )
 140  
     {
 141  4
         if ( !activatedIds.contains( profileId ) )
 142  
         {
 143  4
             container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
 144  
 
 145  4
             activatedIds.add( profileId );
 146  
         }
 147  4
     }
 148  
 
 149  
     /* (non-Javadoc)
 150  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
 151  
     */
 152  
     public void explicitlyActivate( List profileIds )
 153  
     {
 154  55
         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
 155  
         {
 156  1
             String profileId = (String) it.next();
 157  
 
 158  1
             explicitlyActivate( profileId );
 159  1
         }
 160  55
     }
 161  
 
 162  
     /* (non-Javadoc)
 163  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
 164  
     */
 165  
     public void explicitlyDeactivate( String profileId )
 166  
     {
 167  1
         if ( !deactivatedIds.contains( profileId ) )
 168  
         {
 169  1
             container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
 170  
 
 171  1
             deactivatedIds.add( profileId );
 172  
         }
 173  1
     }
 174  
 
 175  
     /* (non-Javadoc)
 176  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
 177  
     */
 178  
     public void explicitlyDeactivate( List profileIds )
 179  
     {
 180  54
         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
 181  
         {
 182  0
             String profileId = (String) it.next();
 183  
 
 184  0
             explicitlyDeactivate( profileId );
 185  0
         }
 186  54
     }
 187  
 
 188  
     /* (non-Javadoc)
 189  
     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
 190  
     */
 191  
     public List getActiveProfiles()
 192  
         throws ProfileActivationException
 193  
     {
 194  308
         List activeFromPom = new ArrayList();
 195  308
         List activeExternal = new ArrayList();
 196  
 
 197  308
         for ( Iterator it = profilesById.entrySet().iterator(); it.hasNext(); )
 198  
         {
 199  125
             Map.Entry entry = (Entry) it.next();
 200  
 
 201  125
             String profileId = (String) entry.getKey();
 202  125
             Profile profile = (Profile) entry.getValue();
 203  
 
 204  125
             boolean shouldAdd = false;
 205  125
             if ( activatedIds.contains( profileId ) )
 206  
             {
 207  6
                 shouldAdd = true;
 208  
             }
 209  119
             else if ( isActive( profile ) )
 210  
             {
 211  4
                 shouldAdd = true;
 212  
             }
 213  
 
 214  125
             if ( !deactivatedIds.contains( profileId ) && shouldAdd )
 215  
             {
 216  9
                 if ( "pom".equals( profile.getSource() ) )
 217  
                 {
 218  9
                     activeFromPom.add( profile );
 219  
                 }
 220  
                 else
 221  
                 {
 222  0
                     activeExternal.add( profile );
 223  
                 }
 224  
             }
 225  125
         }
 226  
 
 227  308
         if ( activeFromPom.isEmpty() )
 228  
         {
 229  299
             for ( Iterator it = defaultIds.iterator(); it.hasNext(); )
 230  
             {
 231  1
                 String profileId = (String) it.next();
 232  
 
 233  1
                 if ( deactivatedIds.contains( profileId ) )
 234  
                 {
 235  0
                     continue;
 236  
                 }
 237  
                 
 238  1
                 Profile profile = (Profile) profilesById.get( profileId );
 239  
 
 240  1
                 activeFromPom.add( profile );
 241  1
             }
 242  
         }
 243  
 
 244  308
         List allActive = new ArrayList( activeFromPom.size() + activeExternal.size() );
 245  
 
 246  308
         allActive.addAll( activeExternal );
 247  308
         allActive.addAll( activeFromPom );
 248  
 
 249  308
         return allActive;
 250  
     }
 251  
 
 252  
     private boolean isActive( Profile profile )
 253  
         throws ProfileActivationException
 254  
     {
 255  119
         List activators = null;
 256  119
         Properties systemProperties = new Properties( System.getProperties() );
 257  119
         if ( requestProperties != null )
 258  
         {
 259  4
             systemProperties.putAll( requestProperties );
 260  
         }
 261  
 
 262  119
         container.addContextValue("SystemProperties", systemProperties);
 263  
         try
 264  
         {
 265  119
             activators = container.lookupList( ProfileActivator.ROLE );
 266  
 
 267  119
             for ( Iterator activatorIterator = activators.iterator(); activatorIterator.hasNext(); )
 268  
             {
 269  587
                 ProfileActivator activator = (ProfileActivator) activatorIterator.next();
 270  
 
 271  587
                 if ( activator.canDetermineActivation( profile ) )
 272  
                 {
 273  117
                     if ( activator.isActive( profile ) )
 274  
                     {
 275  4
                         return true;
 276  
                     }
 277  
                 }
 278  583
             }
 279  
 
 280  115
             return false;
 281  
         }
 282  0
         catch ( ComponentLookupException e )
 283  
         {
 284  0
             throw new ProfileActivationException( "Cannot retrieve list of profile activators.", e );
 285  
         }
 286  
         finally
 287  
         {
 288  119
             container.getContext().put("SystemProperties", null);
 289  119
             if ( activators != null )
 290  
             {
 291  
                 try
 292  
                 {
 293  119
                     container.releaseAll( activators );
 294  
                 }
 295  0
                 catch ( ComponentLifecycleException e )
 296  
                 {
 297  0
                     container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
 298  238
                 }
 299  
             }
 300  
         }
 301  
     }
 302  
 
 303  
     /* (non-Javadoc)
 304  
      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
 305  
      */
 306  
     public void addProfiles( List profiles )
 307  
     {
 308  253
         for ( Iterator it = profiles.iterator(); it.hasNext(); )
 309  
         {
 310  109
             Profile profile = (Profile) it.next();
 311  
 
 312  109
             addProfile( profile );
 313  109
         }
 314  253
     }
 315  
 
 316  
     public void activateAsDefault( String profileId )
 317  
     {
 318  2
         if ( !defaultIds.contains( profileId ) )
 319  
         {
 320  2
             defaultIds.add( profileId );
 321  
         }
 322  2
     }
 323  
 
 324  
     public List getExplicitlyActivatedIds()
 325  
     {
 326  55
         return activatedIds;
 327  
     }
 328  
 
 329  
     public List getExplicitlyDeactivatedIds()
 330  
     {
 331  54
         return deactivatedIds;
 332  
     }
 333  
 
 334  
     public List getIdsActivatedByDefault()
 335  
     {
 336  0
         return defaultIds;
 337  
     }
 338  
 
 339  
     public void loadSettingsProfiles( Settings settings )
 340  
     {
 341  283
         if ( settings == null )
 342  
         {
 343  282
             return;
 344  
         }
 345  
 
 346  1
         List settingsProfiles = settings.getProfiles();
 347  
 
 348  1
         List settingsActiveProfileIds = settings.getActiveProfiles();
 349  
 
 350  1
         explicitlyActivate( settingsActiveProfileIds );
 351  
 
 352  1
         if ( settingsProfiles != null && !settingsProfiles.isEmpty() )
 353  
         {
 354  0
             for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
 355  
             {
 356  0
                 org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next();
 357  
 
 358  0
                 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
 359  
 
 360  0
                 addProfile( profile );
 361  0
             }
 362  
         }
 363  1
     }
 364  
 }