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