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