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