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