001    package 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    
022    import org.apache.maven.model.Activation;
023    import org.apache.maven.model.InputLocation;
024    import org.apache.maven.model.Profile;
025    import org.apache.maven.model.building.ModelProblem;
026    import org.apache.maven.model.building.ModelProblemCollector;
027    import org.apache.maven.model.building.ModelProblem.Severity;
028    import org.apache.maven.model.profile.DefaultProfileActivationContext;
029    import org.apache.maven.model.profile.ProfileActivationContext;
030    import org.apache.maven.model.profile.ProfileSelector;
031    import org.apache.maven.profiles.activation.ProfileActivationException;
032    import org.codehaus.plexus.MutablePlexusContainer;
033    import org.codehaus.plexus.PlexusContainer;
034    import org.codehaus.plexus.component.annotations.Requirement;
035    import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
036    import org.codehaus.plexus.logging.Logger;
037    
038    import java.util.ArrayList;
039    import java.util.Collections;
040    import java.util.Iterator;
041    import java.util.LinkedHashMap;
042    import java.util.List;
043    import java.util.Map;
044    import java.util.Properties;
045    import org.apache.maven.model.building.ModelProblem.Version;
046    import org.apache.maven.model.building.ModelProblemCollectorRequest;
047    
048    @Deprecated
049    public class DefaultProfileManager
050        implements ProfileManager
051    {
052    
053        @Requirement
054        private Logger logger;
055    
056        @Requirement
057        private ProfileSelector profileSelector;
058    
059        private List activatedIds = new ArrayList();
060    
061        private List deactivatedIds = new ArrayList();
062    
063        private List defaultIds = new ArrayList();
064    
065        private Map profilesById = new LinkedHashMap();
066    
067        private Properties requestProperties;
068    
069        /**
070         * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
071         *             correctly in embedded envirnments.
072         */
073        public DefaultProfileManager( PlexusContainer container )
074        {
075            this( container, null );
076        }
077    
078        /**
079         * the properties passed to the profile manager are the props that
080         * are passed to maven, possibly containing profile activator properties
081         *
082         */
083        public DefaultProfileManager( PlexusContainer container, Properties props )
084        {
085            try
086            {
087                this.profileSelector = container.lookup( ProfileSelector.class );
088                this.logger = ( (MutablePlexusContainer) container ).getLogger();
089            }
090            catch ( ComponentLookupException e )
091            {
092                throw new IllegalStateException( e );
093            }
094            this.requestProperties = props;
095        }
096    
097        public Properties getRequestProperties()
098        {
099            return requestProperties;
100        }
101    
102        public Map getProfilesById()
103        {
104            return profilesById;
105        }
106    
107        /* (non-Javadoc)
108        * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
109        */
110        public void addProfile( Profile profile )
111        {
112            String profileId = profile.getId();
113    
114            Profile existing = (Profile) profilesById.get( profileId );
115            if ( existing != null )
116            {
117                logger.warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource()
118                    + ") with new instance from source: " + profile.getSource() );
119            }
120    
121            profilesById.put( profile.getId(), profile );
122    
123            Activation activation = profile.getActivation();
124    
125            if ( activation != null && activation.isActiveByDefault() )
126            {
127                activateAsDefault( profileId );
128            }
129        }
130    
131        /* (non-Javadoc)
132        * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
133        */
134        public void explicitlyActivate( String profileId )
135        {
136            if ( !activatedIds.contains( profileId ) )
137            {
138                logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
139    
140                activatedIds.add( profileId );
141            }
142        }
143    
144        /* (non-Javadoc)
145        * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
146        */
147        public void explicitlyActivate( List profileIds )
148        {
149            for ( Iterator it = profileIds.iterator(); it.hasNext(); )
150            {
151                String profileId = (String) it.next();
152    
153                explicitlyActivate( profileId );
154            }
155        }
156    
157        /* (non-Javadoc)
158        * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
159        */
160        public void explicitlyDeactivate( String profileId )
161        {
162            if ( !deactivatedIds.contains( profileId ) )
163            {
164                logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
165    
166                deactivatedIds.add( profileId );
167            }
168        }
169    
170        /* (non-Javadoc)
171        * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
172        */
173        public void explicitlyDeactivate( List profileIds )
174        {
175            for ( Iterator it = profileIds.iterator(); it.hasNext(); )
176            {
177                String profileId = (String) it.next();
178    
179                explicitlyDeactivate( profileId );
180            }
181        }
182    
183        /* (non-Javadoc)
184        * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
185        */
186        public List getActiveProfiles()
187            throws ProfileActivationException
188        {
189            DefaultProfileActivationContext context = new DefaultProfileActivationContext();
190            context.setActiveProfileIds( activatedIds );
191            context.setInactiveProfileIds( deactivatedIds );
192            context.setSystemProperties( System.getProperties() );
193            context.setUserProperties( requestProperties );
194    
195            final List<ProfileActivationException> errors = new ArrayList<ProfileActivationException>();
196    
197            List<Profile> profiles =
198                profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
199                {
200    
201                    public void add( ModelProblemCollectorRequest req )
202                    {
203                        if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
204                        {
205                            errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
206                        }
207                    }
208                } );
209    
210            if ( !errors.isEmpty() )
211            {
212                throw errors.get( 0 );
213            }
214    
215            return profiles;
216        }
217    
218        /* (non-Javadoc)
219         * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
220         */
221        public void addProfiles( List profiles )
222        {
223            for ( Iterator it = profiles.iterator(); it.hasNext(); )
224            {
225                Profile profile = (Profile) it.next();
226    
227                addProfile( profile );
228            }
229        }
230    
231        public void activateAsDefault( String profileId )
232        {
233            if ( !defaultIds.contains( profileId ) )
234            {
235                defaultIds.add( profileId );
236            }
237        }
238    
239        public List getExplicitlyActivatedIds()
240        {
241            return activatedIds;
242        }
243    
244        public List getExplicitlyDeactivatedIds()
245        {
246            return deactivatedIds;
247        }
248    
249        public List getIdsActivatedByDefault()
250        {
251            return defaultIds;
252        }
253    
254    }