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