KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > SystemUtils


1 /*
2  * $Id: SystemUtils.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.commons.lang.ArrayUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 // @ThreadSafe
28
public class SystemUtils extends org.apache.commons.lang.SystemUtils
29 {
30     protected static final Log logger = LogFactory.getLog(SystemUtils.class);
31
32     // bash prepends: declare -x
33
// zsh prepends: typeset -x
34
private static final String JavaDoc[] UNIX_ENV_PREFIXES = new String JavaDoc[]{"declare -", "typeset -"};
35
36     /**
37      * Get the operating system environment variables. This should work for Windows
38      * and Linux.
39      *
40      * @return Map<String, String> or an empty map if there was an error.
41      */

42     public static synchronized Map JavaDoc getenv()
43     {
44         Map JavaDoc env = Collections./* <String, String> */EMPTY_MAP;
45
46         try
47         {
48             if (SystemUtils.IS_JAVA_1_4)
49             {
50                 // fallback to external process
51
env = getenvJDK14();
52             }
53             else
54             {
55                 // the following runaround is necessary since we still want to
56
// compile on JDK 1.4
57
Class JavaDoc target = System JavaDoc.class;
58                 Method JavaDoc envMethod = target.getMethod("getenv", ArrayUtils.EMPTY_CLASS_ARRAY);
59                 env = (Map JavaDoc)envMethod.invoke(target, (Class JavaDoc[])null);
60             }
61         }
62         catch (Exception JavaDoc ex)
63         {
64             logger.error("Could not access OS environment: ", ex);
65         }
66
67         return env;
68     }
69
70     private static Map JavaDoc getenvJDK14() throws Exception JavaDoc
71     {
72         Map JavaDoc env = new HashMap JavaDoc();
73         Process JavaDoc process = null;
74
75         try
76         {
77             boolean isUnix = true;
78             String JavaDoc command;
79
80             if (SystemUtils.IS_OS_WINDOWS)
81             {
82                 command = "cmd /c set";
83                 isUnix = false;
84             }
85             else
86             {
87                 command = "env";
88             }
89
90             process = Runtime.getRuntime().exec(command);
91             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(process.getInputStream()));
92
93             String JavaDoc line;
94             while ((line = br.readLine()) != null)
95             {
96                 for (int prefix = 0; prefix < UNIX_ENV_PREFIXES.length; prefix++)
97                 {
98                     if (line.startsWith(UNIX_ENV_PREFIXES[prefix]))
99                     {
100                         line = line.substring(UNIX_ENV_PREFIXES[prefix].length());
101                     }
102                 }
103
104                 int index = -1;
105                 if ((index = line.indexOf('=')) > -1)
106                 {
107                     String JavaDoc key = line.substring(0, index).trim();
108                     String JavaDoc value = line.substring(index + 1).trim();
109                     // remove quotes, if any
110
if (isUnix && value.length() > 1 && (value.startsWith("\"") || value.startsWith("'")))
111                     {
112                         value = value.substring(1, value.length() - 1);
113                     }
114                     env.put(key, value);
115                 }
116                 else
117                 {
118                     env.put(line, StringUtils.EMPTY);
119                 }
120             }
121         }
122         catch (Exception JavaDoc e)
123         {
124             throw e; // bubble up
125
}
126         finally
127         {
128             if (process != null)
129             {
130                 process.destroy();
131             }
132         }
133
134         return env;
135     }
136
137     public static String JavaDoc getenv(String JavaDoc name)
138     {
139         return (String JavaDoc)SystemUtils.getenv().get(name);
140     }
141
142     public static boolean isSunJDK()
143     {
144         return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("SUN") != -1;
145     }
146
147     public static boolean isIbmJDK()
148     {
149         return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("IBM") != -1;
150     }
151
152     /**
153      * Returns the value corresponding to the given option from the command line, for
154      * example if the options are "-config mule-config.xml"
155      * getCommandLineOption("config") would return "mule-config.xml" TODO Replace
156      * this functionality with Apache Commons CLI: see MULE-956
157      */

158     public static String JavaDoc getCommandLineOption(String JavaDoc option, String JavaDoc args[])
159     {
160         List JavaDoc options = Arrays.asList(args);
161         if (options.contains(option))
162         {
163             int i = options.indexOf(option);
164             if (i < options.size() - 1)
165             {
166                 return options.get(i + 1).toString();
167             }
168         }
169         return null;
170     }
171 }
172
Popular Tags