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