Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CommandLineUtils |
|
| 7.0;7 |
1 | package org.apache.maven.plugin.assembly.utils; | |
2 | ||
3 | /* | |
4 | * The MIT License | |
5 | * | |
6 | * Copyright (c) 2004, The Codehaus | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of | |
9 | * this software and associated documentation files (the "Software"), to deal in | |
10 | * the Software without restriction, including without limitation the rights to | |
11 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
12 | * of the Software, and to permit persons to whom the Software is furnished to do | |
13 | * so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in all | |
16 | * copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
27 | import java.io.BufferedReader; | |
28 | import java.io.IOException; | |
29 | import java.io.InputStreamReader; | |
30 | import java.util.Properties; | |
31 | ||
32 | /** | |
33 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl </a> | |
34 | * @version $Id: CommandLineUtils.java 728546 2008-12-21 22:56:51Z bentmann $ | |
35 | */ | |
36 | 0 | public abstract class CommandLineUtils |
37 | { | |
38 | /** | |
39 | * Return the shell environment variables. If <code>caseSensitive == true</code>, then envar | |
40 | * keys will all be upper-case. | |
41 | * | |
42 | * @param caseSensitive Whether environment variable keys should be treated case-sensitively. | |
43 | * @return Properties object of (possibly modified) envar keys mapped to their values. | |
44 | * @throws IOException | |
45 | */ | |
46 | public static Properties getSystemEnvVars( boolean caseSensitive ) | |
47 | throws IOException | |
48 | { | |
49 | 110 | Process p = null; |
50 | ||
51 | 110 | Properties envVars = new Properties(); |
52 | ||
53 | 110 | Runtime r = Runtime.getRuntime(); |
54 | ||
55 | 110 | String os = System.getProperty( "os.name" ).toLowerCase(); |
56 | ||
57 | //If this is windows set the shell to command.com or cmd.exe with correct arguments. | |
58 | 110 | if ( os.indexOf( "windows" ) != -1 ) |
59 | { | |
60 | 0 | if ( os.indexOf( "95" ) != -1 || os.indexOf( "98" ) != -1 || os.indexOf( "Me" ) != -1 ) |
61 | { | |
62 | 0 | p = r.exec( "command.com /c set" ); |
63 | } | |
64 | else | |
65 | { | |
66 | 0 | p = r.exec( "cmd.exe /c set" ); |
67 | } | |
68 | } | |
69 | else | |
70 | { | |
71 | 110 | p = r.exec( "env" ); |
72 | } | |
73 | ||
74 | 110 | BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); |
75 | ||
76 | String line; | |
77 | ||
78 | 110 | String lastKey = null; |
79 | 110 | String lastVal = null; |
80 | ||
81 | 5060 | while ( ( line = br.readLine() ) != null ) |
82 | { | |
83 | 4950 | int idx = line.indexOf( '=' ); |
84 | ||
85 | 4950 | if ( idx > 1 ) |
86 | { | |
87 | 4840 | lastKey = line.substring( 0, idx ); |
88 | ||
89 | 4840 | if ( !caseSensitive ) |
90 | { | |
91 | 4840 | lastKey = lastKey.toUpperCase(); |
92 | } | |
93 | ||
94 | 4840 | lastVal = line.substring( idx + 1 ); |
95 | ||
96 | 4840 | envVars.setProperty( lastKey, lastVal ); |
97 | } | |
98 | 110 | else if ( lastKey != null ) |
99 | { | |
100 | 110 | lastVal += "\n" + line; |
101 | ||
102 | 110 | envVars.setProperty( lastKey, lastVal ); |
103 | } | |
104 | 4950 | } |
105 | ||
106 | 110 | return envVars; |
107 | } | |
108 | ||
109 | } |