1 package org.apache.maven.model.profile;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.List;
26
27 import org.apache.maven.model.Activation;
28 import org.apache.maven.model.Profile;
29 import org.apache.maven.model.building.ModelProblemCollector;
30 import org.apache.maven.model.building.ModelProblem.Severity;
31 import org.apache.maven.model.profile.activation.ProfileActivator;
32 import org.codehaus.plexus.component.annotations.Component;
33 import org.codehaus.plexus.component.annotations.Requirement;
34
35
36
37
38
39
40 @Component( role = ProfileSelector.class )
41 public class DefaultProfileSelector
42 implements ProfileSelector
43 {
44
45 @Requirement( role = ProfileActivator.class )
46 private List<ProfileActivator> activators = new ArrayList<ProfileActivator>();
47
48 public DefaultProfileSelector addProfileActivator( ProfileActivator profileActivator )
49 {
50 if ( profileActivator != null )
51 {
52 activators.add( profileActivator );
53 }
54 return this;
55 }
56
57 public List<Profile> getActiveProfiles( Collection<Profile> profiles, ProfileActivationContext context,
58 ModelProblemCollector problems )
59 {
60 Collection<String> activatedIds = new HashSet<String>( context.getActiveProfileIds() );
61 Collection<String> deactivatedIds = new HashSet<String>( context.getInactiveProfileIds() );
62
63 List<Profile> activeProfiles = new ArrayList<Profile>( profiles.size() );
64 List<Profile> activePomProfilesByDefault = new ArrayList<Profile>();
65 boolean activatedPomProfileNotByDefault = false;
66
67 for ( Profile profile : profiles )
68 {
69 if ( !deactivatedIds.contains( profile.getId() ) )
70 {
71 if ( activatedIds.contains( profile.getId() ) || isActive( profile, context, problems ) )
72 {
73 activeProfiles.add( profile );
74
75 if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
76 {
77 activatedPomProfileNotByDefault = true;
78 }
79 }
80 else if ( isActiveByDefault( profile ) )
81 {
82 if ( Profile.SOURCE_POM.equals( profile.getSource() ) )
83 {
84 activePomProfilesByDefault.add( profile );
85 }
86 else
87 {
88 activeProfiles.add( profile );
89 }
90 }
91
92 }
93 }
94
95 if ( !activatedPomProfileNotByDefault )
96 {
97 activeProfiles.addAll( activePomProfilesByDefault );
98 }
99
100 return activeProfiles;
101 }
102
103 private boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
104 {
105 for ( ProfileActivator activator : activators )
106 {
107 try
108 {
109 if ( activator.isActive( profile, context, problems ) )
110 {
111 return true;
112 }
113 }
114 catch ( RuntimeException e )
115 {
116 problems.add( Severity.ERROR, "Failed to determine activation for profile " + profile.getId(),
117 profile.getLocation( "" ), e );
118 return false;
119 }
120 }
121 return false;
122 }
123
124 private boolean isActiveByDefault( Profile profile )
125 {
126 Activation activation = profile.getActivation();
127 return activation != null && activation.isActiveByDefault();
128 }
129
130 }