author | tyan |
Sat, 15 Feb 2014 10:23:07 +0800 | |
changeset 22990 | daa64fcb5ba7 |
parent 22091 | 9e1731e5cfc7 |
child 23064 | e16d4c844e76 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15005
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
2 |
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
14919
cd751b98ef69
8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents:
14342
diff
changeset
|
24 |
import java.io.File; |
cd751b98ef69
8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents:
14342
diff
changeset
|
25 |
import java.io.IOException; |
cd751b98ef69
8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents:
14342
diff
changeset
|
26 |
import java.io.OutputStream; |
2 | 27 |
import java.util.Arrays; |
28 |
import java.util.StringTokenizer; |
|
22990
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
29 |
import java.util.concurrent.TimeoutException; |
2 | 30 |
|
31 |
/** |
|
32 |
* RMI regression test utility class that uses Runtime.exec to spawn a |
|
33 |
* java process that will run a named java class. |
|
34 |
*/ |
|
35 |
public class JavaVM { |
|
36 |
||
37 |
protected Process vm = null; |
|
38 |
||
39 |
private String classname = ""; |
|
40 |
private String args = ""; |
|
41 |
private String options = ""; |
|
42 |
private OutputStream outputStream = System.out; |
|
43 |
private OutputStream errorStream = System.err; |
|
44 |
private String policyFileName = null; |
|
15005
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
45 |
private StreamPipe outPipe; |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
46 |
private StreamPipe errPipe; |
12692 | 47 |
|
2 | 48 |
private static void mesg(Object mesg) { |
49 |
System.err.println("JAVAVM: " + mesg.toString()); |
|
50 |
} |
|
51 |
||
52 |
/** string name of the program execd by JavaVM */ |
|
53 |
private static String javaProgram = "java"; |
|
54 |
||
55 |
static { |
|
56 |
try { |
|
57 |
javaProgram = TestLibrary.getProperty("java.home", "") + |
|
58 |
File.separator + "bin" + File.separator + javaProgram; |
|
59 |
} catch (SecurityException se) { |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
public JavaVM(String classname, |
|
64 |
String options, String args) { |
|
65 |
this.classname = classname; |
|
66 |
this.options = options; |
|
67 |
this.args = args; |
|
68 |
} |
|
69 |
||
70 |
public JavaVM(String classname, |
|
71 |
String options, String args, |
|
72 |
OutputStream out, OutputStream err) { |
|
73 |
this(classname, options, args); |
|
74 |
this.outputStream = out; |
|
75 |
this.errorStream = err; |
|
76 |
} |
|
77 |
||
12692 | 78 |
// Prepends passed opts array to current options |
2 | 79 |
public void addOptions(String[] opts) { |
80 |
String newOpts = ""; |
|
81 |
for (int i = 0 ; i < opts.length ; i ++) { |
|
82 |
newOpts += " " + opts[i]; |
|
83 |
} |
|
84 |
newOpts += " "; |
|
85 |
options = newOpts + options; |
|
86 |
} |
|
12692 | 87 |
|
88 |
// Prepends passed arguments array to current args |
|
2 | 89 |
public void addArguments(String[] arguments) { |
90 |
String newArgs = ""; |
|
91 |
for (int i = 0 ; i < arguments.length ; i ++) { |
|
92 |
newArgs += " " + arguments[i]; |
|
93 |
} |
|
94 |
newArgs += " "; |
|
95 |
args = newArgs + args; |
|
96 |
} |
|
97 |
||
98 |
public void setPolicyFile(String policyFileName) { |
|
99 |
this.policyFileName = policyFileName; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* This method is used for setting VM options on spawned VMs. |
|
104 |
* It returns the extra command line options required |
|
105 |
* to turn on jcov code coverage analysis. |
|
106 |
*/ |
|
107 |
protected static String getCodeCoverageOptions() { |
|
108 |
return TestLibrary.getExtraProperty("jcov.options",""); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Exec the VM as specified in this object's constructor. |
|
113 |
*/ |
|
114 |
public void start() throws IOException { |
|
115 |
||
14919
cd751b98ef69
8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents:
14342
diff
changeset
|
116 |
if (vm != null) |
cd751b98ef69
8005290: remove -showversion from RMI test library subprocess mechanism
smarks
parents:
14342
diff
changeset
|
117 |
throw new IllegalStateException("JavaVM already started"); |
2 | 118 |
|
119 |
/* |
|
120 |
* If specified, add option for policy file |
|
121 |
*/ |
|
122 |
if (policyFileName != null) { |
|
123 |
String option = "-Djava.security.policy=" + policyFileName; |
|
124 |
addOptions(new String[] { option }); |
|
125 |
} |
|
126 |
||
127 |
addOptions(new String[] { getCodeCoverageOptions() }); |
|
128 |
||
129 |
StringTokenizer optionsTokenizer = new StringTokenizer(options); |
|
130 |
StringTokenizer argsTokenizer = new StringTokenizer(args); |
|
131 |
int optionsCount = optionsTokenizer.countTokens(); |
|
132 |
int argsCount = argsTokenizer.countTokens(); |
|
133 |
||
134 |
String javaCommand[] = new String[optionsCount + argsCount + 2]; |
|
135 |
int count = 0; |
|
136 |
||
137 |
javaCommand[count++] = JavaVM.javaProgram; |
|
138 |
while (optionsTokenizer.hasMoreTokens()) { |
|
139 |
javaCommand[count++] = optionsTokenizer.nextToken(); |
|
140 |
} |
|
141 |
javaCommand[count++] = classname; |
|
142 |
while (argsTokenizer.hasMoreTokens()) { |
|
143 |
javaCommand[count++] = argsTokenizer.nextToken(); |
|
144 |
} |
|
145 |
||
146 |
mesg("command = " + Arrays.asList(javaCommand).toString()); |
|
147 |
||
148 |
vm = Runtime.getRuntime().exec(javaCommand); |
|
149 |
||
150 |
/* output from the execed process may optionally be captured. */ |
|
15005
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
151 |
outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream); |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
152 |
errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream); |
2 | 153 |
} |
154 |
||
155 |
public void destroy() { |
|
156 |
if (vm != null) { |
|
157 |
vm.destroy(); |
|
158 |
} |
|
159 |
vm = null; |
|
160 |
} |
|
161 |
||
15005
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
162 |
/** |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
163 |
* Waits for the subprocess to exit, joins the pipe threads to ensure that |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
164 |
* all output is collected, and returns its exit status. |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
165 |
*/ |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
166 |
public int waitFor() throws InterruptedException { |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
167 |
if (vm == null) |
22091
9e1731e5cfc7
8007256: RMI testlibrary cleanup: remove JavaVMCallbackHandler
smarks
parents:
16100
diff
changeset
|
168 |
throw new IllegalStateException("can't wait for JavaVM that isn't running"); |
15005
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
169 |
|
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
170 |
int status = vm.waitFor(); |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
171 |
outPipe.join(); |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
172 |
errPipe.join(); |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
173 |
return status; |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
174 |
} |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
175 |
|
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
176 |
/** |
22990
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
177 |
* Causes the current thread to wait the vm process to exit, if necessary, |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
178 |
* wait until the vm process has terminated, or the specified waiting time |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
179 |
* elapses. Release allocated input/output after vm process has terminated. |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
180 |
* @param timeout the maximum milliseconds to wait. |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
181 |
* @return exit value for vm process. |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
182 |
* @throws InterruptedException if the current thread is interrupted |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
183 |
* while waiting. |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
184 |
* @throws TimeoutException if subprocess does not end after timeout |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
185 |
* milliseconds passed |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
186 |
*/ |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
187 |
public int waitFor(long timeout) |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
188 |
throws InterruptedException, TimeoutException { |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
189 |
if (vm == null) |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
190 |
throw new IllegalStateException("can't wait for JavaVM that isn't running"); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
191 |
long startTime = System.currentTimeMillis(); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
192 |
long rem = timeout; |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
193 |
|
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
194 |
do { |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
195 |
try { |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
196 |
int status = vm.exitValue(); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
197 |
outPipe.join(); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
198 |
errPipe.join(); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
199 |
return status; |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
200 |
} catch (IllegalThreadStateException ex) { |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
201 |
if (rem > 0) { |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
202 |
Thread.sleep(Math.min(rem, 100)); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
203 |
} |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
204 |
} |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
205 |
rem = timeout - (System.currentTimeMillis() - startTime); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
206 |
} while (rem > 0); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
207 |
throw new TimeoutException(); |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
208 |
} |
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
209 |
|
daa64fcb5ba7
8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
tyan
parents:
22091
diff
changeset
|
210 |
/** |
15005
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
211 |
* Starts the subprocess, waits for it to exit, and returns its exit status. |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
212 |
*/ |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
213 |
public int execute() throws IOException, InterruptedException { |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
214 |
start(); |
796ffc400e9e
7187882: TEST_BUG: java/rmi/activation/checkusage/CheckUsage.java fails intermittently
smarks
parents:
14919
diff
changeset
|
215 |
return waitFor(); |
2 | 216 |
} |
217 |
} |