View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.profile;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.HashSet;
28  import java.util.List;
29  
30  import org.apache.maven.model.Activation;
31  import org.apache.maven.model.Profile;
32  import org.apache.maven.model.building.ModelProblem.Severity;
33  import org.apache.maven.model.building.ModelProblem.Version;
34  import org.apache.maven.model.building.ModelProblemCollector;
35  import org.apache.maven.model.building.ModelProblemCollectorRequest;
36  import org.apache.maven.model.profile.activation.ProfileActivator;
37  
38  /**
39   * Calculates the active profiles among a given collection of profiles.
40   *
41   * @author Benjamin Bentmann
42   */
43  @Named
44  @Singleton
45  public class DefaultProfileSelector implements ProfileSelector {
46  
47      @Inject
48      private List<ProfileActivator> activators = new ArrayList<>();
49  
50      public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
51          if (profileActivator != null) {
52              activators.add(profileActivator);
53          }
54          return this;
55      }
56  
57      @Override
58      public List<Profile> getActiveProfiles(
59              Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {
60          Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
61          Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());
62  
63          List<Profile> activeProfiles = new ArrayList<>(profiles.size());
64          List<Profile> activePomProfilesByDefault = new ArrayList<>();
65          boolean activatedPomProfileNotByDefault = false;
66  
67          for (Profile profile : profiles) {
68              if (!deactivatedIds.contains(profile.getId())) {
69                  if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
70                      activeProfiles.add(profile);
71  
72                      if (Profile.SOURCE_POM.equals(profile.getSource())) {
73                          activatedPomProfileNotByDefault = true;
74                      }
75                  } else if (isActiveByDefault(profile)) {
76                      if (Profile.SOURCE_POM.equals(profile.getSource())) {
77                          activePomProfilesByDefault.add(profile);
78                      } else {
79                          activeProfiles.add(profile);
80                      }
81                  }
82              }
83          }
84  
85          if (!activatedPomProfileNotByDefault) {
86              activeProfiles.addAll(activePomProfilesByDefault);
87          }
88  
89          return activeProfiles;
90      }
91  
92      private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
93          boolean isActive = false;
94          for (ProfileActivator activator : activators) {
95              if (activator.presentInConfig(profile, context, problems)) {
96                  isActive = true;
97              }
98          }
99          for (ProfileActivator activator : activators) {
100             try {
101                 if (activator.presentInConfig(profile, context, problems)) {
102                     isActive &= activator.isActive(profile, context, problems);
103                 }
104             } catch (RuntimeException e) {
105                 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
106                         .setMessage("Failed to determine activation for profile " + profile.getId())
107                         .setLocation(profile.getLocation(""))
108                         .setException(e));
109                 return false;
110             }
111         }
112         return isActive;
113     }
114 
115     private boolean isActiveByDefault(Profile profile) {
116         Activation activation = profile.getActivation();
117         return activation != null && activation.isActiveByDefault();
118     }
119 }