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.profiles;
20  
21  import java.util.ArrayList;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  import org.apache.maven.model.Activation;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.model.building.ModelProblem;
29  import org.apache.maven.model.profile.DefaultProfileActivationContext;
30  import org.apache.maven.model.profile.ProfileSelector;
31  import org.apache.maven.profiles.activation.ProfileActivationException;
32  import org.codehaus.plexus.MutablePlexusContainer;
33  import org.codehaus.plexus.PlexusContainer;
34  import org.codehaus.plexus.component.annotations.Requirement;
35  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
36  import org.codehaus.plexus.logging.Logger;
37  
38  /**
39   * DefaultProfileManager
40   */
41  @Deprecated
42  public class DefaultProfileManager implements ProfileManager {
43  
44      @Requirement
45      private Logger logger;
46  
47      @Requirement
48      private ProfileSelector profileSelector;
49  
50      private List<String> activatedIds = new ArrayList<>();
51  
52      private List<String> deactivatedIds = new ArrayList<>();
53  
54      private List<String> defaultIds = new ArrayList<>();
55  
56      private Map<String, Profile> profilesById = new LinkedHashMap<>();
57  
58      private Properties requestProperties;
59  
60      /**
61       * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
62       *             correctly in embedded environments.
63       */
64      public DefaultProfileManager(PlexusContainer container) {
65          this(container, null);
66      }
67  
68      /**
69       * the properties passed to the profile manager are the props that
70       * are passed to maven, possibly containing profile activator properties
71       *
72       */
73      public DefaultProfileManager(PlexusContainer container, Properties props) {
74          try {
75              this.profileSelector = container.lookup(ProfileSelector.class);
76              this.logger = ((MutablePlexusContainer) container).getLogger();
77          } catch (ComponentLookupException e) {
78              throw new IllegalStateException(e);
79          }
80          this.requestProperties = props;
81      }
82  
83      public Properties getRequestProperties() {
84          return requestProperties;
85      }
86  
87      public Map<String, Profile> getProfilesById() {
88          return profilesById;
89      }
90  
91      /* (non-Javadoc)
92       * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
93       */
94      public void addProfile(Profile profile) {
95          String profileId = profile.getId();
96  
97          Profile existing = profilesById.get(profileId);
98          if (existing != null) {
99              logger.warn("Overriding profile: '" + profileId + "' (source: " + existing.getSource()
100                     + ") with new instance from source: " + profile.getSource());
101         }
102 
103         profilesById.put(profile.getId(), profile);
104 
105         Activation activation = profile.getActivation();
106 
107         if (activation != null && activation.isActiveByDefault()) {
108             activateAsDefault(profileId);
109         }
110     }
111 
112     /* (non-Javadoc)
113      * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
114      */
115     public void explicitlyActivate(String profileId) {
116         if (!activatedIds.contains(profileId)) {
117             logger.debug("Profile with id: '" + profileId + "' has been explicitly activated.");
118 
119             activatedIds.add(profileId);
120         }
121     }
122 
123     /* (non-Javadoc)
124      * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
125      */
126     public void explicitlyActivate(List<String> profileIds) {
127         for (String profileId1 : profileIds) {
128             explicitlyActivate(profileId1);
129         }
130     }
131 
132     /* (non-Javadoc)
133      * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
134      */
135     public void explicitlyDeactivate(String profileId) {
136         if (!deactivatedIds.contains(profileId)) {
137             logger.debug("Profile with id: '" + profileId + "' has been explicitly deactivated.");
138 
139             deactivatedIds.add(profileId);
140         }
141     }
142 
143     /* (non-Javadoc)
144      * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
145      */
146     public void explicitlyDeactivate(List<String> profileIds) {
147         for (String profileId1 : profileIds) {
148             explicitlyDeactivate(profileId1);
149         }
150     }
151 
152     /* (non-Javadoc)
153      * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
154      */
155     public List getActiveProfiles() throws ProfileActivationException {
156         DefaultProfileActivationContext context = new DefaultProfileActivationContext();
157         context.setActiveProfileIds(activatedIds);
158         context.setInactiveProfileIds(deactivatedIds);
159         context.setSystemProperties(System.getProperties());
160         context.setUserProperties(requestProperties);
161 
162         final List<ProfileActivationException> errors = new ArrayList<>();
163 
164         List<Profile> profiles = profileSelector.getActiveProfiles(profilesById.values(), context, req -> {
165             if (!ModelProblem.Severity.WARNING.equals(req.getSeverity())) {
166                 errors.add(new ProfileActivationException(req.getMessage(), req.getException()));
167             }
168         });
169 
170         if (!errors.isEmpty()) {
171             throw errors.get(0);
172         }
173 
174         return profiles;
175     }
176 
177     /* (non-Javadoc)
178      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
179      */
180     public void addProfiles(List<Profile> profiles) {
181         for (Profile profile1 : profiles) {
182             addProfile(profile1);
183         }
184     }
185 
186     public void activateAsDefault(String profileId) {
187         if (!defaultIds.contains(profileId)) {
188             defaultIds.add(profileId);
189         }
190     }
191 
192     public List<String> getExplicitlyActivatedIds() {
193         return activatedIds;
194     }
195 
196     public List<String> getExplicitlyDeactivatedIds() {
197         return deactivatedIds;
198     }
199 
200     public List getIdsActivatedByDefault() {
201         return defaultIds;
202     }
203 }