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