View Javadoc
1   package org.codehaus.plexus.interpolation.os;
2   
3   /*
4    * Copyright 2001-2008 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.util.Locale;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  /**
25   * <b>NOTE:</b> This class was copied from plexus-utils, to allow this library
26   * to stand completely self-contained.
27   *
28   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l </a>
29   */
30  public final class OperatingSystemUtils {
31  
32      private static EnvVarSource envVarSource = new DefaultEnvVarSource();
33  
34      private OperatingSystemUtils() {}
35  
36      public static Properties getSystemEnvVars() throws IOException {
37          return getSystemEnvVars(true);
38      }
39  
40      /**
41       * Return the shell environment variables. If <code>caseSensitive == true</code>, then envar
42       * keys will all be upper-case.
43       *
44       * @param caseSensitive Whether environment variable keys should be treated case-sensitively.
45       * @return Properties object of (possibly modified) envar keys mapped to their values.
46       * @throws IOException in case of an error.
47       */
48      public static Properties getSystemEnvVars(boolean caseSensitive) throws IOException {
49          Properties envVars = new Properties();
50          Map<String, String> envs = envVarSource.getEnvMap();
51          for (String key : envs.keySet()) {
52              String value = envs.get(key);
53              if (!caseSensitive) {
54                  key = key.toUpperCase(Locale.ENGLISH);
55              }
56              envVars.put(key, value);
57          }
58          return envVars;
59      }
60  
61      /**
62       * Set the source object to load the environment variables from.
63       * Default implementation should suffice. This is mostly for testing.
64       * @param source the EnvVarSource instance that loads the environment variables.
65       *
66       * @since 3.1.2
67       */
68      public static void setEnvVarSource(EnvVarSource source) {
69          envVarSource = source;
70      }
71  
72      /**
73       * Defines the functionality to load a Map of environment variables.
74       *
75       * @since 3.1.2
76       */
77      public interface EnvVarSource {
78          public Map<String, String> getEnvMap();
79      }
80  
81      /**
82       * Default implementation to load environment variables.
83       *
84       * @since 3.1.2
85       */
86      public static class DefaultEnvVarSource implements EnvVarSource {
87  
88          public Map<String, String> getEnvMap() {
89              return System.getenv();
90          }
91      }
92  }