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