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.LinkedHashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Properties;
40  import org.apache.maven.model.building.ModelProblemCollectorRequest;
41  
42  @Deprecated
43  public class DefaultProfileManager
44      implements ProfileManager
45  {
46  
47      @Requirement
48      private Logger logger;
49  
50      @Requirement
51      private ProfileSelector profileSelector;
52  
53      private List<String> activatedIds = new ArrayList<>();
54  
55      private List<String> deactivatedIds = new ArrayList<>();
56  
57      private List<String> defaultIds = new ArrayList<>();
58  
59      private Map<String, Profile> profilesById = new LinkedHashMap<>();
60  
61      private Properties requestProperties;
62  
63      /**
64       * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
65       *             correctly in embedded envirnments.
66       */
67      public DefaultProfileManager( PlexusContainer container )
68      {
69          this( container, null );
70      }
71  
72      /**
73       * the properties passed to the profile manager are the props that
74       * are passed to maven, possibly containing profile activator properties
75       *
76       */
77      public DefaultProfileManager( PlexusContainer container, Properties props )
78      {
79          try
80          {
81              this.profileSelector = container.lookup( ProfileSelector.class );
82              this.logger = ( (MutablePlexusContainer) container ).getLogger();
83          }
84          catch ( ComponentLookupException e )
85          {
86              throw new IllegalStateException( e );
87          }
88          this.requestProperties = props;
89      }
90  
91      public Properties getRequestProperties()
92      {
93          return requestProperties;
94      }
95  
96      public Map<String, Profile> getProfilesById()
97      {
98          return profilesById;
99      }
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 = 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<String> profileIds )
142     {
143         for ( String profileId1 : profileIds )
144         {
145             explicitlyActivate( profileId1 );
146         }
147     }
148 
149     /* (non-Javadoc)
150     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
151     */
152     public void explicitlyDeactivate( String profileId )
153     {
154         if ( !deactivatedIds.contains( profileId ) )
155         {
156             logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
157 
158             deactivatedIds.add( profileId );
159         }
160     }
161 
162     /* (non-Javadoc)
163     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
164     */
165     public void explicitlyDeactivate( List<String> profileIds )
166     {
167         for ( String profileId1 : profileIds )
168         {
169             explicitlyDeactivate( profileId1 );
170         }
171     }
172 
173     /* (non-Javadoc)
174     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
175     */
176     public List getActiveProfiles()
177         throws ProfileActivationException
178     {
179         DefaultProfileActivationContext context = new DefaultProfileActivationContext();
180         context.setActiveProfileIds( activatedIds );
181         context.setInactiveProfileIds( deactivatedIds );
182         context.setSystemProperties( System.getProperties() );
183         context.setUserProperties( requestProperties );
184 
185         final List<ProfileActivationException> errors = new ArrayList<>();
186 
187         List<Profile> profiles =
188             profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
189             {
190 
191                 public void add( ModelProblemCollectorRequest req )
192                 {
193                     if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
194                     {
195                         errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
196                     }
197                 }
198             } );
199 
200         if ( !errors.isEmpty() )
201         {
202             throw errors.get( 0 );
203         }
204 
205         return profiles;
206     }
207 
208     /* (non-Javadoc)
209      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
210      */
211     public void addProfiles( List<Profile> profiles )
212     {
213         for ( Profile profile1 : profiles )
214         {
215             addProfile( profile1 );
216         }
217     }
218 
219     public void activateAsDefault( String profileId )
220     {
221         if ( !defaultIds.contains( profileId ) )
222         {
223             defaultIds.add( profileId );
224         }
225     }
226 
227     public List<String> getExplicitlyActivatedIds()
228     {
229         return activatedIds;
230     }
231 
232     public List<String>  getExplicitlyDeactivatedIds()
233     {
234         return deactivatedIds;
235     }
236 
237     public List getIdsActivatedByDefault()
238     {
239         return defaultIds;
240     }
241 
242 }