PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/selenium/com/thoughtworks/seleniumtest/testhelper/OSEnvironment.java

https://gitlab.com/BGCX067/fakevcdstore-svn-to-git
Java | 207 lines | 92 code | 22 blank | 93 comment | 16 complexity | 6b55e06ab5f7347c05cdd2b74a60160b MD5 | raw file
  1. package com.thoughtworks.seleniumtest.testhelper;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Enumeration;
  6. import java.util.List;
  7. import java.util.Properties;
  8. import org.apache.log4j.Logger;
  9. public class OSEnvironment {
  10. private static final Logger LOG = Logger.getLogger(OSEnvironment.class);
  11. /**
  12. * Internal representation of the system environment
  13. */
  14. private Properties variables = new Properties();
  15. /**
  16. * Constructor
  17. *
  18. * Creates an instance of OSEnvironment, queries the OS to discover it's
  19. * environment variables and makes them available through the getter methods
  20. */
  21. public OSEnvironment() {
  22. parse();
  23. }
  24. /**
  25. * Parses the OS environment and makes the environment variables available
  26. * through the getter methods
  27. */
  28. private void parse() {
  29. String command;
  30. // Detemine the correct command to run based on OS name
  31. String os = System.getProperty("os.name").toLowerCase();
  32. if (isWindows9x(os)) {
  33. command = "command.com /c set";
  34. } else if ((os.indexOf("nt") > -1) || (os.indexOf("windows") > -1)
  35. || (os.indexOf("os/2") > -1)) {
  36. command = "cmd.exe /c set";
  37. } else {
  38. // should work for just about any Unix variant
  39. command = "env";
  40. }
  41. // Get our environment
  42. try {
  43. Process p = Runtime.getRuntime().exec(command);
  44. p.getOutputStream().close();
  45. // Capture the output of the command
  46. BufferedReader stdoutStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
  47. BufferedReader stderrStream = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  48. // Parse the output
  49. String line;
  50. String key = null;
  51. while ((line = stdoutStream.readLine()) != null) {
  52. int idx = line.indexOf('=');
  53. String value;
  54. if (idx == -1) {
  55. if (key == null) {
  56. continue;
  57. }
  58. // potential multi-line property. Let's rebuild it
  59. value = variables.getProperty(key);
  60. value += "\n" + line;
  61. } else {
  62. key = line.substring(0, idx);
  63. value = line.substring(idx + 1);
  64. }
  65. variables.setProperty(key, value);
  66. }
  67. // Close down our streams
  68. stdoutStream.close();
  69. stderrStream.close();
  70. } catch (Exception e) {
  71. LOG.error("Failed to parse the OS environment.", e);
  72. }
  73. }
  74. private boolean isWindows9x(String os) {
  75. return os.indexOf("windows 9") > -1;
  76. }
  77. /**
  78. * Gets the value of an environment variable. The variable name is case
  79. * sensitive.
  80. *
  81. * @param variable
  82. * The variable for which you wish the value
  83. *
  84. * @return The value of the variable, or <code>null</code> if not found
  85. *
  86. * @see #getVariable(String variable, String defaultValue)
  87. */
  88. public String getVariable(String variable) {
  89. return variables.getProperty(variable);
  90. }
  91. /**
  92. * Gets the value of an environment variable. The variable name is case
  93. * sensitive.
  94. *
  95. * @param variable
  96. * the variable for which you wish the value
  97. *
  98. * @param defaultValue
  99. * The value to return if the variable is not set in the
  100. * environment.
  101. *
  102. * @return The value of the variable. If the variable is not found, the
  103. * defaultValue is returned.
  104. */
  105. public String getVariable(String variable, String defaultValue) {
  106. return variables.getProperty(variable, defaultValue);
  107. }
  108. /**
  109. * Gets the value of an environment variable. The variable name is NOT case
  110. * sensitive. If more than one variable matches the pattern provided, the
  111. * result is unpredictable. You are greatly encouraged to use
  112. * <code>getVariable()</code> instead.
  113. *
  114. * @param variable
  115. * the variable for which you wish the value
  116. *
  117. * @see #getVariable(String variable)
  118. * @see #getVariable(String variable, String defaultValue)
  119. */
  120. public String getVariableIgnoreCase(String variable) {
  121. Enumeration keys = variables.keys();
  122. while (keys.hasMoreElements()) {
  123. String key = (String) keys.nextElement();
  124. if (key.equalsIgnoreCase(variable)) {
  125. return variables.getProperty(key);
  126. }
  127. }
  128. return null;
  129. }
  130. /**
  131. * Adds a variable to this representation of the environment. If the
  132. * variable already existed, the value will be replaced.
  133. *
  134. * @param variable
  135. * the variable to set
  136. * @param value
  137. * the value of the variable
  138. */
  139. public void add(String variable, String value) {
  140. variables.setProperty(variable, value);
  141. }
  142. /**
  143. * Returns all environment variables which were set at the time the class
  144. * was instantiated, as well as any which have been added programatically.
  145. *
  146. * @return a <code>List</code> of all environment variables. The
  147. * <code>List</code> is made up of <code>String</code>s of the
  148. * form "variable=value".
  149. *
  150. * @see #toArray()
  151. */
  152. public List getEnvironment() {
  153. List env = new ArrayList();
  154. Enumeration keys = variables.keys();
  155. while (keys.hasMoreElements()) {
  156. String key = (String) keys.nextElement();
  157. env.add(key + "=" + variables.getProperty(key));
  158. }
  159. return env;
  160. }
  161. /**
  162. * Returns all environment variables which were set at the time the class
  163. * was instantiated, as well as any which have been added programatically.
  164. *
  165. * @return a <code>String[]</code> containing all environment variables.
  166. * The <code>String</code>s are of the form "variable=value".
  167. * This is the format expected by
  168. * <code>java.lang.Runtime.exec()</code>.
  169. *
  170. * @see java.lang.Runtime
  171. */
  172. public String[] toArray() {
  173. List list = getEnvironment();
  174. return (String[]) list.toArray(new String[list.size()]);
  175. }
  176. /**
  177. * Returns a <code>String<code> representation of the
  178. * environment.
  179. *
  180. * @return A <code>String<code> representation of the environment
  181. */
  182. public String toString() {
  183. return variables.toString();
  184. }
  185. }