KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > test > EnvironmentVariables


1 package org.tanukisoftware.wrapper.test;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  *
28  * Portions of the Software have been derived from source code
29  * developed by Silver Egg Technology under the following license:
30  *
31  * Copyright (c) 2001 Silver Egg Technology
32  *
33  * Permission is hereby granted, free of charge, to any person
34  * obtaining a copy of this software and associated documentation
35  * files (the "Software"), to deal in the Software without
36  * restriction, including without limitation the rights to use,
37  * copy, modify, merge, publish, distribute, sub-license, and/or
38  * sell copies of the Software, and to permit persons to whom the
39  * Software is furnished to do so, subject to the following
40  * conditions:
41  *
42  * The above copyright notice and this permission notice shall be
43  * included in all copies or substantial portions of the Software.
44  */

45
46 import java.util.Properties JavaDoc;
47 import java.io.BufferedReader JavaDoc;
48 import java.io.InputStreamReader JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 import org.tanukisoftware.wrapper.WrapperManager;
52
53 /**
54  *
55  *
56  * @author Leif Mortenson <leif@tanukisoftware.com>
57  */

58 public class EnvironmentVariables {
59
60     private static Properties JavaDoc _env = null;
61     
62     /*---------------------------------------------------------------
63      * Main Method
64      *-------------------------------------------------------------*/

65     public static void main(String JavaDoc[] 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 JavaDoc 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 JavaDoc variable, String JavaDoc expected) {
100         
101         String JavaDoc 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 JavaDoc {
115         
116         String JavaDoc os = System.getProperty("os.name").toLowerCase();
117         
118         System.out.println("Platform is " + os + ".");
119         
120         Process JavaDoc 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 JavaDoc();
142         
143         BufferedReader JavaDoc br=new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
144         
145         String JavaDoc line = null;
146         while ((line = br.readLine()) != null) {
147             
148             int idx = line.indexOf('=');
149             
150             if (idx > -1) {
151                 String JavaDoc key = line.substring(0, idx);
152                 String JavaDoc value = line.substring(idx + 1);
153                 
154                 _env.setProperty(key, value);
155             }
156         }
157     }
158 }
159
160
Popular Tags