View Javadoc
1   package org.apache.maven.model.profile;
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 java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.List;
26  
27  import javax.inject.Inject;
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.apache.maven.model.Activation;
32  import org.apache.maven.model.Profile;
33  import org.apache.maven.model.building.ModelProblemCollector;
34  import org.apache.maven.model.building.ModelProblem.Severity;
35  import org.apache.maven.model.building.ModelProblem.Version;
36  import org.apache.maven.model.building.ModelProblemCollectorRequest;
37  import org.apache.maven.model.profile.activation.ProfileActivator;
38  
39  /**
40   * Calculates the active profiles among a given collection of profiles.
41   *
42   * @author Benjamin Bentmann
43   */
44  @Named
45  @Singleton
46  public class DefaultProfileSelector
47      implements ProfileSelector
48  {
49  
50      private final List<ProfileActivator> activators;
51  
52      @Inject
53      public DefaultProfileSelector( List<ProfileActivator> activators )
54      {
55          this.activators = activators;
56      }
57  
58      public DefaultProfileSelector addProfileActivator( ProfileActivator profileActivator )
59      {
60          if ( profileActivator != null )
61          {
62              activators.add( profileActivator );
63          }
64          return this;
65      }
66  
67      @Override
68      public List<Profile> getActiveProfiles( Collection<Profile> profiles, ProfileActivationContext context,
69                                              ModelProblemCollector problems )
70      {
71          Collection<String> activatedIds = new HashSet<>( context.getActiveProfileIds() );
72          Collection<String> deactivatedIds = new HashSet<>( context.getInactiveProfileIds() );
73  
74          List<Profile> activeProfiles = new ArrayList<>( profiles.size() );
75          List<Profile> activePomProfilesByDefault = new ArrayList<>();
76          boolean activatedPomProfileNotByDefault = false;
77  
78          for ( Profile profile : profiles )
79          {
80              if ( !deactivatedIds.contains( profile.getId() ) )
81              {
82                  if ( activatedIds.contains( profile.getId() ) || isActive( profile, context, problems ) )
83                  {
84                      activeProfiles.add( profile );
85  
86                      if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
87                      {
88                          activatedPomProfileNotByDefault = true;
89                      }
90                  }
91                  else if ( isActiveByDefault( profile ) )
92                  {
93                      if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
94                      {
95                          activePomProfilesByDefault.add( profile );
96                      }
97                      else
98                      {
99                          activeProfiles.add( profile );
100                     }
101                 }
102 
103             }
104         }
105 
106         if ( !activatedPomProfileNotByDefault )
107         {
108             activeProfiles.addAll( activePomProfilesByDefault );
109         }
110 
111         return activeProfiles;
112     }
113 
114     private boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
115     {
116         boolean isActive = false;
117         for ( ProfileActivator activator : activators )
118         {
119             if ( activator.presentInConfig( profile, context, problems ) )
120             {
121                 isActive = true;
122             }
123         }
124         for ( ProfileActivator activator : activators )
125         {
126             try
127             {
128                 if ( activator.presentInConfig( profile, context, problems ) )
129                 {
130                     isActive &=  activator.isActive( profile, context, problems );
131                 }
132             }
133             catch ( RuntimeException e )
134             {
135                 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
136                         .setMessage( "Failed to determine activation for profile " + profile.getId() )
137                         .setLocation( profile.getLocation( "" ) )
138                         .setException( e ) );
139                 return false;
140             }
141         }
142         return isActive;
143     }
144 
145     private boolean isActiveByDefault( Profile profile )
146     {
147         Activation activation = profile.getActivation();
148         return activation != null && activation.isActiveByDefault();
149     }
150 
151 }