KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > profiles > DefaultProfileManager


1 package org.apache.maven.profiles;
2
3 import org.apache.maven.model.Activation;
4 import org.apache.maven.model.Profile;
5 import org.apache.maven.profiles.activation.ProfileActivationException;
6 import org.apache.maven.profiles.activation.ProfileActivator;
7 import org.apache.maven.settings.Settings;
8 import org.apache.maven.settings.SettingsUtils;
9 import org.codehaus.plexus.PlexusContainer;
10 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
11 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Map.Entry;
19
20 /*
21  * Copyright 2001-2005 The Apache Software Foundation.
22  *
23  * Licensed under the Apache License, Version 2.0 (the "License");
24  * you may not use this file except in compliance with the License.
25  * You may obtain a copy of the License at
26  *
27  * http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing, software
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  */

35
36 public class DefaultProfileManager
37     implements ProfileManager
38 {
39     private PlexusContainer container;
40
41     private List JavaDoc activatedIds = new ArrayList JavaDoc();
42
43     private List JavaDoc deactivatedIds = new ArrayList JavaDoc();
44
45     private List JavaDoc defaultIds = new ArrayList JavaDoc();
46
47     private Map JavaDoc profilesById = new HashMap JavaDoc();
48
49     public DefaultProfileManager( PlexusContainer container )
50     {
51         this( container, null );
52     }
53
54     public DefaultProfileManager( PlexusContainer container, Settings settings )
55     {
56         this.container = container;
57
58         loadSettingsProfiles( settings );
59     }
60
61     public Map JavaDoc getProfilesById()
62     {
63         return profilesById;
64     }
65
66     /* (non-Javadoc)
67     * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
68     */

69     public void addProfile( Profile profile )
70     {
71         String JavaDoc profileId = profile.getId();
72
73         Profile existing = (Profile) profilesById.get( profileId );
74         if ( existing != null )
75         {
76             container.getLogger().warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource() +
77                 ") with new instance from source: " + profile.getSource() );
78         }
79
80         profilesById.put( profile.getId(), profile );
81
82         Activation activation = profile.getActivation();
83
84         if ( activation != null && activation.isActiveByDefault() )
85         {
86             activateAsDefault( profileId );
87         }
88     }
89
90     /* (non-Javadoc)
91     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
92     */

93     public void explicitlyActivate( String JavaDoc profileId )
94     {
95         if ( !activatedIds.contains( profileId ) )
96         {
97             container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
98
99             activatedIds.add( profileId );
100         }
101     }
102
103     /* (non-Javadoc)
104     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
105     */

106     public void explicitlyActivate( List JavaDoc profileIds )
107     {
108         for ( Iterator JavaDoc it = profileIds.iterator(); it.hasNext(); )
109         {
110             String JavaDoc profileId = (String JavaDoc) it.next();
111
112             explicitlyActivate( profileId );
113         }
114     }
115
116     /* (non-Javadoc)
117     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
118     */

119     public void explicitlyDeactivate( String JavaDoc profileId )
120     {
121         if ( !deactivatedIds.contains( profileId ) )
122         {
123             container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
124
125             deactivatedIds.add( profileId );
126         }
127     }
128
129     /* (non-Javadoc)
130     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
131     */

132     public void explicitlyDeactivate( List JavaDoc profileIds )
133     {
134         for ( Iterator JavaDoc it = profileIds.iterator(); it.hasNext(); )
135         {
136             String JavaDoc profileId = (String JavaDoc) it.next();
137
138             explicitlyDeactivate( profileId );
139         }
140     }
141
142     /* (non-Javadoc)
143     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
144     */

