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   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
42   */
43  @Named
44  @Singleton
45  @Deprecated(since = "4.0.0")
46  public class DefaultProfileSelector implements ProfileSelector {
47  
48      @Inject
49      private List<ProfileActivator> activators = new ArrayList<>();
50  
51      public DefaultProfileSelector addProfileActivator(ProfileActivator profileActivator) {
52          if (profileActivator != null) {
53              activators.add(profileActivator);
54          }
55          return this;
56      }
57  
58      @Override
59      public List<Profile> getActiveProfiles(
60              Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {
61          Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
62          Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());
63  
64          List<Profile> activeProfiles = new ArrayList<>(profiles.size());
65          List<Profile> activePomProfilesByDefault = new ArrayList<>();
66          boolean activatedPomProfileNotByDefault = false;
67  
68          for (Profile profile : profiles) {
69              if (!deactivatedIds.contains(profile.getId())) {
70                  if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
71                      activeProfiles.add(profile);
72  
73                      if (Profile.SOURCE_POM.equals(profile.getSource())) {
74                          activatedPomProfileNotByDefault = true;
75                      }
76                  } else if (isActiveByDefault(profile)) {
77                      if (Profile.SOURCE_POM.equals(profile.getSource())) {
78                          activePomProfilesByDefault.add(profile);
79                      } else {
80                          activeProfiles.add(profile);
81                      }
82                  }
83              }
84          }
85  
86          if (!activatedPomProfileNotByDefault) {
87              activeProfiles.addAll(activePomProfilesByDefault);
88          }
89  
90          return activeProfiles;
91      }
92  
93      private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
94          boolean isActive = false;
95          for (ProfileActivator activator : activators) {
96              if (activator.presentInConfig(profile, context, problems)) {
97                  isActive = true;
98              }
99          }
100         for (ProfileActivator activator : activators) {
101             try {
102                 if (activator.presentInConfig(profile, context, problems)) {
103                     isActive &= activator.isActive(profile, context, problems);
104                 }
105             } catch (RuntimeException e) {
106                 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
107                         .setMessage(
108                                 "Failed to determine activation for profile " + profile.getId() + ": " + e.getMessage())
109                         .setLocation(profile.getLocation(""))
110                         .setException(e));
111                 return false;
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 }