001: package org.tanukisoftware.wrapper.test;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: *
027: *
028: * Portions of the Software have been derived from source code
029: * developed by Silver Egg Technology under the following license:
030: *
031: * Copyright (c) 2001 Silver Egg Technology
032: *
033: * Permission is hereby granted, free of charge, to any person
034: * obtaining a copy of this software and associated documentation
035: * files (the "Software"), to deal in the Software without
036: * restriction, including without limitation the rights to use,
037: * copy, modify, merge, publish, distribute, sub-license, and/or
038: * sell copies of the Software, and to permit persons to whom the
039: * Software is furnished to do so, subject to the following
040: * conditions:
041: *
042: * The above copyright notice and this permission notice shall be
043: * included in all copies or substantial portions of the Software.
044: */
045:
046: import java.util.Properties;
047: import java.io.BufferedReader;
048: import java.io.InputStreamReader;
049: import java.io.IOException;
050:
051: import org.tanukisoftware.wrapper.WrapperManager;
052:
053: /**
054: *
055: *
056: * @author Leif Mortenson <leif@tanukisoftware.com>
057: */
058: public class EnvironmentVariables {
059:
060: private static Properties _env = null;
061:
062: /*---------------------------------------------------------------
063: * Main Method
064: *-------------------------------------------------------------*/
065: public static void main(String[] args) {
066: System.out.println("user.language="
067: + System.getProperty("user.language"));
068: System.out.println("user.region="
069: + System.getProperty("user.region"));
070: System.out.println("Locale=" + java.util.Locale.getDefault());
071: System.out.println("Looking for environment variables...");
072:
073: try {
074: getEnvironmentVariables();
075: } catch (IOException e) {
076: System.out.println(e.getMessage());
077: }
078:
079: boolean passed = false;
080:
081: if (_env != null) {
082:
083: System.out.println();
084: passed = check("ENV_VAR_1", "a");
085: passed = check("ENV_VAR_2", "b");
086: passed = check("ENV_VAR_3", "c");
087: passed = check("ENV_VAR_4", "d");
088: System.out.println();
089: }
090:
091: if (passed) {
092: System.out.println("Environment variables test passed.");
093: } else {
094: System.out.println("Environment variables test FAILED.");
095: }
096:
097: System.out.println("Request a JVM restart.");
098: WrapperManager.restart();
099: }
100:
101: private static boolean check(String variable, String expected) {
102:
103: String actual = _env.getProperty(variable);
104:
105: System.out.print(variable + " = " + actual + ": ");
106:
107: if (expected.equals(actual)) {
108: System.out.println("OK");
109: return true;
110: }
111:
112: System.out.println("FAILED (expected: " + expected + ")");
113: return false;
114: }
115:
116: private static void getEnvironmentVariables() throws IOException {
117:
118: String os = System.getProperty("os.name").toLowerCase();
119:
120: System.out.println("Platform is " + os + ".");
121:
122: Process p = null;
123:
124: if (os.indexOf("windows 9") > -1) {
125: p = Runtime.getRuntime().exec("command.com /c set");
126: } else if (os.indexOf("unix") > -1) {
127: p = Runtime.getRuntime().exec("/bin/env");
128: } else if ((os.indexOf("nt") > -1)
129: || (os.indexOf("windows 2000") > -1)
130: || (os.indexOf("windows xp") > -1)
131: || (os.indexOf("windows 2003") > -1)) {
132: p = Runtime.getRuntime().exec("cmd.exe /c set");
133: } else if (os.indexOf("unix") > -1) {
134: p = Runtime.getRuntime().exec("/bin/env");
135: } else if ((os.indexOf("linux") > -1)
136: || (os.indexOf("mac os x") > -1)) {
137: p = Runtime.getRuntime().exec("/usr/bin/env");
138: }
139:
140: if (p == null) {
141: System.out
142: .println("Don't know how to read environment variables on this platform: "
143: + os);
144: return;
145: }
146:
147: _env = new Properties();
148:
149: BufferedReader br = new BufferedReader(new InputStreamReader(p
150: .getInputStream()));
151:
152: String line = null;
153: while ((line = br.readLine()) != null) {
154:
155: int idx = line.indexOf('=');
156:
157: if (idx > -1) {
158: String key = line.substring(0, idx);
159: String value = line.substring(idx + 1);
160:
161: _env.setProperty(key, value);
162: }
163: }
164: }
165: }
|