20
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
21
21
*/
22
22
package j9vm .runner ;
23
- import java .io .*;
24
- import java .util .Properties ;
25
- import java .util .Iterator ;
23
+
24
+ import java .io .BufferedInputStream ;
26
25
import java .util .Map ;
27
- import java .lang . reflect . Method ;
26
+ import java .util . Properties ;
28
27
29
28
public class Runner {
30
-
31
- protected enum OSName {
29
+
30
+ protected static enum OSName {
32
31
AIX ,
33
32
LINUX ,
33
+ MAC ,
34
34
WINDOWS ,
35
35
ZOS ,
36
- MAC ,
37
- UNKNOWN
36
+ UNKNOWN ;
37
+
38
+ public static OSName current () {
39
+ String osSpec = System .getProperty ("os.name" , "?" ).toLowerCase ();
40
+
41
+ /* Get OS from the spec string. */
42
+ if (osSpec .contains ("aix" )) {
43
+ return AIX ;
44
+ } else if (osSpec .contains ("linux" )) {
45
+ return LINUX ;
46
+ } else if (osSpec .contains ("mac" )) {
47
+ return MAC ;
48
+ } else if (osSpec .contains ("windows" )) {
49
+ return WINDOWS ;
50
+ } else if (osSpec .contains ("z/os" )) {
51
+ return ZOS ;
52
+ } else {
53
+ System .out .println ("Runner couldn't determine underlying OS. Got OS Name:" + osSpec );
54
+ return UNKNOWN ;
55
+ }
56
+ }
38
57
}
39
-
40
- protected enum OSArch {
58
+
59
+ protected static enum OSArch {
60
+ AARCH64 ,
41
61
PPC ,
42
62
S390X ,
43
63
X86 ,
44
- UNKNOWN
45
- }
46
-
47
- protected enum AddrMode {
48
- BIT31 ,
49
- BIT32 ,
50
- BIT64 ,
51
- UNKNOWN
52
- }
53
-
54
- public static final String systemPropertyPrefix = "j9vm." ;
64
+ UNKNOWN ;
55
65
56
- protected String className ;
57
- protected String exeName ;
58
- protected String bootClassPath ;
59
- protected String userClassPath ;
60
- protected String javaVersion ;
61
- protected OutputCollector inCollector ;
62
- protected OutputCollector errCollector ;
63
- protected OSName osName = OSName .UNKNOWN ;
64
- protected OSArch osArch = OSArch .UNKNOWN ;
65
- protected AddrMode addrMode = AddrMode .UNKNOWN ;
66
-
67
- private final String heapOptions = "-Xms64m -Xmx64m" ;
68
-
69
- private void setPlatform () {
70
-
71
- String OSSpec = System .getProperty ("os.name" ).toLowerCase ();
72
- if (OSSpec != null ) {
73
- /* Get OS from the spec string */
74
- if (OSSpec .contains ("aix" )) {
75
- osName = OSName .AIX ;
76
- } else if (OSSpec .contains ("linux" )) {
77
- osName = OSName .LINUX ;
78
- } else if (OSSpec .contains ("windows" )) {
79
- osName = OSName .WINDOWS ;
80
- } else if (OSSpec .contains ("z/os" )) {
81
- osName = OSName .ZOS ;
82
- } else if (OSSpec .contains ("mac" )) {
83
- osName = OSName .MAC ;
84
- } else {
85
- System .out .println ("Runner couldn't determine underlying OS. Got OS Name:" + OSSpec );
86
- osName = OSName .UNKNOWN ;
87
- }
88
- }
89
- String archSpec = System .getProperty ("os.arch" ).toLowerCase ();
90
- if (archSpec != null ) {
91
- /* Get arch from spec string */
92
- if (archSpec .contains ("ppc" )) {
93
- osArch = OSArch .PPC ;
66
+ public static OSArch current () {
67
+ String archSpec = System .getProperty ("os.arch" , "?" ).toLowerCase ();
68
+
69
+ /* Get arch from spec string. */
70
+ if (archSpec .contains ("aarch64" )) {
71
+ return AARCH64 ;
72
+ } else if (archSpec .contains ("ppc" )) {
73
+ return PPC ;
94
74
} else if (archSpec .contains ("s390" )) {
95
- osArch = OSArch . S390X ;
75
+ return S390X ;
96
76
} else if (archSpec .contains ("amd64" ) || archSpec .contains ("x86" )) {
97
- osArch = OSArch . X86 ;
77
+ return X86 ;
98
78
} else {
99
79
System .out .println ("Runner couldn't determine underlying architecture. Got OS Arch:" + archSpec );
100
- osArch = OSArch . UNKNOWN ;
80
+ return UNKNOWN ;
101
81
}
102
82
}
83
+ }
84
+
85
+ protected static enum AddrMode {
86
+ BIT31 ,
87
+ BIT32 ,
88
+ BIT64 ,
89
+ UNKNOWN ;
90
+
91
+ public static AddrMode current (OSArch osArch ) {
92
+ String dataModel = System .getProperty ("sun.arch.data.model" , "?" );
103
93
104
- String addressingMode = System .getProperty ("sun.arch.data.model" );
105
- if (addressingMode != null ) {
106
94
/* Get address mode. S390 31-Bit addressing mode should return 32. */
107
- if ((osArch == OSArch .S390X ) && (addressingMode .contains ("32" ))) {
108
- addrMode = AddrMode .BIT31 ;
109
- } else if (addressingMode .contains ("32" )) {
110
- addrMode = AddrMode .BIT32 ;
111
- } else if (addressingMode .contains ("64" )) {
112
- addrMode = AddrMode .BIT64 ;
95
+ if (dataModel .contains ("32" )) {
96
+ return (osArch == OSArch .S390X ) ? BIT31 : BIT32 ;
97
+ } else if (dataModel .contains ("64" )) {
98
+ return BIT64 ;
113
99
} else {
114
- System .out .println ("Runner couldn't determine underlying addressing mode. Got addressingMode :" + addressingMode );
115
- addrMode = AddrMode . UNKNOWN ;
100
+ System .out .println ("Runner couldn't determine underlying addressing mode. Got addressing mode :" + dataModel );
101
+ return UNKNOWN ;
116
102
}
117
103
}
118
104
}
119
-
120
- public Runner (String className , String exeName , String bootClassPath , String userClassPath , String javaVersion ) {
105
+
106
+ public static final String systemPropertyPrefix = "j9vm." ;
107
+
108
+ private static final String heapOptions = "-Xms64m -Xmx64m" ;
109
+
110
+ protected final String className ;
111
+ protected final String exeName ;
112
+ protected final String bootClassPath ;
113
+ protected final String userClassPath ;
114
+ protected final String javaVersion ;
115
+ protected final OSName osName ;
116
+ protected final OSArch osArch ;
117
+ protected final AddrMode addrMode ;
118
+ protected OutputCollector inCollector ;
119
+ protected OutputCollector errCollector ;
120
+
121
+ public Runner (String className , String exeName , String bootClassPath , String userClassPath , String javaVersion ) {
121
122
super ();
122
123
this .className = className ;
123
124
this .exeName = exeName ;
124
125
this .bootClassPath = bootClassPath ;
125
126
this .userClassPath = userClassPath ;
126
127
this .javaVersion = javaVersion ;
127
- setPlatform ();
128
+ this .osName = OSName .current ();
129
+ this .osArch = OSArch .current ();
130
+ this .addrMode = AddrMode .current (osArch );
128
131
}
129
132
130
- public String getBootClassPathOption () {
131
- if (bootClassPath == null ) return "" ;
133
+ public String getBootClassPathOption () {
134
+ if (bootClassPath == null ) {
135
+ return "" ;
136
+ }
132
137
return "-Xbootclasspath:" + bootClassPath ;
133
138
}
134
139
135
- public String getUserClassPathOption () {
136
- if (userClassPath == null ) return "" ;
140
+ public String getUserClassPathOption () {
141
+ if (userClassPath == null ) {
142
+ return "" ;
143
+ }
137
144
return "-classpath " + userClassPath ;
138
145
}
139
146
140
147
public String getJ9VMSystemPropertiesString () {
141
- String result = "" ;
148
+ StringBuilder result = new StringBuilder () ;
142
149
Properties systemProperties = System .getProperties ();
143
- Iterator it = systemProperties .entrySet ().iterator ();
144
- while (it .hasNext ()) {
145
- Map .Entry entry = (Map .Entry ) it .next ();
150
+ for (Map .Entry <?, ?> entry : systemProperties .entrySet ()) {
146
151
String key = (String ) entry .getKey ();
147
- if (key .startsWith (systemPropertyPrefix )) {
148
- String value = (String ) entry .getValue ();
149
- result += "-D" + key + "=" + value + " " ;
152
+ if (key .startsWith (systemPropertyPrefix )) {
153
+ if (result .length () > 0 ) {
154
+ result .append (" " );
155
+ }
156
+ Object value = entry .getValue ();
157
+ result .append ("-D" ).append (key ).append ("=" ).append (value );
150
158
}
151
-
152
159
}
153
- return result ;
160
+ return result . toString () ;
154
161
}
155
162
156
163
public String getCustomCommandLineOptions () {
@@ -159,23 +166,38 @@ public String getCustomCommandLineOptions() {
159
166
}
160
167
161
168
public String getCommandLine () {
162
- return exeName + " " + heapOptions + " " + getCustomCommandLineOptions () + " "
163
- + getJ9VMSystemPropertiesString () + " " + getBootClassPathOption () + " "
164
- + getUserClassPathOption () + " " ;
169
+ StringBuilder command = new StringBuilder (exeName );
170
+
171
+ appendIfNonTrivial (command , heapOptions );
172
+ appendIfNonTrivial (command , getCustomCommandLineOptions ());
173
+ appendIfNonTrivial (command , getJ9VMSystemPropertiesString ());
174
+ appendIfNonTrivial (command , getBootClassPathOption ());
175
+ appendIfNonTrivial (command , getUserClassPathOption ());
176
+
177
+ return command .toString ();
165
178
}
166
-
179
+
180
+ private static void appendIfNonTrivial (StringBuilder buffer , String options ) {
181
+ if (options != null ) {
182
+ options = options .trim ();
183
+ if (!options .isEmpty ()) {
184
+ buffer .append (" " ).append (options );
185
+ }
186
+ }
187
+ }
188
+
167
189
public String getTestClassArguments () {
168
190
/* For sub-classes to override, if desired. */
169
191
return "" ;
170
192
}
171
-
172
- public int runCommandLine (String commandLine ) {
193
+
194
+ public int runCommandLine (String commandLine ) {
173
195
System .out .println ("command: " + commandLine );
174
196
System .out .println ();
175
197
Process process ;
176
- try {
198
+ try {
177
199
process = Runtime .getRuntime ().exec (commandLine );
178
- } catch (Throwable e ) {
200
+ } catch (Throwable e ) {
179
201
System .out .println ("Exception starting process!" );
180
202
System .out .println ("(" + e .getMessage () + ")" );
181
203
e .printStackTrace ();
@@ -188,25 +210,25 @@ public int runCommandLine(String commandLine) {
188
210
errCollector = new OutputCollector (errStream );
189
211
inCollector .start ();
190
212
errCollector .start ();
191
- try {
213
+ try {
192
214
process .waitFor ();
193
215
inCollector .join ();
194
216
errCollector .join ();
195
- } catch (InterruptedException e ) {
217
+ } catch (InterruptedException e ) {
196
218
/* Nothing. */
197
219
}
198
- /* Must release process resources here, or wimpy platforms
199
- like Neutrino will run out of handles! */
220
+ /* Release process resources here, to avoid running out of handles. */
200
221
int retval = process .exitValue ();
201
- process .destroy (); process = null ;
222
+ process .destroy ();
223
+ process = null ;
202
224
System .gc ();
203
225
return retval ;
204
226
}
205
227
206
- public boolean run () {
228
+ public boolean run () {
207
229
int retval = runCommandLine (getCommandLine () + " " + className + " " + getTestClassArguments ());
208
- if ( 0 != retval ) {
209
- System .out .println ("no -zero exit value: " + retval );
230
+ if (0 != retval ) {
231
+ System .out .println ("non -zero exit value: " + retval );
210
232
return false ;
211
233
}
212
234
return true ;
0 commit comments