001package org.apache.maven.profiles;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.model.Activation;
023import org.apache.maven.model.Profile;
024import org.apache.maven.model.building.ModelProblem;
025import org.apache.maven.model.building.ModelProblemCollector;
026import org.apache.maven.model.profile.DefaultProfileActivationContext;
027import org.apache.maven.model.profile.ProfileSelector;
028import org.apache.maven.profiles.activation.ProfileActivationException;
029import org.codehaus.plexus.MutablePlexusContainer;
030import org.codehaus.plexus.PlexusContainer;
031import org.codehaus.plexus.component.annotations.Requirement;
032import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
033import org.codehaus.plexus.logging.Logger;
034
035import java.util.ArrayList;
036import java.util.LinkedHashMap;
037import java.util.List;
038import java.util.Map;
039import java.util.Properties;
040import org.apache.maven.model.building.ModelProblemCollectorRequest;
041
042@Deprecated
043public class DefaultProfileManager
044    implements ProfileManager
045{
046
047    @Requirement
048    private Logger logger;
049
050    @Requirement
051    private ProfileSelector profileSelector;
052
053    private List activatedIds = new ArrayList();
054
055    private List deactivatedIds = new ArrayList();
056
057    private List defaultIds = new ArrayList();
058
059    private Map profilesById = new LinkedHashMap();
060
061    private Properties requestProperties;
062
063    /**
064     * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
065     *             correctly in embedded envirnments.
066     */
067    public DefaultProfileManager( PlexusContainer container )
068    {
069        this( container, null );
070    }
071
072    /**
073     * the properties passed to the profile manager are the props that
074     * are passed to maven, possibly containing profile activator properties
075     *
076     */
077    public DefaultProfileManager( PlexusContainer container, Properties props )
078    {
079        try
080        {
081            this.profileSelector = container.lookup( ProfileSelector.class );
082            this.logger = ( (MutablePlexusContainer) container ).getLogger();
083        }
084        catch ( ComponentLookupException e )
085        {
086            throw new IllegalStateException( e );
087        }
088        this.requestProperties = props;
089    }
090
091    public Properties getRequestProperties()
092    {
093        return requestProperties;
094    }
095
096    public Map getProfilesById()
097    {
098        return profilesById;
099    }
100
101    /* (non-Javadoc)
102    * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
103    */
104    public void addProfile( Profile profile )
105    {
106        String profileId = profile.getId();
107
108        Profile existing = (Profile) profilesById.get( profileId );
109        if ( existing != null )
110        {
111            logger.warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource()
112                + ") with new instance from source: " + profile.getSource() );
113        }
114
115        profilesById.put( profile.getId(), profile );
116
117        Activation activation = profile.getActivation();
118
119        if ( activation != null && activation.isActiveByDefault() )
120        {
121            activateAsDefault( profileId );
122        }
123    }
124
125    /* (non-Javadoc)
126    * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
127    */
128    public void explicitlyActivate( String profileId )
129    {
130        if ( !activatedIds.contains( profileId ) )
131        {
132            logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
133
134            activatedIds.add( profileId );
135        }
136    }
137
138    /* (non-Javadoc)
139    * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
140    */
141    public void explicitlyActivate( List profileIds )
142    {
143        for ( Object profileId1 : profileIds )
144        {
145            String profileId = (String) profileId1;
146
147            explicitlyActivate( profileId );
148        }
149    }
150
151    /* (non-Javadoc)
152    * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
153    */
154    public void explicitlyDeactivate( String profileId )
155    {
156        if ( !deactivatedIds.contains( profileId ) )
157        {
158            logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
159
160            deactivatedIds.add( profileId );
161        }
162    }
163
164    /* (non-Javadoc)
165    * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
166    */
167    public void explicitlyDeactivate( List profileIds )
168    {
169        for ( Object profileId1 : profileIds )
170        {
171            String profileId = (String) profileId1;
172
173            explicitlyDeactivate( profileId );
174        }
175    }
176
177    /* (non-Javadoc)
178    * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
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<ProfileActivationException>();
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    /* (non-Javadoc)
213     * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
214     */
215    public void addProfiles( List profiles )
216    {
217        for ( Object profile1 : profiles )
218        {
219            Profile profile = (Profile) profile1;
220
221            addProfile( profile );
222        }
223    }
224
225    public void activateAsDefault( String profileId )
226    {
227        if ( !defaultIds.contains( profileId ) )
228        {
229            defaultIds.add( profileId );
230        }
231    }
232
233    public List getExplicitlyActivatedIds()
234    {
235        return activatedIds;
236    }
237
238    public List getExplicitlyDeactivatedIds()
239    {
240        return deactivatedIds;
241    }
242
243    public List getIdsActivatedByDefault()
244    {
245        return defaultIds;
246    }
247
248}