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