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.Iterator;
040    import java.util.LinkedHashMap;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.Properties;
044    
045    @Deprecated
046    public class DefaultProfileManager
047        implements ProfileManager
048    {
049    
050        @Requirement
051        private Logger logger;
052    
053        @Requirement
054        private ProfileSelector profileSelector;
055    
056        private List activatedIds = new ArrayList();
057    
058        private List deactivatedIds = new ArrayList();
059    
060        private List defaultIds = new ArrayList();
061    
062        private Map profilesById = new LinkedHashMap();
063    
064        private Properties requestProperties;
065    
066        /**
067         * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
068         *             correctly in embedded envirnments.
069         */
070        public DefaultProfileManager( PlexusContainer container )
071        {
072            this( container, null );
073        }
074    
075        /**
076         * the properties passed to the profile manager are the props that
077         * are passed to maven, possibly containing profile activator properties
078         *
079         */
080        public DefaultProfileManager( PlexusContainer container, Properties props )
081        {
082            try
083            {
084                this.profileSelector = container.lookup( ProfileSelector.class );
085                this.logger = ( (MutablePlexusContainer) container ).getLogger();
086            }
087            catch ( ComponentLookupException e )
088            {
089                throw new IllegalStateException( e );
090            }
091            this.requestProperties = props;
092        }
093    
094        public Properties getRequestProperties()
095        {
096            return requestProperties;
097        }
098    
099        public Map getProfilesById()
100        {
101            return profilesById;
102        }
103    
104        /* (non-Javadoc)
105        * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
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        /* (non-Javadoc)
129        * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
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        /* (non-Javadoc)
142        * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
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        /* (non-Javadoc)
155        * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
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        /* (non-Javadoc)
168        * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
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        /* (non-Javadoc)
181        * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
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        /* (non-Javadoc)
217         * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
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    }