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.InputLocation;
24  import org.apache.maven.model.Profile;
25  import org.apache.maven.model.building.ModelProblem;
26  import org.apache.maven.model.building.ModelProblemCollector;
27  import org.apache.maven.model.building.ModelProblem.Severity;
28  import org.apache.maven.model.profile.DefaultProfileActivationContext;
29  import org.apache.maven.model.profile.ProfileActivationContext;
30  import org.apache.maven.model.profile.ProfileSelector;
31  import org.apache.maven.profiles.activation.ProfileActivationException;
32  import org.codehaus.plexus.MutablePlexusContainer;
33  import org.codehaus.plexus.PlexusContainer;
34  import org.codehaus.plexus.component.annotations.Requirement;
35  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
36  import org.codehaus.plexus.logging.Logger;
37  
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.Iterator;
41  import java.util.LinkedHashMap;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Properties;
45  import org.apache.maven.model.building.ModelProblem.Version;
46  import org.apache.maven.model.building.ModelProblemCollectorRequest;
47  
48  @Deprecated
49  public class DefaultProfileManager
50      implements ProfileManager
51  {
52  
53      @Requirement
54      private Logger logger;
55  
56      @Requirement
57      private ProfileSelector profileSelector;
58  
59      private List activatedIds = new ArrayList();
60  
61      private List deactivatedIds = new ArrayList();
62  
63      private List defaultIds = new ArrayList();
64  
65      private Map profilesById = new LinkedHashMap();
66  
67      private Properties requestProperties;
68  
69      /**
70       * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work
71       *             correctly in embedded envirnments.
72       */
73      public DefaultProfileManager( PlexusContainer container )
74      {
75          this( container, null );
76      }
77  
78      /**
79       * the properties passed to the profile manager are the props that
80       * are passed to maven, possibly containing profile activator properties
81       *
82       */
83      public DefaultProfileManager( PlexusContainer container, Properties props )
84      {
85          try
86          {
87              this.profileSelector = container.lookup( ProfileSelector.class );
88              this.logger = ( (MutablePlexusContainer) container ).getLogger();
89          }
90          catch ( ComponentLookupException e )
91          {
92              throw new IllegalStateException( e );
93          }
94          this.requestProperties = props;
95      }
96  
97      public Properties getRequestProperties()
98      {
99          return requestProperties;
100     }
101 
102     public Map getProfilesById()
103     {
104         return profilesById;
105     }
106 
107     /* (non-Javadoc)
108     * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
109     */
110     public void addProfile( Profile profile )
111     {
112         String profileId = profile.getId();
113 
114         Profile existing = (Profile) profilesById.get( profileId );
115         if ( existing != null )
116         {
117             logger.warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource()
118                 + ") with new instance from source: " + profile.getSource() );
119         }
120 
121         profilesById.put( profile.getId(), profile );
122 
123         Activation activation = profile.getActivation();
124 
125         if ( activation != null && activation.isActiveByDefault() )
126         {
127             activateAsDefault( profileId );
128         }
129     }
130 
131     /* (non-Javadoc)
132     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
133     */
134     public void explicitlyActivate( String profileId )
135     {
136         if ( !activatedIds.contains( profileId ) )
137         {
138             logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." );
139 
140             activatedIds.add( profileId );
141         }
142     }
143 
144     /* (non-Javadoc)
145     * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
146     */
147     public void explicitlyActivate( List profileIds )
148     {
149         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
150         {
151             String profileId = (String) it.next();
152 
153             explicitlyActivate( profileId );
154         }
155     }
156 
157     /* (non-Javadoc)
158     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
159     */
160     public void explicitlyDeactivate( String profileId )
161     {
162         if ( !deactivatedIds.contains( profileId ) )
163         {
164             logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
165 
166             deactivatedIds.add( profileId );
167         }
168     }
169 
170     /* (non-Javadoc)
171     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
172     */
173     public void explicitlyDeactivate( List profileIds )
174     {
175         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
176         {
177             String profileId = (String) it.next();
178 
179             explicitlyDeactivate( profileId );
180         }
181     }
182 
183     /* (non-Javadoc)
184     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
185     */
186     public List getActiveProfiles()
187         throws ProfileActivationException
188     {
189         DefaultProfileActivationContext context = new DefaultProfileActivationContext();
190         context.setActiveProfileIds( activatedIds );
191         context.setInactiveProfileIds( deactivatedIds );
192         context.setSystemProperties( System.getProperties() );
193         context.setUserProperties( requestProperties );
194 
195         final List<ProfileActivationException> errors = new ArrayList<ProfileActivationException>();
196 
197         List<Profile> profiles =
198             profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
199             {
200 
201                 public void add( ModelProblemCollectorRequest req )
202                 {
203                     if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) )
204                     {
205                         errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) );
206                     }
207                 }
208             } );
209 
210         if ( !errors.isEmpty() )
211         {
212             throw errors.get( 0 );
213         }
214 
215         return profiles;
216     }
217 
218     /* (non-Javadoc)
219      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
220      */
221     public void addProfiles( List profiles )
222     {
223         for ( Iterator it = profiles.iterator(); it.hasNext(); )
224         {
225             Profile profile = (Profile) it.next();
226 
227             addProfile( profile );
228         }
229     }
230 
231     public void activateAsDefault( String profileId )
232     {
233         if ( !defaultIds.contains( profileId ) )
234         {
235             defaultIds.add( profileId );
236         }
237     }
238 
239     public List getExplicitlyActivatedIds()
240     {
241         return activatedIds;
242     }
243 
244     public List getExplicitlyDeactivatedIds()
245     {
246         return deactivatedIds;
247     }
248 
249     public List getIdsActivatedByDefault()
250     {
251         return defaultIds;
252     }
253 
254 }