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.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  
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  
63  
64  
65      public DefaultProfileManager(PlexusContainer container) {
66          this(container, null);
67      }
68  
69      
70  
71  
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      
93  
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     
114 
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     
125 
126 
127     public void explicitlyActivate(List<String> profileIds) {
128         for (String profileId1 : profileIds) {
129             explicitlyActivate(profileId1);
130         }
131     }
132 
133     
134 
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     
145 
146 
147     public void explicitlyDeactivate(List<String> profileIds) {
148         for (String profileId1 : profileIds) {
149             explicitlyDeactivate(profileId1);
150         }
151     }
152 
153     
154 
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     
179 
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 }