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.Iterator;
40  import java.util.LinkedHashMap;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
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 activatedIds = new ArrayList();
57  
58      private List deactivatedIds = new ArrayList();
59  
60      private List defaultIds = new ArrayList();
61  
62      private Map 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 envirnments.
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 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 = (Profile) 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 profileIds )
145     {
146         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
147         {
148             String profileId = (String) it.next();
149 
150             explicitlyActivate( profileId );
151         }
152     }
153 
154     /* (non-Javadoc)
155     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
156     */
157     public void explicitlyDeactivate( String profileId )
158     {
159         if ( !deactivatedIds.contains( profileId ) )
160         {
161             logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." );
162 
163             deactivatedIds.add( profileId );
164         }
165     }
166 
167     /* (non-Javadoc)
168     * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
169     */
170     public void explicitlyDeactivate( List profileIds )
171     {
172         for ( Iterator it = profileIds.iterator(); it.hasNext(); )
173         {
174             String profileId = (String) it.next();
175 
176             explicitlyDeactivate( profileId );
177         }
178     }
179 
180     /* (non-Javadoc)
181     * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
182     */
183     public List getActiveProfiles()
184         throws ProfileActivationException
185     {
186         DefaultProfileActivationContext context = new DefaultProfileActivationContext();
187         context.setActiveProfileIds( activatedIds );
188         context.setInactiveProfileIds( deactivatedIds );
189         context.setSystemProperties( System.getProperties() );
190         context.setUserProperties( requestProperties );
191 
192         final List<ProfileActivationException> errors = new ArrayList<ProfileActivationException>();
193 
194         List<Profile> profiles =
195             profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector()
196             {
197 
198                 public void add( Severity severity, String message, InputLocation location, Exception cause )
199                 {
200                     if ( !ModelProblem.Severity.WARNING.equals( severity ) )
201                     {
202                         errors.add( new ProfileActivationException( message, cause ) );
203                     }
204                 }
205 
206             } );
207 
208         if ( !errors.isEmpty() )
209         {
210             throw errors.get( 0 );
211         }
212 
213         return profiles;
214     }
215 
216     /* (non-Javadoc)
217      * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
218      */
219     public void addProfiles( List profiles )
220     {
221         for ( Iterator it = profiles.iterator(); it.hasNext(); )
222         {
223             Profile profile = (Profile) it.next();
224 
225             addProfile( profile );
226         }
227     }
228 
229     public void activateAsDefault( String profileId )
230     {
231         if ( !defaultIds.contains( profileId ) )
232         {
233             defaultIds.add( profileId );
234         }
235     }
236 
237     public List getExplicitlyActivatedIds()
238     {
239         return activatedIds;
240     }
241 
242     public List getExplicitlyDeactivatedIds()
243     {
244         return deactivatedIds;
245     }
246 
247     public List getIdsActivatedByDefault()
248     {
249         return defaultIds;
250     }
251 
252 }