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