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