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