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 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 public Properties getRequestProperties() {
87 return requestProperties;
88 }
89
90 public Map<String, Profile> getProfilesById() {
91 return profilesById;
92 }
93
94
95
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
116
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
127
128
129 public void explicitlyActivate(List<String> profileIds) {
130 for (String profileId1 : profileIds) {
131 explicitlyActivate(profileId1);
132 }
133 }
134
135
136
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
147
148
149 public void explicitlyDeactivate(List<String> profileIds) {
150 for (String profileId1 : profileIds) {
151 explicitlyDeactivate(profileId1);
152 }
153 }
154
155
156
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 =
168 profileSelector.getActiveProfiles(profilesById.values(), context, new ModelProblemCollector() {
169
170 public void add(ModelProblemCollectorRequest req) {
171 if (!ModelProblem.Severity.WARNING.equals(req.getSeverity())) {
172 errors.add(new ProfileActivationException(req.getMessage(), req.getException()));
173 }
174 }
175 });
176
177 if (!errors.isEmpty()) {
178 throw errors.get(0);
179 }
180
181 return profiles;
182 }
183
184
185
186
187 public void addProfiles(List<Profile> profiles) {
188 for (Profile profile1 : profiles) {
189 addProfile(profile1);
190 }
191 }
192
193 public void activateAsDefault(String profileId) {
194 if (!defaultIds.contains(profileId)) {
195 defaultIds.add(profileId);
196 }
197 }
198
199 public List<String> getExplicitlyActivatedIds() {
200 return activatedIds;
201 }
202
203 public List<String> getExplicitlyDeactivatedIds() {
204 return deactivatedIds;
205 }
206
207 public List getIdsActivatedByDefault() {
208 return defaultIds;
209 }
210 }