View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.profile;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import static java.util.stream.Collectors.collectingAndThen;
28  import static java.util.stream.Collectors.toMap;
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 implements ProfileActivationContext {
36  
37      private List<String> activeProfileIds = Collections.emptyList();
38  
39      private List<String> inactiveProfileIds = Collections.emptyList();
40  
41      private Map<String, String> systemProperties = Collections.emptyMap();
42  
43      private Map<String, String> userProperties = Collections.emptyMap();
44  
45      private Map<String, String> projectProperties = Collections.emptyMap();
46  
47      private File projectDirectory;
48  
49      @Override
50      public List<String> getActiveProfileIds() {
51          return activeProfileIds;
52      }
53  
54      /**
55       * Sets the identifiers of those profiles that should be activated by explicit demand.
56       *
57       * @param activeProfileIds The identifiers of those profiles to activate, may be {@code null}.
58       * @return This context, never {@code null}.
59       */
60      public DefaultProfileActivationContext setActiveProfileIds(List<String> activeProfileIds) {
61          if (activeProfileIds != null) {
62              this.activeProfileIds = Collections.unmodifiableList(activeProfileIds);
63          } else {
64              this.activeProfileIds = Collections.emptyList();
65          }
66  
67          return this;
68      }
69  
70      @Override
71      public List<String> getInactiveProfileIds() {
72          return inactiveProfileIds;
73      }
74  
75      /**
76       * Sets the identifiers of those profiles that should be deactivated by explicit demand.
77       *
78       * @param inactiveProfileIds The identifiers of those profiles to deactivate, may be {@code null}.
79       * @return This context, never {@code null}.
80       */
81      public DefaultProfileActivationContext setInactiveProfileIds(List<String> inactiveProfileIds) {
82          if (inactiveProfileIds != null) {
83              this.inactiveProfileIds = Collections.unmodifiableList(inactiveProfileIds);
84          } else {
85              this.inactiveProfileIds = Collections.emptyList();
86          }
87  
88          return this;
89      }
90  
91      @Override
92      public Map<String, String> getSystemProperties() {
93          return systemProperties;
94      }
95  
96      /**
97       * Sets the system properties to use for interpolation and profile activation. The system properties are collected
98       * from the runtime environment like {@link System#getProperties()} and environment variables.
99       *
100      * @param systemProperties The system properties, may be {@code null}.
101      * @return This context, never {@code null}.
102      */
103     @SuppressWarnings("unchecked")
104     public DefaultProfileActivationContext setSystemProperties(Properties systemProperties) {
105         if (systemProperties != null) {
106             this.systemProperties = Collections.unmodifiableMap((Map) systemProperties);
107         } else {
108             this.systemProperties = Collections.emptyMap();
109         }
110 
111         return this;
112     }
113 
114     /**
115      * Sets the system properties to use for interpolation and profile activation. The system properties are collected
116      * from the runtime environment like {@link System#getProperties()} and environment variables.
117      *
118      * @param systemProperties The system properties, may be {@code null}.
119      * @return This context, never {@code null}.
120      */
121     public DefaultProfileActivationContext setSystemProperties(Map<String, String> systemProperties) {
122         if (systemProperties != null) {
123             this.systemProperties = Collections.unmodifiableMap(systemProperties);
124         } else {
125             this.systemProperties = Collections.emptyMap();
126         }
127 
128         return this;
129     }
130 
131     @Override
132     public Map<String, String> getUserProperties() {
133         return userProperties;
134     }
135 
136     /**
137      * Sets the user properties to use for interpolation and profile activation. The user properties have been
138      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
139      * line.
140      *
141      * @param userProperties The user properties, may be {@code null}.
142      * @return This context, never {@code null}.
143      */
144     @SuppressWarnings("unchecked")
145     public DefaultProfileActivationContext setUserProperties(Properties userProperties) {
146         if (userProperties != null) {
147             this.userProperties = Collections.unmodifiableMap((Map) userProperties);
148         } else {
149             this.userProperties = Collections.emptyMap();
150         }
151 
152         return this;
153     }
154 
155     /**
156      * Sets the user properties to use for interpolation and profile activation. The user properties have been
157      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
158      * line.
159      *
160      * @param userProperties The user properties, may be {@code null}.
161      * @return This context, never {@code null}.
162      */
163     public DefaultProfileActivationContext setUserProperties(Map<String, String> userProperties) {
164         if (userProperties != null) {
165             this.userProperties = Collections.unmodifiableMap(userProperties);
166         } else {
167             this.userProperties = Collections.emptyMap();
168         }
169 
170         return this;
171     }
172 
173     @Override
174     public File getProjectDirectory() {
175         return projectDirectory;
176     }
177 
178     /**
179      * Sets the base directory of the current project.
180      *
181      * @param projectDirectory The base directory of the current project, may be {@code null} if profile activation
182      *                         happens in the context of metadata retrieval rather than project building.
183      * @return This context, never {@code null}.
184      */
185     public DefaultProfileActivationContext setProjectDirectory(File projectDirectory) {
186         this.projectDirectory = projectDirectory;
187 
188         return this;
189     }
190 
191     @Override
192     public Map<String, String> getProjectProperties() {
193         return projectProperties;
194     }
195 
196     public DefaultProfileActivationContext setProjectProperties(Properties projectProperties) {
197         if (projectProperties != null) {
198             this.projectProperties = projectProperties.entrySet().stream()
199                     .collect(collectingAndThen(
200                             toMap(e -> String.valueOf(e.getKey()), e -> String.valueOf(e.getValue())),
201                             Collections::unmodifiableMap));
202         } else {
203             this.projectProperties = Collections.emptyMap();
204         }
205 
206         return this;
207     }
208 }