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 boolean isActive = false;
108 for ( ProfileActivator activator : activators )
109 {
110 if ( activator.presentInConfig( profile, context, problems ) )
111 {
112 isActive = true;
113 }
114 }
115 for ( ProfileActivator activator : activators )
116 {
117 try
118 {
119 if ( activator.presentInConfig( profile, context, problems ) )
120 {
121 isActive &= activator.isActive( profile, context, problems );
122 }
123 }
124 catch ( RuntimeException e )
125 {
126 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
127 .setMessage( "Failed to determine activation for profile " + profile.getId() )
128 .setLocation( profile.getLocation( "" ) )
129 .setException( e ) );
130 return false;
131 }
132 }
133 return isActive;
134 }
135
136 private boolean isActiveByDefault( Profile profile )
137 {
138 Activation activation = profile.getActivation();
139 return activation != null && activation.isActiveByDefault();
140 }
141
142 }