View Javadoc
1   package org.apache.maven.model.profile;
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 java.io.File;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  /**
31   * Describes the environmental context used to determine the activation status of profiles.
32   *
33   * @author Benjamin Bentmann
34   */
35  public class DefaultProfileActivationContext
36      implements ProfileActivationContext
37  {
38  
39      private List<String> activeProfileIds = Collections.emptyList();
40  
41      private List<String> inactiveProfileIds = Collections.emptyList();
42  
43      private Map<String, String> systemProperties = Collections.emptyMap();
44  
45      private Map<String, String> userProperties = Collections.emptyMap();
46  
47      private Map<String, String> projectProperties = Collections.emptyMap();
48  
49      private File projectDirectory;
50  
51      public List<String> getActiveProfileIds()
52      {
53          return activeProfileIds;
54      }
55  
56      /**
57       * Sets the identifiers of those profiles that should be activated by explicit demand.
58       *
59       * @param activeProfileIds The identifiers of those profiles to activate, may be {@code null}.
60       * @return This context, never {@code null}.
61       */
62      public DefaultProfileActivationContext setActiveProfileIds( List<String> activeProfileIds )
63      {
64          if ( activeProfileIds != null )
65          {
66              this.activeProfileIds = Collections.unmodifiableList( activeProfileIds );
67          }
68          else
69          {
70              this.activeProfileIds = Collections.emptyList();
71          }
72  
73          return this;
74      }
75  
76      public List<String> getInactiveProfileIds()
77      {
78          return inactiveProfileIds;
79      }
80  
81      /**
82       * Sets the identifiers of those profiles that should be deactivated by explicit demand.
83       *
84       * @param inactiveProfileIds The identifiers of those profiles to deactivate, may be {@code null}.
85       * @return This context, never {@code null}.
86       */
87      public DefaultProfileActivationContext setInactiveProfileIds( List<String> inactiveProfileIds )
88      {
89          if ( inactiveProfileIds != null )
90          {
91              this.inactiveProfileIds = Collections.unmodifiableList( inactiveProfileIds );
92          }
93          else
94          {
95              this.inactiveProfileIds = Collections.emptyList();
96          }
97  
98          return this;
99      }
100 
101     public Map<String, String> getSystemProperties()
102     {
103         return systemProperties;
104     }
105 
106     /**
107      * Sets the system properties to use for interpolation and profile activation. The system properties are collected
108      * from the runtime environment like {@link System#getProperties()} and environment variables.
109      *
110      * @param systemProperties The system properties, may be {@code null}.
111      * @return This context, never {@code null}.
112      */
113     @SuppressWarnings( "unchecked" )
114     public DefaultProfileActivationContext setSystemProperties( Properties systemProperties )
115     {
116         if ( systemProperties != null )
117         {
118             this.systemProperties = Collections.unmodifiableMap( (Map) systemProperties );
119         }
120         else
121         {
122             this.systemProperties = Collections.emptyMap();
123         }
124 
125         return this;
126     }
127 
128     /**
129      * Sets the system properties to use for interpolation and profile activation. The system properties are collected
130      * from the runtime environment like {@link System#getProperties()} and environment variables.
131      *
132      * @param systemProperties The system properties, may be {@code null}.
133      * @return This context, never {@code null}.
134      */
135     public DefaultProfileActivationContext setSystemProperties( Map<String, String> systemProperties )
136     {
137         if ( systemProperties != null )
138         {
139             this.systemProperties = Collections.unmodifiableMap( systemProperties );
140         }
141         else
142         {
143             this.systemProperties = Collections.emptyMap();
144         }
145 
146         return this;
147     }
148 
149     public Map<String, String> getUserProperties()
150     {
151         return userProperties;
152     }
153 
154     /**
155      * Sets the user properties to use for interpolation and profile activation. The user properties have been
156      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
157      * line.
158      *
159      * @param userProperties The user properties, may be {@code null}.
160      * @return This context, never {@code null}.
161      */
162     @SuppressWarnings( "unchecked" )
163     public DefaultProfileActivationContext setUserProperties( Properties userProperties )
164     {
165         if ( userProperties != null )
166         {
167             this.userProperties = Collections.unmodifiableMap( (Map) userProperties );
168         }
169         else
170         {
171             this.userProperties = Collections.emptyMap();
172         }
173 
174         return this;
175     }
176 
177     /**
178      * Sets the user properties to use for interpolation and profile activation. The user properties have been
179      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
180      * line.
181      *
182      * @param userProperties The user properties, may be {@code null}.
183      * @return This context, never {@code null}.
184      */
185     public DefaultProfileActivationContext setUserProperties( Map<String, String> userProperties )
186     {
187         if ( userProperties != null )
188         {
189             this.userProperties = Collections.unmodifiableMap( userProperties );
190         }
191         else
192         {
193             this.userProperties = Collections.emptyMap();
194         }
195 
196         return this;
197     }
198 
199     public File getProjectDirectory()
200     {
201         return projectDirectory;
202     }
203 
204     /**
205      * Sets the base directory of the current project.
206      *
207      * @param projectDirectory The base directory of the current project, may be {@code null} if profile activation
208      *                         happens in the context of metadata retrieval rather than project building.
209      * @return This context, never {@code null}.
210      */
211     public DefaultProfileActivationContext setProjectDirectory( File projectDirectory )
212     {
213         this.projectDirectory = projectDirectory;
214 
215         return this;
216     }
217 
218     public Map<String, String> getProjectProperties()
219     {
220         return projectProperties;
221     }
222 
223     public DefaultProfileActivationContext setProjectProperties( Properties projectProperties )
224     {
225         if ( projectProperties != null )
226         {
227 
228             this.projectProperties = Collections.unmodifiableMap( toMap( projectProperties ) );
229         }
230         else
231         {
232             this.projectProperties = Collections.emptyMap();
233         }
234 
235         return this;
236     }
237 
238     private Map<String, String> toMap( Properties properties )
239     {
240         if ( properties == null )
241         {
242             return Collections.emptyMap();
243         }
244         Map<String, String> map = new HashMap<String, String>();
245         Enumeration keys = properties.keys();
246         while ( keys.hasMoreElements() )
247         {
248             String key = (String) keys.nextElement();
249             map.put( key, properties.getProperty( key ) );
250         }
251         return map;
252     }
253 }