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