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 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  
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  
64  
65  
66      @Deprecated
67      public DefaultProfileManager(PlexusContainer container) {
68          this(container, null);
69      }
70  
71      
72  
73  
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      
97  
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     
119 
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     
131 
132 
133     @Override
134     public void explicitlyActivate(List<String> profileIds) {
135         for (String profileId1 : profileIds) {
136             explicitlyActivate(profileId1);
137         }
138     }
139 
140     
141 
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     
153 
154 
155     @Override
156     public void explicitlyDeactivate(List<String> profileIds) {
157         for (String profileId1 : profileIds) {
158             explicitlyDeactivate(profileId1);
159         }
160     }
161 
162     
163 
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     
189 
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 }