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