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.profile.DefaultProfileActivationContext;
26 import org.apache.maven.model.profile.ProfileSelector;
27 import org.apache.maven.profiles.activation.ProfileActivationException;
28 import org.codehaus.plexus.MutablePlexusContainer;
29 import org.codehaus.plexus.PlexusContainer;
30 import org.codehaus.plexus.component.annotations.Requirement;
31 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32 import org.codehaus.plexus.logging.Logger;
33
34 import java.util.ArrayList;
35 import java.util.LinkedHashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Properties;
39
40
41
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<String> activatedIds = new ArrayList<>();
55
56 private List<String> deactivatedIds = new ArrayList<>();
57
58 private List<String> defaultIds = new ArrayList<>();
59
60 private Map<String, Profile> 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<String, Profile> 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 = 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<String> profileIds )
143 {
144 for ( String profileId1 : profileIds )
145 {
146 explicitlyActivate( profileId1 );
147 }
148 }
149
150
151
152
153 public void explicitlyDeactivate( String profileId )
154 {
155 if ( !deactivatedIds.contains( profileId ) )
156 {
157 logger.debug( "Profile with id: '" + profileId + "' has been explicitly deactivated." );
158
159 deactivatedIds.add( profileId );
160 }
161 }
162
163
164
165
166 public void explicitlyDeactivate( List<String> profileIds )
167 {
168 for ( String profileId1 : profileIds )
169 {
170 explicitlyDeactivate( profileId1 );
171 }
172 }
173
174
175
176
177 public List getActiveProfiles()
178 throws ProfileActivationException
179 {
180 DefaultProfileActivationContext context = new DefaultProfileActivationContext();
181 context.setActiveProfileIds( activatedIds );
182 context.setInactiveProfileIds( deactivatedIds );
183 context.setSystemProperties( System.getProperties() );
184 context.setUserProperties( requestProperties );
185
186 final List<ProfileActivationException> errors = new ArrayList<>();
187
188 List<Profile> profiles = profileSelector.getActiveProfiles( profilesById.values(), context, req ->
189 {
190 if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
191 {
192 errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
193 }
194 } );
195
196 if ( !errors.isEmpty() )
197 {
198 throw errors.get( 0 );
199 }
200
201 return profiles;
202 }
203
204
205
206
207 public void addProfiles( List<Profile> profiles )
208 {
209 for ( Profile profile1 : profiles )
210 {
211 addProfile( profile1 );
212 }
213 }
214
215 public void activateAsDefault( String profileId )
216 {
217 if ( !defaultIds.contains( profileId ) )
218 {
219 defaultIds.add( profileId );
220 }
221 }
222
223 public List<String> getExplicitlyActivatedIds()
224 {
225 return activatedIds;
226 }
227
228 public List<String> getExplicitlyDeactivatedIds()
229 {
230 return deactivatedIds;
231 }
232
233 public List getIdsActivatedByDefault()
234 {
235 return defaultIds;
236 }
237
238 }