View Javadoc

1   package org.apache.maven.profiles;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.building.ModelProblemCollector;
26  import org.apache.maven.model.profile.DefaultProfileActivationContext;
27  import org.apache.maven.model.profile.ProfileSelector;
28  import org.apache.maven.profiles.activation.ProfileActivationException;
29  import org.codehaus.plexus.MutablePlexusContainer;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.logging.Logger;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Properties;
41  import org.apache.maven.model.building.ModelProblemCollectorRequest;
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 activatedIds = new ArrayList();
55  
56      private List deactivatedIds = new ArrayList();
57  
58      private List defaultIds = new ArrayList();
59  
60      private Map profilesById = new LinkedHashMap();
61  
62      private Properties requestProperties;
63  
64      /**
65       * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
66       *             correctly in embedded envirnments.
67       */
68      public DefaultProfileManager( PlexusContainer container )
69      {
70          this( container, null );
71      }
72  
73      /**
74       * the properties passed to the profile manager are the props that
75       * are passed to maven, possibly containing profile activator properties
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 getProfilesById()
98      {
99          return profilesById;
100     }
101 
102     /* (non-Javadoc)
103     * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
104     */
105     public void addProfile( Profile profile )
106     {
107         String profileId = profile.getId();
108 
109         Profile existing = (Profile) 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     /* (non-Javadoc)
127     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
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     /* (non-Javadoc)
140     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
141     */
142     public void explicitlyActivate( List profileIds )
143     {
144         for ( Object profileId1 : profileIds )
145         {
146             String profileId = (String) profileId1;
147 
148             explicitlyActivate( profileId );
149         }
150     }
151 
152     /* (non-Javadoc)
153     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
154     */
155     public void explicitlyDeactivate( String profileId )
156     {
157         if ( !deactivatedIds.contains( profileId ) )
158         {
159             logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
160 
161             deactivatedIds.add( profileId );
162         }
163     }
164 
165     /* (non-Javadoc)
166     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
167     */
168     public void explicitlyDeactivate( List profileIds )
169     {
170         for ( Object profileId1 : profileIds )
171         {
172             String profileId = (String) profileId1;
173 
174             explicitlyDeactivate( profileId );
175         }
176     }
177 
178     /* (non-Javadoc)
179     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
180     */
181     public List getActiveProfiles()
182         throws ProfileActivationException
183     {
184         DefaultProfileActivationContext context = new DefaultProfileActivationContext();
185         context.setActiveProfileIds( activatedIds );
186         context.setInactiveProfileIds( deactivatedIds );
187         context.setSystemProperties( System.getProperties() );
188         context.setUserProperties( requestProperties );
189 
190         final List<ProfileActivationException> errors = new ArrayList<ProfileActivationException>();
191 
192         List<Profile> profiles =
193             profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
194             {
195 
196                 public void add( ModelProblemCollectorRequest req )
197                 {
198                     if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
199                     {
200                         errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
201                     }
202                 }
203             } );
204 
205         if ( !errors.isEmpty() )
206         {
207             throw errors.get( 0 );
208         }
209 
210         return profiles;
211     }
212 
213     /* (non-Javadoc)
214      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
215      */
216     public void addProfiles( List profiles )
217     {
218         for ( Object profile1 : profiles )
219         {
220             Profile profile = (Profile) profile1;
221 
222             addProfile( profile );
223         }
224     }
225 
226     public void activateAsDefault( String profileId )
227     {
228         if ( !defaultIds.contains( profileId ) )
229         {
230             defaultIds.add( profileId );
231         }
232     }
233 
234     public List getExplicitlyActivatedIds()
235     {
236         return activatedIds;
237     }
238 
239     public List getExplicitlyDeactivatedIds()
240     {
241         return deactivatedIds;
242     }
243 
244     public List getIdsActivatedByDefault()
245     {
246         return defaultIds;
247     }
248 
249 }