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  import java.util.stream.Collectors;
27  
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          this.activeProfileIds = unmodifiable(activeProfileIds);
62          return this;
63      }
64  
65      @Override
66      public List<String> getInactiveProfileIds() {
67          return inactiveProfileIds;
68      }
69  
70      /**
71       * Sets the identifiers of those profiles that should be deactivated by explicit demand.
72       *
73       * @param inactiveProfileIds The identifiers of those profiles to deactivate, may be {@code null}.
74       * @return This context, never {@code null}.
75       */
76      public DefaultProfileActivationContext setInactiveProfileIds(List<String> inactiveProfileIds) {
77          this.inactiveProfileIds = unmodifiable(inactiveProfileIds);
78          return this;
79      }
80  
81      @Override
82      public Map<String, String> getSystemProperties() {
83          return systemProperties;
84      }
85  
86      /**
87       * Sets the system properties to use for interpolation and profile activation. The system properties are collected
88       * from the runtime environment like {@link System#getProperties()} and environment variables.
89       *
90       * @param systemProperties The system properties, may be {@code null}.
91       * @return This context, never {@code null}.
92       */
93      @SuppressWarnings("unchecked")
94      public DefaultProfileActivationContext setSystemProperties(Properties systemProperties) {
95          return setSystemProperties(toMap(systemProperties));
96      }
97  
98      /**
99       * Sets the system properties to use for interpolation and profile activation. The system properties are collected
100      * from the runtime environment like {@link System#getProperties()} and environment variables.
101      *
102      * @param systemProperties The system properties, may be {@code null}.
103      * @return This context, never {@code null}.
104      */
105     public DefaultProfileActivationContext setSystemProperties(Map<String, String> systemProperties) {
106         this.systemProperties = unmodifiable(systemProperties);
107         return this;
108     }
109 
110     @Override
111     public Map<String, String> getUserProperties() {
112         return userProperties;
113     }
114 
115     /**
116      * Sets the user properties to use for interpolation and profile activation. The user properties have been
117      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
118      * line.
119      *
120      * @param userProperties The user properties, may be {@code null}.
121      * @return This context, never {@code null}.
122      */
123     @SuppressWarnings("unchecked")
124     public DefaultProfileActivationContext setUserProperties(Properties userProperties) {
125         return setUserProperties(toMap(userProperties));
126     }
127 
128     /**
129      * Sets the user properties to use for interpolation and profile activation. The user properties have been
130      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
131      * line.
132      *
133      * @param userProperties The user properties, may be {@code null}.
134      * @return This context, never {@code null}.
135      */
136     public DefaultProfileActivationContext setUserProperties(Map<String, String> userProperties) {
137         this.userProperties = unmodifiable(userProperties);
138         return this;
139     }
140 
141     @Override
142     public File getProjectDirectory() {
143         return projectDirectory;
144     }
145 
146     /**
147      * Sets the base directory of the current project.
148      *
149      * @param projectDirectory The base directory of the current project, may be {@code null} if profile activation
150      *                         happens in the context of metadata retrieval rather than project building.
151      * @return This context, never {@code null}.
152      */
153     public DefaultProfileActivationContext setProjectDirectory(File projectDirectory) {
154         this.projectDirectory = projectDirectory;
155 
156         return this;
157     }
158 
159     @Override
160     public Map<String, String> getProjectProperties() {
161         return projectProperties;
162     }
163 
164     public DefaultProfileActivationContext setProjectProperties(Properties projectProperties) {
165         return setProjectProperties(toMap(projectProperties));
166     }
167 
168     public DefaultProfileActivationContext setProjectProperties(Map<String, String> projectProperties) {
169         this.projectProperties = unmodifiable(projectProperties);
170 
171         return this;
172     }
173 
174     private static List<String> unmodifiable(List<String> list) {
175         return list != null ? Collections.unmodifiableList(list) : Collections.emptyList();
176     }
177 
178     private static Map<String, String> unmodifiable(Map<String, String> map) {
179         return map != null ? Collections.unmodifiableMap(map) : Collections.emptyMap();
180     }
181 
182     private static Map<String, String> toMap(Properties properties) {
183         if (properties != null && !properties.isEmpty()) {
184             return properties.entrySet().stream()
185                     .collect(Collectors.toMap(e -> String.valueOf(e.getKey()), e -> String.valueOf(e.getValue())));
186 
187         } else {
188             return null;
189         }
190     }
191 }