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.properties.internal;
20
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import org.codehaus.plexus.util.Os;
26
27 /**
28 * Assists the project builder. <strong>Warning:</strong> This is an internal utility class that is only public for
29 * technical reasons, it is not part of the public API. In particular, this class can be changed or deleted without
30 * prior notice.
31 *
32 * @since 3.0
33 * @author Benjamin Bentmann
34 */
35 public class EnvironmentUtils {
36
37 private static Properties envVars;
38
39 /**
40 * Adds the environment variables in the form of properties whose keys are prefixed with {@code env.}, e.g. {@code
41 * env.PATH}. Unlike native environment variables, properties are always case-sensitive. For the sake of
42 * determinism, the environment variable names will be normalized to upper case on platforms with case-insensitive
43 * variable lookup.
44 *
45 * @param props The properties to add the environment variables to, may be {@code null}.
46 */
47 public static void addEnvVars(Properties props) {
48 if (props != null) {
49 if (envVars == null) {
50 Properties tmp = new Properties();
51 boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS);
52 for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
53 String key = "env."
54 + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH));
55 tmp.setProperty(key, entry.getValue());
56 }
57 envVars = tmp;
58 }
59
60 props.putAll(envVars);
61 }
62 }
63 }