001package org.apache.maven.properties.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Locale;
023import java.util.Map;
024import java.util.Properties;
025
026import org.codehaus.plexus.util.Os;
027
028/**
029 * Assists the project builder. <strong>Warning:</strong> This is an internal utility class that is only public for
030 * technical reasons, it is not part of the public API. In particular, this class can be changed or deleted without
031 * prior notice.
032 *
033 * @since 3.0
034 * @author Benjamin Bentmann
035 */
036public class EnvironmentUtils
037{
038
039    private static Properties envVars;
040
041    /**
042     * Adds the environment variables in the form of properties whose keys are prefixed with {@code env.}, e.g. {@code
043     * env.PATH}. Unlike native environment variables, properties are always case-sensitive. For the sake of
044     * determinism, the environment variable names will be normalized to upper case on platforms with case-insensitive
045     * variable lookup.
046     *
047     * @param props The properties to add the environment variables to, may be {@code null}.
048     */
049    public static void addEnvVars( Properties props )
050    {
051        if ( props != null )
052        {
053            if ( envVars == null )
054            {
055                Properties tmp = new Properties();
056                boolean caseSensitive = !Os.isFamily( Os.FAMILY_WINDOWS );
057                for ( Map.Entry<String, String> entry : System.getenv().entrySet() )
058                {
059                    String key =
060                        "env." + ( caseSensitive ? entry.getKey() : entry.getKey().toUpperCase( Locale.ENGLISH ) );
061                    tmp.setProperty( key, entry.getValue() );
062                }
063                envVars = tmp;
064            }
065
066            props.putAll( envVars );
067        }
068    }
069
070}