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