Coverage Report - org.apache.maven.profiles.DefaultProfileManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultProfileManager
0%
0/119
0%
0/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  0
     private List activatedIds = new ArrayList();
 46  
 
 47  0
     private List deactivatedIds = new ArrayList();
 48  
 
 49  0
     private List defaultIds = new ArrayList();
 50  
 
 51  0
     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  0
         this( container, (Settings)null);
 62  0
     }
 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  0
         this( container, (Settings)null, props );
 72  
 
 73  0
     }
 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  0
     {
 81  0
         this.container = container;
 82  
 
 83  0
         loadSettingsProfiles( settings );
 84  0
     }
 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  0
     {
 93  0
         this.container = container;
 94  
 
 95  0
         loadSettingsProfiles( settings );
 96  
 
 97  0
         if ( props != null )
 98  
         {
 99  0
             requestProperties = props;
 100  
         }
 101  0
     }
 102  
 
 103  
     public Properties getRequestProperties() {
 104  0
         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  0
         String profileId = profile.getId();
 118  
 
 119  0
         Profile existing = (Profile) profilesById.get( profileId );
 120  0
         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  0
         profilesById.put( profile.getId(), profile );
 127  
 
 128  0
         Activation activation = profile.getActivation();
 129  
 
 130  0
         if ( activation != null && activation.isActiveByDefault() )
 131  
         {
 132  0
             activateAsDefault( profileId );
 133  
         }
 134  0
     }
 135  
 
 136  
     /* (non-Javadoc)
 137  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
 138  
     */
 139  
     public void explicitlyActivate( String profileId )
 140  
     {
 141  0
         if ( !activatedIds.contains( profileId ) )
 142  
         {
 143  0
             container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
 144  
 
 145  0
             activatedIds.add( profileId );
 146  
         }
 147  0
     }
 148  
 
 149  
     /* (non-Javadoc)
 150  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
 151  
     */
 152  
     public void explicitlyActivate( List profileIds )
 153  
     {
 154  0
         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
 155  
         {
 156  0
             String profileId = (String) it.next();
 157  
 
 158  0
             explicitlyActivate( profileId );
 159  0
         }
 160  0
     }
 161  
 
 162  
     /* (non-Javadoc)
 163  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
 164  
     */
 165  
     public void explicitlyDeactivate( String profileId )
 166  
     {
 167  0
         if ( !deactivatedIds.contains( profileId ) )
 168  
         {
 169  0
             container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
 170  
 
 171  0
             deactivatedIds.add( profileId );
 172  
         }
 173  0
     }
 174  
 
 175  
     /* (non-Javadoc)
 176  
     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
 177  
     */
 178  
     public void explicitlyDeactivate( List profileIds )
 179  
     {
 180  0
         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
 181  
         {
 182  0
             String profileId = (String) it.next();
 183  
 
 184  0
             explicitlyDeactivate( profileId );
 185  0
         }
 186  0
     }
 187  
 
 188  
     /* (non-Javadoc)
 189  
     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
 190  
     */
 191  
     public List getActiveProfiles()
 192  
         throws ProfileActivationException
 193  
     {
 194  0
         List activeFromPom = new ArrayList();
 195  0
         List activeExternal = new ArrayList();
 196  
 
 197  0
         for ( Iterator it = profilesById.entrySet().iterator(); it.hasNext(); )
 198  
         {
 199  0
             Map.Entry entry = (Entry) it.next();
 200  
 
 201  0
             String profileId = (String) entry.getKey();
 202  0
             Profile profile = (Profile) entry.getValue();
 203  
 
 204  0
             boolean shouldAdd = false;
 205  0
             if ( activatedIds.contains( profileId ) )
 206  
             {
 207  0
                 shouldAdd = true;
 208  
             }
 209  0
             else if ( isActive( profile ) )
 210  
             {
 211  0
                 shouldAdd = true;
 212  
             }
 213  
 
 214  0
             if ( !deactivatedIds.contains( profileId ) && shouldAdd )
 215  
             {
 216  0
                 if ( "pom".equals( profile.getSource() ) )
 217  
                 {
 218  0
                     activeFromPom.add( profile );
 219  
                 }
 220  
                 else
 221  
                 {
 222  0
                     activeExternal.add( profile );
 223  
                 }
 224  
             }
 225  0
         }
 226  
 
 227  0
         if ( activeFromPom.isEmpty() )
 228  
         {
 229  0
             for ( Iterator it = defaultIds.iterator(); it.hasNext(); )
 230  
             {
 231  0
                 String profileId = (String) it.next();
 232  
 
 233  0
                 if ( deactivatedIds.contains( profileId ) )
 234  
                 {
 235  0
                     continue;
 236  
                 }
 237  
                 
 238  0
                 Profile profile = (Profile) profilesById.get( profileId );
 239  
 
 240  0
                 activeFromPom.add( profile );
 241  0
             }
 242  
         }
 243  
 
 244  0
         List allActive = new ArrayList( activeFromPom.size() + activeExternal.size() );
 245  
 
 246  0
         allActive.addAll( activeExternal );
 247  0
         allActive.addAll( activeFromPom );
 248  
 
 249  0
         return allActive;
 250  
     }
 251  
 
 252  
     private boolean isActive( Profile profile )
 253  
         throws ProfileActivationException
 254  
     {
 255  0
         List activators = null;
 256  0
         Properties systemProperties = new Properties( System.getProperties() );
 257  0
         if ( requestProperties != null )
 258  
         {
 259  0
             systemProperties.putAll( requestProperties );
 260  
         }
 261  
 
 262  0
         container.addContextValue("SystemProperties", systemProperties);
 263  
         try
 264  
         {
 265  0
             activators = container.lookupList( ProfileActivator.ROLE );
 266  
 
 267  0
             for ( Iterator activatorIterator = activators.iterator(); activatorIterator.hasNext(); )
 268  
             {
 269  0
                 ProfileActivator activator = (ProfileActivator) activatorIterator.next();
 270  
 
 271  0
                 if ( activator.canDetermineActivation( profile ) )
 272  
                 {
 273  0
                     if ( activator.isActive( profile ) )
 274  
                     {
 275  0
                         return true;
 276  
                     }
 277  
                 }
 278  0
             }
 279  
 
 280  0
             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  0
             container.getContext().put("SystemProperties", null);
 289  0
             if ( activators != null )
 290  
             {
 291  
                 try
 292  
                 {
 293  0
                     container.releaseAll( activators );
 294  
                 }
 295  0
                 catch ( ComponentLifecycleException e )
 296  
                 {
 297  0
                     container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
 298  0
                 }
 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  0
         for ( Iterator it = profiles.iterator(); it.hasNext(); )
 309  
         {
 310  0
             Profile profile = (Profile) it.next();
 311  
 
 312  0
             addProfile( profile );
 313  0
         }
 314  0
     }
 315  
 
 316  
     public void activateAsDefault( String profileId )
 317  
     {
 318  0
         if ( !defaultIds.contains( profileId ) )
 319  
         {
 320  0
             defaultIds.add( profileId );
 321  
         }
 322  0
     }
 323  
 
 324  
     public List getExplicitlyActivatedIds()
 325  
     {
 326  0
         return activatedIds;
 327  
     }
 328  
 
 329  
     public List getExplicitlyDeactivatedIds()
 330  
     {
 331  0
         return deactivatedIds;
 332  
     }
 333  
 
 334  
     public List getIdsActivatedByDefault()
 335  
     {
 336  0
         return defaultIds;
 337  
     }
 338  
 
 339  
     public void loadSettingsProfiles( Settings settings )
 340  
     {
 341  0
         if ( settings == null )
 342  
         {
 343  0
             return;
 344  
         }
 345  
 
 346  0
         List settingsProfiles = settings.getProfiles();
 347  
 
 348  0
         List settingsActiveProfileIds = settings.getActiveProfiles();
 349  
 
 350  0
         explicitlyActivate( settingsActiveProfileIds );
 351  
 
 352  0
         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  0
     }
 364  
 }