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