blob: cb8ac8707b9eada92cbc07b7bc2985d86e81dd12 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 *
26 */
27
28import java.io.*;
29import java.util.Arrays;
30import java.util.Properties;
31import java.util.StringTokenizer;
32
33/**
34 * RMI regression test utility class that uses Runtime.exec to spawn a
35 * java process that will run a named java class.
36 */
37public class JavaVM {
38
39 // need to
40 protected Process vm = null;
41
42 private String classname = "";
43 private String args = "";
44 private String options = "";
45 private OutputStream outputStream = System.out;
46 private OutputStream errorStream = System.err;
47 private String policyFileName = null;
48
49 private static void mesg(Object mesg) {
50 System.err.println("JAVAVM: " + mesg.toString());
51 }
52
53 /** string name of the program execd by JavaVM */
54 private static String javaProgram = "java";
55
56 static {
57 try {
58 javaProgram = TestLibrary.getProperty("java.home", "") +
59 File.separator + "bin" + File.separator + javaProgram;
60 } catch (SecurityException se) {
61 }
62 }
63
64 public JavaVM(String classname) {
65 this.classname = classname;
66 }
67 public JavaVM(String classname,
68 String options, String args) {
69 this.classname = classname;
70 this.options = options;
71 this.args = args;
72 }
73
74 public JavaVM(String classname,
75 String options, String args,
76 OutputStream out, OutputStream err) {
77 this(classname, options, args);
78 this.outputStream = out;
79 this.errorStream = err;
80 }
81
82 public void addOptions(String[] opts) {
83 String newOpts = "";
84 for (int i = 0 ; i < opts.length ; i ++) {
85 newOpts += " " + opts[i];
86 }
87 newOpts += " ";
88 options = newOpts + options;
89 }
90 public void addArguments(String[] arguments) {
91 String newArgs = "";
92 for (int i = 0 ; i < arguments.length ; i ++) {
93 newArgs += " " + arguments[i];
94 }
95 newArgs += " ";
96 args = newArgs + args;
97 }
98
99 public void setPolicyFile(String policyFileName) {
100 this.policyFileName = policyFileName;
101 }
102
103 /**
104 * This method is used for setting VM options on spawned VMs.
105 * It returns the extra command line options required
106 * to turn on jcov code coverage analysis.
107 */
108 protected static String getCodeCoverageOptions() {
109 return TestLibrary.getExtraProperty("jcov.options","");
110 }
111
112
113 /**
114 * Exec the VM as specified in this object's constructor.
115 */
116 public void start() throws IOException {
117
118 if (vm != null) return;
119
120 /*
121 * If specified, add option for policy file
122 */
123 if (policyFileName != null) {
124 String option = "-Djava.security.policy=" + policyFileName;
125 addOptions(new String[] { option });
126 }
127
128 addOptions(new String[] { getCodeCoverageOptions() });
129
130 StringTokenizer optionsTokenizer = new StringTokenizer(options);
131 StringTokenizer argsTokenizer = new StringTokenizer(args);
132 int optionsCount = optionsTokenizer.countTokens();
133 int argsCount = argsTokenizer.countTokens();
134
135 String javaCommand[] = new String[optionsCount + argsCount + 2];
136 int count = 0;
137
138 javaCommand[count++] = JavaVM.javaProgram;
139 while (optionsTokenizer.hasMoreTokens()) {
140 javaCommand[count++] = optionsTokenizer.nextToken();
141 }
142 javaCommand[count++] = classname;
143 while (argsTokenizer.hasMoreTokens()) {
144 javaCommand[count++] = argsTokenizer.nextToken();
145 }
146
147 mesg("command = " + Arrays.asList(javaCommand).toString());
148 System.err.println("");
149
150 vm = Runtime.getRuntime().exec(javaCommand);
151
152 /* output from the execed process may optionally be captured. */
153 StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
154 StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
155
156 try {
157 Thread.sleep(2000);
158 } catch (Exception ignore) {
159 }
160
161 mesg("finished starting vm.");
162 }
163
164 public void destroy() {
165 if (vm != null) {
166 vm.destroy();
167 }
168 vm = null;
169 }
170
171 protected Process getVM() {
172 return vm;
173 }
174}