GetEnviroment.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * MCS Media Computer Software Copyright (c) 2005 by MCS
  3. * -------------------------------------- Created on 16.01.2004 by w.klaas
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. package de.mcs.utils;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.util.HashMap;
  22. /**
  23. * Environment class simulates the System.getenv() method which is deprecated on
  24. * java 1.4.2.
  25. *
  26. * @author v-josp
  27. */
  28. public final class GetEnviroment {
  29. /** prevent instancing. */
  30. private GetEnviroment() {
  31. }
  32. /** result of all enviornment variables. */
  33. private static BufferedReader commandResult;
  34. /** map with all commands. */
  35. private static HashMap<String, String> commandList;
  36. static {
  37. String cmd = null;
  38. String os = null;
  39. // getting the OS name
  40. os = System.getProperty("os.name").toLowerCase();
  41. // according to OS set the command to execute
  42. if (os.startsWith("windows")) {
  43. cmd = "cmd /c SET";
  44. } else {
  45. cmd = "env";
  46. }
  47. try {
  48. // execute the command and get the result in the form of InputStream
  49. Process p = Runtime.getRuntime().exec(cmd);
  50. // parse the InputStream data
  51. InputStreamReader isr = new InputStreamReader(p.getInputStream());
  52. commandResult = new BufferedReader(isr);
  53. } catch (Exception e) {
  54. System.out.println("OSEnvironment.class error: " + cmd + ":" + e);
  55. }
  56. commandList = new HashMap<String, String>();
  57. String line = null;
  58. try {
  59. while ((line = commandResult.readLine()) != null) {
  60. String key = line.substring(0, line.indexOf('='));
  61. String value = line.substring(line.indexOf('=') + 1);
  62. commandList.put(key, value);
  63. }
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. /**
  69. * This method is used to get the path of the given enviornment variable.
  70. * This method tries to simulates the System.getenv() which is deprecated on
  71. * java 1.4.2
  72. *
  73. * @param envName
  74. * name of the environment variable
  75. * @param defaultValue
  76. * default value
  77. * @return String
  78. */
  79. public static String getenv(final String envName, final String defaultValue) {
  80. String value = defaultValue;
  81. if (commandList.containsKey(envName)) {
  82. value = (String) commandList.get(envName);
  83. }
  84. return value;
  85. }
  86. }