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