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      @Inject
51      private List<ProfileActivator> activators = new ArrayList<>();
52  
53      public DefaultProfileSelector addProfileActivator( ProfileActivator profileActivator )
54      {
55          if ( profileActivator != null )
56          {
57              activators.add( profileActivator );
58          }
59          return this;
60      }
61  
62      @Override
63      public List<Profile> getActiveProfiles( Collection<Profile> profiles, ProfileActivationContext context,
64                                              ModelProblemCollector problems )
65      {
66          Collection<String> activatedIds = new HashSet<>( context.getActiveProfileIds() );
67          Collection<String> deactivatedIds = new HashSet<>( context.getInactiveProfileIds() );
68  
69          List<Profile> activeProfiles = new ArrayList<>( profiles.size() );
70          List<Profile> activePomProfilesByDefault = new ArrayList<>();
71          boolean activatedPomProfileNotByDefault = false;
72  
73          for ( Profile profile : profiles )
74          {
75              if ( !deactivatedIds.contains( profile.getId() ) )
76              {
77                  if ( activatedIds.contains( profile.getId() ) || isActive( profile, context, problems ) )
78                  {
79                      activeProfiles.add( profile );
80  
81                      if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
82                      {
83                          activatedPomProfileNotByDefault = true;
84                      }
85                  }
86                  else if ( isActiveByDefault( profile ) )
87                  {
88                      if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
89                      {
90                          activePomProfilesByDefault.add( profile );
91                      }
92                      else
93                      {
94                          activeProfiles.add( profile );
95                      }
96                  }
97  
98              }
99          }
100 
101         if ( !activatedPomProfileNotByDefault )
102         {
103             activeProfiles.addAll( activePomProfilesByDefault );
104         }
105 
106         return activeProfiles;
107     }
108 
109     private boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
110     {
111         boolean isActive = false;
112         for ( ProfileActivator activator : activators )
113         {
114             if ( activator.presentInConfig( profile, context, problems ) )
115             {
116                 isActive = true;
117             }
118         }
119         for ( ProfileActivator activator : activators )
120         {
121             try
122             {
123                 if ( activator.presentInConfig( profile, context, problems ) )
124                 {
125                     isActive &=  activator.isActive( profile, context, problems );
126                 }
127             }
128             catch ( RuntimeException e )
129             {
130                 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
131                         .setMessage( "Failed to determine activation for profile " + profile.getId() )
132                         .setLocation( profile.getLocation( "" ) )
133                         .setException( e ) );
134                 return false;
135             }
136         }
137         return isActive;
138     }
139 
140     private boolean isActiveByDefault( Profile profile )
141     {
142         Activation activation = profile.getActivation();
143         return activation != null && activation.isActiveByDefault();
144     }
145 
146 }