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.Collections;
28  import java.util.HashSet;
29  import java.util.List;
30  
31  import org.apache.maven.model.Activation;
32  import org.apache.maven.model.Profile;
33  import org.apache.maven.model.building.ModelProblem.Severity;
34  import org.apache.maven.model.building.ModelProblem.Version;
35  import org.apache.maven.model.building.ModelProblemCollector;
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 implements ProfileSelector {
47  
48      private final List<ProfileActivator> activators;
49  
50      public DefaultProfileSelector() {
51          this.activators = Collections.emptyList();
52      }
53  
54      @Inject
55      public DefaultProfileSelector(List<ProfileActivator> activators) {
56          this.activators = new ArrayList<>(activators);
57      }
58  
59      public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
60          if (profileActivator != null) {
61              activators.add(profileActivator);
62          }
63          return this;
64      }
65  
66      @Override
67      public List<Profile> getActiveProfiles(
68              Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {
69          Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
70          Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());
71  
72          List<Profile> activeProfiles = new ArrayList<>(profiles.size());
73          List<Profile> activePomProfilesByDefault = new ArrayList<>();
74          boolean activatedPomProfileNotByDefault = false;
75  
76          for (Profile profile : profiles) {
77              if (!deactivatedIds.contains(profile.getId())) {
78                  if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
79                      activeProfiles.add(profile);
80  
81                      if (Profile.SOURCE_POM.equals(profile.getSource())) {
82                          activatedPomProfileNotByDefault = true;
83                      }
84                  } else if (isActiveByDefault(profile)) {
85                      if (Profile.SOURCE_POM.equals(profile.getSource())) {
86                          activePomProfilesByDefault.add(profile);
87                      } else {
88                          activeProfiles.add(profile);
89                      }
90                  }
91              }
92          }
93  
94          if (!activatedPomProfileNotByDefault) {
95              activeProfiles.addAll(activePomProfilesByDefault);
96          }
97  
98          return activeProfiles;
99      }
100 
101     private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
102         boolean isActive = false;
103         for (ProfileActivator activator : activators) {
104             if (activator.presentInConfig(profile, context, problems)) {
105                 isActive = true;
106                 try {
107                     if (!activator.isActive(profile, context, problems)) {
108                         return false;
109                     }
110                 } catch (RuntimeException e) {
111                     problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
112                             .setMessage("Failed to determine activation for profile " + profile.getId())
113                             .setLocation(profile.getLocation(""))
114                             .setException(e));
115                     return false;
116                 }
117             }
118         }
119         return isActive;
120     }
121 
122     private boolean isActiveByDefault(Profile profile) {
123         Activation activation = profile.getActivation();
124         return activation != null && activation.isActiveByDefault();
125     }
126 }