001    package org.apache.maven.model.profile;
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    
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.HashSet;
025    import java.util.List;
026    
027    import org.apache.maven.model.Activation;
028    import org.apache.maven.model.Profile;
029    import org.apache.maven.model.building.ModelProblem;
030    import org.apache.maven.model.building.ModelProblemCollector;
031    import org.apache.maven.model.building.ModelProblem.Severity;
032    import org.apache.maven.model.building.ModelProblem.Version;
033    import org.apache.maven.model.building.ModelProblemCollectorRequest;
034    import org.apache.maven.model.profile.activation.ProfileActivator;
035    import org.codehaus.plexus.component.annotations.Component;
036    import org.codehaus.plexus.component.annotations.Requirement;
037    
038    /**
039     * Calculates the active profiles among a given collection of profiles.
040     * 
041     * @author Benjamin Bentmann
042     */
043    @Component( role = ProfileSelector.class )
044    public class DefaultProfileSelector
045        implements ProfileSelector
046    {
047    
048        @Requirement( role = ProfileActivator.class )
049        private List<ProfileActivator> activators = new ArrayList<ProfileActivator>();
050    
051        public DefaultProfileSelector addProfileActivator( ProfileActivator profileActivator )
052        {
053            if ( profileActivator != null )
054            {
055                activators.add( profileActivator );
056            }
057            return this;
058        }
059    
060        public List<Profile> getActiveProfiles( Collection<Profile> profiles, ProfileActivationContext context,
061                                                ModelProblemCollector problems )
062        {
063            Collection<String> activatedIds = new HashSet<String>( context.getActiveProfileIds() );
064            Collection<String> deactivatedIds = new HashSet<String>( context.getInactiveProfileIds() );
065    
066            List<Profile> activeProfiles = new ArrayList<Profile>( profiles.size() );
067            List<Profile> activePomProfilesByDefault = new ArrayList<Profile>();
068            boolean activatedPomProfileNotByDefault = false;
069    
070            for ( Profile profile : profiles )
071            {
072                if ( !deactivatedIds.contains( profile.getId() ) )
073                {
074                    if ( activatedIds.contains( profile.getId() ) || isActive( profile, context, problems ) )
075                    {
076                        activeProfiles.add( profile );
077    
078                        if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
079                        {
080                            activatedPomProfileNotByDefault = true;
081                        }
082                    }
083                    else if ( isActiveByDefault( profile ) )
084                    {
085                        if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
086                        {
087                            activePomProfilesByDefault.add( profile );
088                        }
089                        else
090                        {
091                            activeProfiles.add( profile );
092                        }
093                    }
094    
095                }
096            }
097    
098            if ( !activatedPomProfileNotByDefault )
099            {
100                activeProfiles.addAll( activePomProfilesByDefault );
101            }
102    
103            return activeProfiles;
104        }
105    
106        private boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
107        {
108            for ( ProfileActivator activator : activators )
109            {
110                try
111                {
112                    if ( activator.isActive( profile, context, problems ) )
113                    {
114                        return true;
115                    }
116                }
117                catch ( RuntimeException e )
118                {
119                    problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE)
120                            .setMessage( "Failed to determine activation for profile " + profile.getId())
121                            .setLocation( profile.getLocation( "" ))
122                            .setException( e ));
123                    return false;
124                }
125            }
126            return false;
127        }
128    
129        private boolean isActiveByDefault( Profile profile )
130        {
131            Activation activation = profile.getActivation();
132            return activation != null && activation.isActiveByDefault();
133        }
134    
135    }