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.profile.activation.ProfileActivator;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  
35  /**
36   * Calculates the active profiles among a given collection of profiles.
37   * 
38   * @author Benjamin Bentmann
39   */
40  @Component( role = ProfileSelector.class )
41  public class DefaultProfileSelector
42      implements ProfileSelector
43  {
44  
45      @Requirement( role = ProfileActivator.class )
46      private List<ProfileActivator> activators = new ArrayList<ProfileActivator>();
47  
48      public DefaultProfileSelector addProfileActivator( ProfileActivator profileActivator )
49      {
50          if ( profileActivator != null )
51          {
52              activators.add( profileActivator );
53          }
54          return this;
55      }
56  
57      public List<Profile> getActiveProfiles( Collection<Profile> profiles, ProfileActivationContext context,
58                                              ModelProblemCollector problems )
59      {
60          Collection<String> activatedIds = new HashSet<String>( context.getActiveProfileIds() );
61          Collection<String> deactivatedIds = new HashSet<String>( context.getInactiveProfileIds() );
62  
63          List<Profile> activeProfiles = new ArrayList<Profile>( profiles.size() );
64          List<Profile> activePomProfilesByDefault = new ArrayList<Profile>();
65          boolean activatedPomProfileNotByDefault = false;
66  
67          for ( Profile profile : profiles )
68          {
69              if ( !deactivatedIds.contains( profile.getId() ) )
70              {
71                  if ( activatedIds.contains( profile.getId() ) || isActive( profile, context, problems ) )
72                  {
73                      activeProfiles.add( profile );
74  
75                      if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
76                      {
77                          activatedPomProfileNotByDefault = true;
78                      }
79                  }
80                  else if ( isActiveByDefault( profile ) )
81                  {
82                      if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
83                      {
84                          activePomProfilesByDefault.add( profile );
85                      }
86                      else
87                      {
88                          activeProfiles.add( profile );
89                      }
90                  }
91  
92              }
93          }
94  
95          if ( !activatedPomProfileNotByDefault )
96          {
97              activeProfiles.addAll( activePomProfilesByDefault );
98          }
99  
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 }