1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.MuleException;
14
15 import java.io.BufferedReader;
16 import java.io.InputStreamReader;
17 import java.lang.reflect.Method;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.commons.cli.BasicParser;
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.Option;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.ParseException;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31 public class SystemUtils extends org.apache.commons.lang.SystemUtils
32 {
33
34 protected static final Log logger = LogFactory.getLog(SystemUtils.class);
35
36
37
38 private static final String[] UNIX_ENV_PREFIXES = new String[]{"declare -", "typeset -"};
39
40
41 private static Map environment = null;
42
43
44
45
46
47
48
49 public static synchronized Map getenv()
50 {
51 if (environment == null)
52 {
53 try
54 {
55 if (SystemUtils.IS_JAVA_1_4)
56 {
57
58 environment = Collections.unmodifiableMap(getenvJDK14());
59 }
60 else
61 {
62
63
64 Class target = System.class;
65 Method envMethod = target.getMethod("getenv", ArrayUtils.EMPTY_CLASS_ARRAY);
66 environment = Collections.unmodifiableMap((Map) envMethod.invoke(target, (Class[]) null));
67 }
68 }
69 catch (Exception ex)
70 {
71 logger.error("Could not access OS environment: ", ex);
72 environment = Collections.EMPTY_MAP;
73 }
74 }
75
76 return environment;
77 }
78
79 private static Map getenvJDK14() throws Exception
80 {
81 Map env = new HashMap();
82 Process process = null;
83
84 try
85 {
86 boolean isUnix = true;
87 String command;
88
89 if (SystemUtils.IS_OS_WINDOWS)
90 {
91 command = "cmd /c set";
92 isUnix = false;
93 }
94 else
95 {
96 command = "env";
97 }
98
99 process = Runtime.getRuntime().exec(command);
100 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
101
102 String line;
103 while ((line = br.readLine()) != null)
104 {
105 for (int prefix = 0; prefix < UNIX_ENV_PREFIXES.length; prefix++)
106 {
107 if (line.startsWith(UNIX_ENV_PREFIXES[prefix]))
108 {
109 line = line.substring(UNIX_ENV_PREFIXES[prefix].length());
110 }
111 }
112
113 int index = -1;
114 if ((index = line.indexOf('=')) > -1)
115 {
116 String key = line.substring(0, index).trim();
117 String value = line.substring(index + 1).trim();
118
119 if (isUnix && value.length() > 1 && (value.startsWith("\"") || value.startsWith("'")))
120 {
121 value = value.substring(1, value.length() - 1);
122 }
123 env.put(key, value);
124 }
125 else
126 {
127 env.put(line, StringUtils.EMPTY);
128 }
129 }
130 }
131 catch (Exception e)
132 {
133 throw e;
134 }
135 finally
136 {
137 if (process != null)
138 {
139 process.destroy();
140 }
141 }
142
143 return env;
144 }
145
146 public static String getenv(String name)
147 {
148 return (String) SystemUtils.getenv().get(name);
149 }
150
151 public static boolean isSunJDK()
152 {
153 return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("SUN") != -1;
154 }
155
156 public static boolean isIbmJDK()
157 {
158 return SystemUtils.JAVA_VM_VENDOR.toUpperCase().indexOf("IBM") != -1;
159 }
160
161 private static CommandLine parseCommandLine(String args[], String opts[][]) throws MuleException
162 {
163 Options options = new Options();
164 for (int i = 0; i < opts.length; i++)
165 {
166 options.addOption(opts[i][0], opts[i][1].equals("true") ? true : false, opts[i][2]);
167 }
168
169 BasicParser parser = new BasicParser();
170
171 try
172 {
173 CommandLine line = parser.parse(options, args, true);
174 if (line == null)
175 {
176 throw new MuleException("Unknown error parsing the Mule command line");
177 }
178
179 return line;
180 }
181 catch (ParseException p)
182 {
183 throw new MuleException("Unable to parse the Mule command line because of: " + p.toString(), p);
184 }
185 }
186
187
188
189
190
191
192 public static String getCommandLineOption(String option, String args[], String opts[][])
193 throws MuleException
194 {
195 CommandLine line = parseCommandLine(args, opts);
196 return line.getOptionValue(option);
197 }
198
199
200
201
202
203
204 public static boolean hasCommandLineOption(String option, String args[], String opts[][])
205 throws MuleException
206 {
207 CommandLine line = parseCommandLine(args, opts);
208 return line.hasOption(option);
209 }
210
211
212
213
214
215
216 public static Map getCommandLineOptions(String args[], String opts[][]) throws MuleException
217 {
218 CommandLine line = parseCommandLine(args, opts);
219 Map ret = new HashMap();
220 Option[] options = line.getOptions();
221
222 for (int i = 0; i < options.length; i++)
223 {
224 Option option = options[i];
225 ret.put(option.getOpt(), option.getValue("true"));
226 }
227
228 return ret;
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 public static Map parsePropertyDefinitions(String input)
243 {
244 if (StringUtils.isEmpty(input))
245 {
246 return Collections.EMPTY_MAP;
247 }
248
249
250 final Map result = new HashMap();
251
252
253 int tokenStart = 0;
254
255
256 findtoken : while (tokenStart < input.length())
257 {
258
259 tokenStart = StringUtils.indexOf(input, "-D", tokenStart);
260 if (tokenStart == StringUtils.INDEX_NOT_FOUND)
261 {
262 break findtoken;
263 }
264 else
265 {
266
267 tokenStart += 2;
268 }
269
270
271 int keyStart = tokenStart;
272 int keyEnd = keyStart;
273
274 if (keyStart == input.length())
275 {
276
277 break;
278 }
279
280
281 char cursor = input.charAt(keyStart);
282
283
284 if (cursor == ' ')
285 {
286 continue findtoken;
287 }
288
289
290 if (cursor == '=')
291 {
292
293 tokenStart = StringUtils.indexOf(input, ' ', tokenStart);
294 if (tokenStart != StringUtils.INDEX_NOT_FOUND)
295 {
296
297 continue findtoken;
298 }
299 else
300 {
301
302 break findtoken;
303 }
304 }
305
306
307 findkey : while (keyEnd < input.length())
308 {
309 cursor = input.charAt(keyEnd);
310
311
312 if (cursor == ' ')
313 {
314 tokenStart = keyEnd;
315 break findkey;
316 }
317
318
319 if (cursor == '=')
320 {
321 break findkey;
322 }
323
324
325 keyEnd++;
326 }
327
328
329 String key = StringUtils.substring(input, keyStart, keyEnd);
330
331
332 int valueStart = keyEnd;
333 int valueEnd = keyEnd;
334
335
336 String value = "true";
337
338
339 if (keyEnd < input.length() && cursor != ' ')
340 {
341
342 valueStart = keyEnd + 1;
343 valueEnd = valueStart;
344
345
346 cursor = input.charAt(valueStart);
347 if (cursor == '"')
348 {
349
350 valueEnd = StringUtils.indexOf(input, '"', ++valueStart);
351 }
352 else
353 {
354
355 valueEnd = StringUtils.indexOf(input, ' ', valueStart);
356 }
357
358
359 if (valueEnd == StringUtils.INDEX_NOT_FOUND)
360 {
361 valueEnd = input.length();
362 }
363
364
365 value = StringUtils.substring(input, valueStart, valueEnd);
366 }
367
368
369 result.put(key, value);
370
371
372 tokenStart = valueEnd;
373 }
374
375 return result;
376 }
377
378 }