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