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