View Javadoc

1   package org.apache.maven.properties.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import org.codehaus.plexus.util.Os;
27  
28  /**
29   * Assists the project builder. <strong>Warning:</strong> This is an internal utility class that is only public for
30   * technical reasons, it is not part of the public API. In particular, this class can be changed or deleted without
31   * prior notice.
32   * 
33   * @since 3.0
34   * @author Benjamin Bentmann
35   */
36  public class EnvironmentUtils
37  {
38  
39      private static Properties envVars;
40  
41      /**
42       * Adds the environment variables in the form of properties whose keys are prefixed with {@code env.}, e.g. {@code
43       * env.PATH}. Unlike native environment variables, properties are always case-sensitive. For the sake of
44       * determinism, the environment variable names will be normalized to upper case on platforms with case-insensitive
45       * variable lookup.
46       * 
47       * @param props The properties to add the environment variables to, may be {@code null}.
48       */
49      public static void addEnvVars( Properties props )
50      {
51          if ( props != null )
52          {
53              if ( envVars == null )
54              {
55                  Properties tmp = new Properties();
56                  boolean caseSensitive = !Os.isFamily( Os.FAMILY_WINDOWS );
57                  for ( Map.Entry<String, String> entry : System.getenv().entrySet() )
58                  {
59                      String key =
60                          "env." + ( caseSensitive ? entry.getKey() : entry.getKey().toUpperCase( Locale.ENGLISH ) );
61                      tmp.setProperty( key, entry.getValue() );
62                  }
63                  envVars = tmp;
64              }
65  
66              props.putAll( envVars );
67          }
68      }
69  
70  }