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