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.model.building.ModelProblem;
25 import org.apache.maven.model.building.ModelProblemCollector;
26 import org.apache.maven.model.profile.DefaultProfileActivationContext;
27 import org.apache.maven.model.profile.ProfileSelector;
28 import org.apache.maven.profiles.activation.ProfileActivationException;
29 import org.codehaus.plexus.MutablePlexusContainer;
30 import org.codehaus.plexus.PlexusContainer;
31 import org.codehaus.plexus.component.annotations.Requirement;
32 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33 import org.codehaus.plexus.logging.Logger;
34
35 import java.util.ArrayList;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Properties;
40 import org.apache.maven.model.building.ModelProblemCollectorRequest;
41
42
43
44
45 @Deprecated
46 public class DefaultProfileManager
47 implements ProfileManager
48 {
49
50 @Requirement
51 private Logger logger;
52
53 @Requirement
54 private ProfileSelector profileSelector;
55
56 private List<String> activatedIds = new ArrayList<>();
57
58 private List<String> deactivatedIds = new ArrayList<>();
59
60 private List<String> defaultIds = new ArrayList<>();
61
62 private Map<String, Profile> profilesById = new LinkedHashMap<>();
63
64 private Properties requestProperties;
65
66
67
68
69
70 @Deprecated
71 public DefaultProfileManager( PlexusContainer container )
72 {
73 this( container, null );
74 }
75
76
77
78
79
80
81 public DefaultProfileManager( PlexusContainer container, Properties props )
82 {
83 try
84 {
85 this.profileSelector = container.lookup( ProfileSelector.class );
86 this.logger = ( (MutablePlexusContainer) container ).getLogger();
87 }
88 catch ( ComponentLookupException e )
89 {
90 throw new IllegalStateException( e );
91 }
92 this.requestProperties = props;
93 }
94
95 public Properties getRequestProperties()
96 {
97 return requestProperties;
98 }
99
100 public Map<String, Profile> getProfilesById()
101 {
102 return profilesById;
103 }
104
105
106
107
108 public void addProfile( Profile profile )
109 {
110 String profileId = profile.getId();
111
112 Profile existing = profilesById.get( profileId );
113 if ( existing != null )
114 {
115 logger.warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource()
116 + ") with new instance from source: " + profile.getSource() );
117 }
118
119 profilesById.put( profile.getId(), profile );
120
121 Activation activation = profile.getActivation();
122
123 if ( activation != null && activation.isActiveByDefault() )
124 {
125 activateAsDefault( profileId );
126 }
127 }
128
129
130
131
132 public void explicitlyActivate( String profileId )
133 {
134 if ( !activatedIds.contains( profileId ) )
135 {
136 logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
137
138 activatedIds.add( profileId );
139 }
140 }
141
142
143
144
145 public void explicitlyActivate( List<String> profileIds )
146 {
147 for ( String profileId1 : profileIds )
148 {
149 explicitlyActivate( profileId1 );
150 }
151 }
152
153
154
155
156 public void explicitlyDeactivate( String profileId )
157 {
158 if ( !deactivatedIds.contains( profileId ) )
159 {
160 logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
161
162 deactivatedIds.add( profileId );
163 }
164 }
165
166
167
168
169 public void explicitlyDeactivate( List<String> profileIds )
170 {
171 for ( String profileId1 : profileIds )
172 {
173 explicitlyDeactivate( profileId1 );
174 }
175 }
176
177
178
179
180 public List getActiveProfiles()
181 throws ProfileActivationException
182 {
183 DefaultProfileActivationContext context = new DefaultProfileActivationContext();
184 context.setActiveProfileIds( activatedIds );
185 context.setInactiveProfileIds( deactivatedIds );
186 context.setSystemProperties( System.getProperties() );
187 context.setUserProperties( requestProperties );
188
189 final List<ProfileActivationException> errors = new ArrayList<>();
190
191 List<Profile> profiles =
192 profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
193 {
194
195 public void add( ModelProblemCollectorRequest req )
196 {
197 if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
198 {
199 errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
200 }
201 }
202 } );
203
204 if ( !errors.isEmpty() )
205 {
206 throw errors.get( 0 );
207 }
208
209 return profiles;
210 }
211
212
213
214
215 public void addProfiles( List<Profile> profiles )
216 {
217 for ( Profile profile1 : profiles )
218 {
219 addProfile( profile1 );
220 }
221 }
222
223 public void activateAsDefault( String profileId )
224 {
225 if ( !defaultIds.contains( profileId ) )
226 {
227 defaultIds.add( profileId );
228 }
229 }
230
231 public List<String> getExplicitlyActivatedIds()
232 {
233 return activatedIds;
234 }
235
236 public List<String> getExplicitlyDeactivatedIds()
237 {
238 return deactivatedIds;
239 }
240
241 public List getIdsActivatedByDefault()
242 {
243 return defaultIds;
244 }
245
246 }