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