1 package org.tanukisoftware.wrapper.test; 2 3 45 46 import java.util.Properties ; 47 import java.io.BufferedReader ; 48 import java.io.InputStreamReader ; 49 import java.io.IOException ; 50 51 import org.tanukisoftware.wrapper.WrapperManager; 52 53 58 public class EnvironmentVariables { 59 60 private static Properties _env = null; 61 62 65 public static void main(String [] args) { 66 System.out.println("user.language=" + System.getProperty("user.language")); 67 System.out.println("user.region=" + System.getProperty("user.region")); 68 System.out.println("Locale=" + java.util.Locale.getDefault()); 69 System.out.println("Looking for environment variables..."); 70 71 try { 72 getEnvironmentVariables(); 73 } catch (IOException e) { 74 System.out.println(e.getMessage()); 75 } 76 77 boolean passed = false; 78 79 if (_env != null) { 80 81 System.out.println(); 82 passed = check("ENV_VAR_1", "a"); 83 passed = check("ENV_VAR_2", "b"); 84 passed = check("ENV_VAR_3", "c"); 85 passed = check("ENV_VAR_4", "d"); 86 System.out.println(); 87 } 88 89 if (passed) { 90 System.out.println("Environment variables test passed."); 91 } else { 92 System.out.println("Environment variables test FAILED."); 93 } 94 95 System.out.println("Request a JVM restart."); 96 WrapperManager.restart(); 97 } 98 99 private static boolean check(String variable, String expected) { 100 101 String actual = _env.getProperty(variable); 102 103 System.out.print(variable + " = " + actual + ": "); 104 105 if (expected.equals(actual)) { 106 System.out.println("OK"); 107 return true; 108 } 109 110 System.out.println("FAILED (expected: " + expected + ")"); 111 return false; 112 } 113 114 private static void getEnvironmentVariables() throws IOException { 115 116 String os = System.getProperty("os.name").toLowerCase(); 117 118 System.out.println("Platform is " + os + "."); 119 120 Process p = null; 121 122 if (os.indexOf("windows 9") > -1) { 123 p = Runtime.getRuntime().exec("command.com /c set"); 124 } else if (os.indexOf("unix") > -1) { 125 p = Runtime.getRuntime().exec("/bin/env"); 126 } else if ((os.indexOf("nt") > -1) || (os.indexOf("windows 2000") > -1) 127 || (os.indexOf("windows xp") > -1) || (os.indexOf("windows 2003") > -1) ) { 128 p = Runtime.getRuntime().exec("cmd.exe /c set"); 129 } else if (os.indexOf("unix") > -1) { 130 p = Runtime.getRuntime().exec("/bin/env"); 131 } else if ((os.indexOf("linux") > -1) || (os.indexOf("mac os x") > -1)) { 132 p = Runtime.getRuntime().exec("/usr/bin/env"); 133 } 134 135 if (p == null) { 136 System.out.println( 137 "Don't know how to read environment variables on this platform: " + os); 138 return; 139 } 140 141 _env = new Properties (); 142 143 BufferedReader br=new BufferedReader (new InputStreamReader (p.getInputStream())); 144 145 String line = null; 146 while ((line = br.readLine()) != null) { 147 148 int idx = line.indexOf('='); 149 150 if (idx > -1) { 151 String key = line.substring(0, idx); 152 String value = line.substring(idx + 1); 153 154 _env.setProperty(key, value); 155 } 156 } 157 } 158 } 159 160 | Popular Tags |