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