145     public List JavaDoc getActiveProfiles()
146         throws ProfileActivationException
147     {
148         List JavaDoc activeFromPom = new ArrayList JavaDoc();
149         List JavaDoc activeExternal = new ArrayList JavaDoc();
150
151         for ( Iterator JavaDoc it = profilesById.entrySet().iterator(); it.hasNext(); )
152         {
153             Map.Entry JavaDoc entry = (Entry) it.next();
154
155             String JavaDoc profileId = (String JavaDoc) entry.getKey();
156             Profile profile = (Profile) entry.getValue();
157
158             boolean shouldAdd = false;
159             if ( activatedIds.contains( profileId ) )
160             {
161                 shouldAdd = true;
162             }
163             else if ( !deactivatedIds.contains( profileId ) && isActive( profile ) )
164             {
165                 shouldAdd = true;
166             }
167             
168             if ( shouldAdd )
169             {
170                 if ( "pom".equals( profile.getSource() ) )
171                 {
172                     activeFromPom.add( profile );
173                 }
174                 else
175                 {
176                     activeExternal.add( profile );
177                 }
178             }
179         }
180
181         if ( activeFromPom.isEmpty() )
182         {
183             for ( Iterator JavaDoc it = defaultIds.iterator(); it.hasNext(); )
184             {
185                 String JavaDoc profileId = (String JavaDoc) it.next();
186
187                 Profile profile = (Profile) profilesById.get( profileId );
188
189                 activeFromPom.add( profile );
190             }
191         }
192         
193         List JavaDoc allActive = new ArrayList JavaDoc( activeFromPom.size() + activeExternal.size() );
194         
195         allActive.addAll( activeExternal );
196         allActive.addAll( activeFromPom );
197
198         return allActive;
199     }
200
201     private boolean isActive( Profile profile )
202         throws ProfileActivationException
203     {
204         List JavaDoc activators = null;
205         try
206         {
207             activators = container.lookupList( ProfileActivator.ROLE );
208
209             for ( Iterator JavaDoc activatorIterator = activators.iterator(); activatorIterator.hasNext(); )
210             {
211                 ProfileActivator activator = (ProfileActivator) activatorIterator.next();
212
213                 if ( activator.canDetermineActivation( profile ) )
214                 {
215                     return activator.isActive( profile );
216                 }
217             }
218
219             return false;
220         }
221         catch ( ComponentLookupException e )
222         {
223             throw new ProfileActivationException( "Cannot retrieve list of profile activators.", e );
224         }
225         finally
226         {
227             try
228             {
229                 container.releaseAll( activators );
230             }
231             catch ( ComponentLifecycleException e )
232             {
233                 container.getLogger().debug( "Error releasing profile activators - ignoring.", e );
234             }
235         }
236     }
237
238     /* (non-Javadoc)
239      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
240      */

241     public void addProfiles( List JavaDoc profiles )
242     {
243         for ( Iterator JavaDoc it = profiles.iterator(); it.hasNext(); )
244         {
245             Profile profile = (Profile) it.next();
246
247             addProfile( profile );
248         }
249     }
250
251     public void activateAsDefault( String JavaDoc profileId )
252     {
253         if ( !defaultIds.contains( profileId ) )
254         {
255             defaultIds.add( profileId );
256         }
257     }
258
259     public List JavaDoc getExplicitlyActivatedIds()
260     {
261         return activatedIds;
262     }
263
264     public List JavaDoc getExplicitlyDeactivatedIds()
265     {
266         return deactivatedIds;
267     }
268
269     public List JavaDoc getIdsActivatedByDefault()
270     {
271         return defaultIds;
272     }
273
274     public void loadSettingsProfiles( Settings settings )
275     {
276         if ( settings == null )
277         {
278             return;
279         }
280
281         List JavaDoc settingsProfiles = settings.getProfiles();
282
283         if ( settingsProfiles != null && !settingsProfiles.isEmpty() )
284         {
285             List JavaDoc settingsActiveProfileIds = settings.getActiveProfiles();
286
287             explicitlyActivate( settingsActiveProfileIds );
288
289             for ( Iterator JavaDoc it = settings.getProfiles().iterator(); it.hasNext(); )
290             {
291                 org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next();
292
293                 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
294
295                 addProfile( profile );
296             }
297         }
298     }
299 }
300
Popular Tags