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