1 package org.apache.maven.profiles;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.model.Activation;
23 import org.apache.maven.model.Profile;
24 import org.apache.maven.profiles.activation.ProfileActivationException;
25 import org.apache.maven.profiles.activation.ProfileActivator;
26 import org.apache.maven.settings.Settings;
27 import org.apache.maven.settings.SettingsUtils;
28 import org.codehaus.plexus.PlexusContainer;
29 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
30 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.Map.Entry;
39
40 public class DefaultProfileManager
41 implements ProfileManager
42 {
43 private PlexusContainer container;
44
45 private List activatedIds = new ArrayList();
46
47 private List deactivatedIds = new ArrayList();
48
49 private List defaultIds = new ArrayList();
50
51 private Map profilesById = new LinkedHashMap();
52
53 private Properties requestProperties;
54
55
56
57
58
59 public DefaultProfileManager( PlexusContainer container )
60 {
61 this( container, (Settings)null);
62 }
63
64
65
66
67
68
69 public DefaultProfileManager( PlexusContainer container, Properties props )
70 {
71 this( container, (Settings)null, props );
72
73 }
74
75
76
77
78
79 public DefaultProfileManager( PlexusContainer container, Settings settings )
80 {
81 this.container = container;
82
83 loadSettingsProfiles( settings );
84 }
85
86
87
88
89
90
91 public DefaultProfileManager( PlexusContainer container, Settings settings, Properties props )
92 {
93 this.container = container;
94
95 loadSettingsProfiles( settings );
96
97 if ( props != null )
98 {
99 requestProperties = props;
100 }
101 }
102
103 public Properties getRequestProperties() {
104 return requestProperties;
105 }
106
107 public Map getProfilesById()
108 {
109 return profilesById;
110 }
111
112
113
114
115 public void addProfile( Profile profile )
116 {
117 String profileId = profile.getId();
118
119 Profile existing = (Profile) profilesById.get( profileId );
120 if ( existing != null )
121 {
122 container.getLogger().warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource() +
123 ") with new instance from source: " + profile.getSource() );
124 }
125
126 profilesById.put( profile.getId(), profile );
127
128 Activation activation = profile.getActivation();
129
130 if ( activation != null && activation.isActiveByDefault() )
131 {
132 activateAsDefault( profileId );
133 }
134 }
135
136
137
138
139 public void explicitlyActivate( String profileId )
140 {
141 if ( !activatedIds.contains( profileId ) )
142 {
143 container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
144
145 activatedIds.add( profileId );
146 }
147 }
148
149
150
151
152 public void explicitlyActivate( List profileIds )
153 {
154 for ( Iterator it = profileIds.iterator(); it.hasNext(); )
155 {
156 String profileId = (String) it.next();
157
158 explicitlyActivate( profileId );
159 }
160 }
161
162
163
164
165 public void explicitlyDeactivate( String profileId )
166 {
167 if ( !deactivatedIds.contains( profileId ) )
168 {
169 container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
170
171 deactivatedIds.add( profileId );
172 }
173 }
174
175
176
177
178 public void explicitlyDeactivate( List profileIds )
179 {
180 for ( Iterator it = profileIds.iterator(); it.hasNext(); )
181 {
182 String profileId = (String) it.next();
183
184 explicitlyDeactivate( profileId );
185 }
186 }
187
188
189
190
191 public List getActiveProfiles()
192 throws ProfileActivationException
193 {
194 List activeFromPom = new ArrayList();
195 List activeExternal = new ArrayList();
196
197 for ( Iterator it = profilesById.entrySet().iterator(); it.hasNext(); )
198 {
199 Map.Entry entry = (Entry) it.next();
200
201 String profileId = (String) entry.getKey();
202 Profile profile = (Profile) entry.getValue();
203
204 boolean shouldAdd = false;
205 if ( activatedIds.contains( profileId ) )
206 {
207 shouldAdd = true;
208 }
209 else if ( isActive( profile ) )
210 {
211 shouldAdd = true;
212 }
213
214 if ( !deactivatedIds.contains( profileId ) && shouldAdd )
215 {
216 if ( "pom".equals( profile.getSource() ) )
217 {
218 activeFromPom.add( profile );
219 }
220 else
221 {
222 activeExternal.add( profile );
223 }
224 }
225 }
226
227 if ( activeFromPom.isEmpty() )
228 {
229 for ( Iterator it = defaultIds.iterator(); it.hasNext(); )
230 {
231 String profileId = (String) it.next();
232
233 if ( deactivatedIds.contains( profileId ) )
234 {
235 continue;
236 }
237
238 Profile profile = (Profile) profilesById.get( profileId );
239
240 activeFromPom.add( profile );
241 }
242 }
243
244 List allActive = new ArrayList( activeFromPom.size() + activeExternal.size() );
245
246 allActive.addAll( activeExternal );
247 allActive.addAll( activeFromPom );
248
249 return allActive;
250 }
251
252 private boolean isActive( Profile profile )
253 throws ProfileActivationException
254 {
255 List activators = null;
256 Properties systemProperties = new Properties( System.getProperties() );
257 if ( requestProperties != null )
258 {
259 systemProperties.putAll( requestProperties );
260 }
261
262 container.addContextValue("SystemProperties", systemProperties);
263 try
264 {
265 activators = container.lookupList( ProfileActivator.ROLE );
266
267 for ( Iterator activatorIterator = activators.iterator(); activatorIterator.hasNext(); )
268 {
269 ProfileActivator activator = (ProfileActivator) activatorIterator.next();
270
271 if ( activator.canDetermineActivation( profile ) )
272 {
273 if ( activator.isActive( profile ) )
274 {
275 return true;
276 }
277 }
278 }
279
280 return false;
281 }
282 catch ( ComponentLookupException e )
283 {
284 throw new ProfileActivationException( "Cannot retrieve list of profile activators.", e );
285 }
286 finally
287 {
288 container.getContext().put("SystemProperties", null);
289 if ( activators != null )
290 {
291 try
292 {
293 container.releaseAll( activators );
294 }
295 catch ( ComponentLifecycleException e )
296 {
297 container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
298 }
299 }
300 }
301 }
302
303
304
305
306 public void addProfiles( List profiles )
307 {
308 for ( Iterator it = profiles.iterator(); it.hasNext(); )
309 {
310 Profile profile = (Profile) it.next();
311
312 addProfile( profile );
313 }
314 }
315
316 public void activateAsDefault( String profileId )
317 {
318 if ( !defaultIds.contains( profileId ) )
319 {
320 defaultIds.add( profileId );
321 }
322 }
323
324 public List getExplicitlyActivatedIds()
325 {
326 return activatedIds;
327 }
328
329 public List getExplicitlyDeactivatedIds()
330 {
331 return deactivatedIds;
332 }
333
334 public List getIdsActivatedByDefault()
335 {
336 return defaultIds;
337 }
338
339 public void loadSettingsProfiles( Settings settings )
340 {
341 if ( settings == null )
342 {
343 return;
344 }
345
346 List settingsProfiles = settings.getProfiles();
347
348 List settingsActiveProfileIds = settings.getActiveProfiles();
349
350 explicitlyActivate( settingsActiveProfileIds );
351
352 if ( settingsProfiles != null && !settingsProfiles.isEmpty() )
353 {
354 for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); )
355 {
356 org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next();
357
358 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
359
360 addProfile( profile );
361 }
362 }
363 }
364 }