PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/ivertex-java-service-wrapper-f24e5cb9a234/src/java/org/tanukisoftware/wrapper/test/EnvironmentVariables.java

#
Java | 159 lines | 79 code | 30 blank | 50 comment | 23 complexity | 9e63ba7d8ddb2814f8d9a6d8b9d4fe53 MD5 | raw file
  1. package org.tanukisoftware.wrapper.test;
  2. /*
  3. * Copyright (c) 1999, 2007 Tanuki Software Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of the Java Service Wrapper and associated
  7. * documentation files (the "Software"), to deal in the Software
  8. * without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sub-license,
  10. * and/or sell copies of the Software, and to permit persons to
  11. * whom the Software is furnished to do so, subject to the
  12. * following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *
  27. * Portions of the Software have been derived from source code
  28. * developed by Silver Egg Technology under the following license:
  29. *
  30. * Copyright (c) 2001 Silver Egg Technology
  31. *
  32. * Permission is hereby granted, free of charge, to any person
  33. * obtaining a copy of this software and associated documentation
  34. * files (the "Software"), to deal in the Software without
  35. * restriction, including without limitation the rights to use,
  36. * copy, modify, merge, publish, distribute, sub-license, and/or
  37. * sell copies of the Software, and to permit persons to whom the
  38. * Software is furnished to do so, subject to the following
  39. * conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be
  42. * included in all copies or substantial portions of the Software.
  43. */
  44. import java.util.Properties;
  45. import java.io.BufferedReader;
  46. import java.io.InputStreamReader;
  47. import java.io.IOException;
  48. import org.tanukisoftware.wrapper.WrapperManager;
  49. /**
  50. *
  51. *
  52. * @author Leif Mortenson <leif@tanukisoftware.com>
  53. */
  54. public class EnvironmentVariables {
  55. private static Properties _env = null;
  56. /*---------------------------------------------------------------
  57. * Main Method
  58. *-------------------------------------------------------------*/
  59. public static void main(String[] args) {
  60. System.out.println("user.language=" + System.getProperty("user.language"));
  61. System.out.println("user.region=" + System.getProperty("user.region"));
  62. System.out.println("Locale=" + java.util.Locale.getDefault());
  63. System.out.println("Looking for environment variables...");
  64. try {
  65. getEnvironmentVariables();
  66. } catch (IOException e) {
  67. System.out.println(e.getMessage());
  68. }
  69. boolean passed = false;
  70. if (_env != null) {
  71. System.out.println();
  72. passed = check("ENV_VAR_1", "a");
  73. passed = check("ENV_VAR_2", "b");
  74. passed = check("ENV_VAR_3", "c");
  75. passed = check("ENV_VAR_4", "d");
  76. System.out.println();
  77. }
  78. if (passed) {
  79. System.out.println("Environment variables test passed.");
  80. } else {
  81. System.out.println("Environment variables test FAILED.");
  82. }
  83. System.out.println("Request a JVM restart.");
  84. WrapperManager.restart();
  85. }
  86. private static boolean check(String variable, String expected) {
  87. String actual = _env.getProperty(variable);
  88. System.out.print(variable + " = " + actual + ": ");
  89. if (expected.equals(actual)) {
  90. System.out.println("OK");
  91. return true;
  92. }
  93. System.out.println("FAILED (expected: " + expected + ")");
  94. return false;
  95. }
  96. private static void getEnvironmentVariables() throws IOException {
  97. String os = System.getProperty("os.name").toLowerCase();
  98. System.out.println("Platform is " + os + ".");
  99. Process p = null;
  100. if (os.indexOf("windows 9") > -1) {
  101. p = Runtime.getRuntime().exec("command.com /c set");
  102. } else if (os.indexOf("unix") > -1) {
  103. p = Runtime.getRuntime().exec("/bin/env");
  104. } else if ((os.indexOf("nt") > -1) || (os.indexOf("windows 2000") > -1)
  105. || (os.indexOf("windows xp") > -1) || (os.indexOf("windows 2003") > -1) ) {
  106. p = Runtime.getRuntime().exec("cmd.exe /c set");
  107. } else if (os.indexOf("unix") > -1) {
  108. p = Runtime.getRuntime().exec("/bin/env");
  109. } else if ((os.indexOf("linux") > -1) || (os.indexOf("mac os x") > -1)) {
  110. p = Runtime.getRuntime().exec("/usr/bin/env");
  111. }
  112. if (p == null) {
  113. System.out.println(
  114. "Don't know how to read environment variables on this platform: " + os);
  115. return;
  116. }
  117. _env = new Properties();
  118. BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
  119. String line = null;
  120. while ((line = br.readLine()) != null) {
  121. int idx = line.indexOf('=');
  122. if (idx > -1) {
  123. String key = line.substring(0, idx);
  124. String value = line.substring(idx + 1);
  125. _env.setProperty(key, value);
  126. }
  127. }
  128. }
  129. }