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.profile.DefaultProfileActivationContext;
26  import org.apache.maven.model.profile.ProfileSelector;
27  import org.apache.maven.profiles.activation.ProfileActivationException;
28  import org.codehaus.plexus.MutablePlexusContainer;
29  import org.codehaus.plexus.PlexusContainer;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32  import org.codehaus.plexus.logging.Logger;
33  
34  import java.util.ArrayList;
35  import java.util.LinkedHashMap;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Properties;
39  
40  /**
41   * DefaultProfileManager
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<String> activatedIds = new ArrayList<>();
55  
56      private List<String> deactivatedIds = new ArrayList<>();
57  
58      private List<String> defaultIds = new ArrayList<>();
59  
60      private Map<String, Profile> 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 environments.
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<String, Profile> 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 = 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<String> profileIds )
143     {
144         for ( String profileId1 : profileIds )
145         {
146             explicitlyActivate( profileId1 );
147         }
148     }
149 
150     /* (non-Javadoc)
151     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
152     */
153     public void explicitlyDeactivate( String profileId )
154     {
155         if ( !deactivatedIds.contains( profileId ) )
156         {
157             logger.debug( "Profile with id: '" + profileId + "' has been explicitly deactivated." );
158 
159             deactivatedIds.add( profileId );
160         }
161     }
162 
163     /* (non-Javadoc)
164     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
165     */
166     public void explicitlyDeactivate( List<String> profileIds )
167     {
168         for ( String profileId1 : profileIds )
169         {
170             explicitlyDeactivate( profileId1 );
171         }
172     }
173 
174     /* (non-Javadoc)
175     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
176     */
177     public List getActiveProfiles()
178         throws ProfileActivationException
179     {
180         DefaultProfileActivationContext context = new DefaultProfileActivationContext();
181         context.setActiveProfileIds( activatedIds );
182         context.setInactiveProfileIds( deactivatedIds );
183         context.setSystemProperties( System.getProperties() );
184         context.setUserProperties( requestProperties );
185 
186         final List<ProfileActivationException> errors = new ArrayList<>();
187 
188         List<Profile> profiles = profileSelector.getActiveProfiles( profilesById.values(), context, req ->
189         {
190             if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
191             {
192                 errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
193             }
194         } );
195 
196         if ( !errors.isEmpty() )
197         {
198             throw errors.get( 0 );
199         }
200 
201         return profiles;
202     }
203 
204     /* (non-Javadoc)
205      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
206      */
207     public void addProfiles( List<Profile> profiles )
208     {
209         for ( Profile profile1 : profiles )
210         {
211             addProfile( profile1 );
212         }
213     }
214 
215     public void activateAsDefault( String profileId )
216     {
217         if ( !defaultIds.contains( profileId ) )
218         {
219             defaultIds.add( profileId );
220         }
221     }
222 
223     public List<String> getExplicitlyActivatedIds()
224     {
225         return activatedIds;
226     }
227 
228     public List<String>  getExplicitlyDeactivatedIds()
229     {
230         return deactivatedIds;
231     }
232 
233     public List getIdsActivatedByDefault()
234     {
235         return defaultIds;
236     }
237 
238 }