Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdline_options_tester/src/CommandExecuter.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
import java.io.*;
24
import java.util.Iterator;
25
import java.util.Vector;
26
27
class CommandExecuter implements Runnable, Command {
28
private String _command;
29
private String _background;
30
private String _variableStdout;
31
private String _variableStderr;
32
private String _return;
33
private Vector _args;
34
35
CommandExecuter( String cmd, String background, String var1, String var2, String ret) {
36
_command = cmd;
37
_background = background;
38
_variableStdout = var1;
39
_variableStderr = var2;
40
_return = ret;
41
_args = new Vector();
42
}
43
44
public void executeSelf() {
45
String bg = TestSuite.evaluateVariables( _background );
46
if (bg != null && bg.equalsIgnoreCase("yes")) {
47
new Thread( this ).start();
48
} else {
49
this.run();
50
}
51
}
52
53
public void run() {
54
/**
55
* Set up a built-in variable "Q" to represent a double-quotes(").
56
* The command comes with $Q$ initially set up by tester to quote the classpath with white spaces.
57
* $Q$ in the command string will be replaced with a double-quotes(") by the parser of
58
* test framework before passing it over to Tokenizer for further processing.
59
* @see Tokenizer
60
*/
61
TestSuite.putVariable("Q", "\"");
62
StringBuffer buf1 = new StringBuffer();
63
StringBuffer buf2 = new StringBuffer();
64
String command = TestSuite.evaluateVariables( _command );
65
System.out.println( "Executing command: " + command );
66
try {
67
/**
68
* According to the test framework, a command string is passed over to exec(String command,...) of Runtime for test execution.
69
* However, the method is unable to recognize a command string if white spaces occur in the classpath of the command string.
70
* To solve the issue, exec(String command,...) is replaced with exec(String[] cmdarray,...), in which case it requires that
71
* a command string be replaced with a command array with all arguments split up in the array.
72
* Meanwhile, a path of .jar file with white spaces should be treated as a single argument in the command array.
73
* Thus, a new class called Tokenizer is created to address the issue of classpath when splitting up a command string.
74
* NOTE: The reason why StreamTokenizer was discarded is that it wrongly interpreted escape characters (e.g. \b, \n, \t)
75
* in the classpath into a single character rather than two characters.
76
* @see Tokenizer
77
*/
78
String[] cmdArray = Tokenizer.tokenize(command);
79
Process proc;
80
if (_args.isEmpty()) {
81
proc = Runtime.getRuntime().exec(
82
cmdArray,
83
null,
84
new File( System.getProperty("user.dir") ) );
85
} else {
86
String[] args = new String[cmdArray.length + _args.size()];
87
System.arraycopy(cmdArray, 0, args, 0, cmdArray.length);
88
89
for (int i = 0; i < _args.size(); i++) {
90
String arg = (String) _args.get(i);
91
args[cmdArray.length + i] = TestSuite.evaluateVariables(arg);
92
}
93
proc = Runtime.getRuntime().exec(
94
args,
95
null,
96
new File( System.getProperty("user.dir") ) );
97
}
98
// need to read from input/error streams as noted in
99
// http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
100
CaptureThread ct = new CaptureThread( proc.getInputStream(), (null == _variableStdout) ? null : buf1);
101
ct.start();
102
CaptureThread ce = new CaptureThread( proc.getErrorStream(), (null == _variableStderr) ? null : buf2);
103
ce.start();
104
ct.join();
105
ce.join();
106
int retcode = proc.waitFor();
107
if (_return != null) {
108
//System.out.println( '"' + command + "\" returned code: " + retcode );
109
TestSuite.putVariable( _return, Integer.toString( retcode ) );
110
}
111
} catch (Exception e) {
112
System.out.println("Error while executing command");
113
e.printStackTrace();
114
}
115
if (null != _variableStdout) {
116
String value = buf1.toString();
117
//System.out.println( "Captured output from \"" + command + "\": " + value );
118
TestSuite.putVariable(_variableStdout, value );
119
}
120
if (null != _variableStderr) {
121
String value = buf2.toString();
122
//System.out.println( "Captured output from \"" + command + "\": " + value );
123
TestSuite.putVariable(_variableStderr, value );
124
}
125
System.out.println("");
126
}
127
128
class CaptureThread extends Thread {
129
BufferedReader _br;
130
StringBuffer _buf;
131
132
CaptureThread( InputStream in, StringBuffer buf ) {
133
_br = new BufferedReader( new InputStreamReader( in ) );
134
_buf = buf;
135
}
136
137
public void run() {
138
try {
139
String read;
140
while ((read = _br.readLine()) != null) {
141
//System.out.println(read);
142
if (_buf != null) {
143
_buf.append( read );
144
}
145
}
146
} catch (IOException ioe) {
147
/* do nothing; user can fix problem based on captured output */
148
}
149
}
150
}
151
152
public void addArg(String string) {
153
_args.add(string);
154
}
155
}
156
157