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