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