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