test/java/rmi/testlibrary/JavaVM.java

Print this page




   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 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.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 import java.util.Arrays;
  28 import java.util.StringTokenizer;

  29 
  30 /**
  31  * RMI regression test utility class that uses Runtime.exec to spawn a
  32  * java process that will run a named java class.
  33  */
  34 public class JavaVM {
  35 
  36     protected Process vm = null;
  37 
  38     private String classname = "";
  39     private String args = "";
  40     private String options = "";
  41     private OutputStream outputStream = System.out;
  42     private OutputStream errorStream = System.err;
  43     private String policyFileName = null;
  44     private StreamPipe outPipe;
  45     private StreamPipe errPipe;
  46 
  47     private static void mesg(Object mesg) {
  48         System.err.println("JAVAVM: " + mesg.toString());


 156             vm.destroy();
 157         }
 158         vm = null;
 159     }
 160 
 161     /**
 162      * Waits for the subprocess to exit, joins the pipe threads to ensure that
 163      * all output is collected, and returns its exit status.
 164      */
 165     public int waitFor() throws InterruptedException {
 166         if (vm == null)
 167             throw new IllegalStateException("can't wait for JavaVM that isn't running");
 168 
 169         int status = vm.waitFor();
 170         outPipe.join();
 171         errPipe.join();
 172         return status;
 173     }
 174 
 175     /**



































 176      * Starts the subprocess, waits for it to exit, and returns its exit status.
 177      */
 178     public int execute() throws IOException, InterruptedException {
 179         start();
 180         return waitFor();
 181     }
 182 }


   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 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.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 import java.util.Arrays;
  28 import java.util.StringTokenizer;
  29 import java.util.concurrent.TimeoutException;
  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;
  45     private StreamPipe outPipe;
  46     private StreamPipe errPipe;
  47 
  48     private static void mesg(Object mesg) {
  49         System.err.println("JAVAVM: " + mesg.toString());


 157             vm.destroy();
 158         }
 159         vm = null;
 160     }
 161 
 162     /**
 163      * Waits for the subprocess to exit, joins the pipe threads to ensure that
 164      * all output is collected, and returns its exit status.
 165      */
 166     public int waitFor() throws InterruptedException {
 167         if (vm == null)
 168             throw new IllegalStateException("can't wait for JavaVM that isn't running");
 169 
 170         int status = vm.waitFor();
 171         outPipe.join();
 172         errPipe.join();
 173         return status;
 174     }
 175 
 176         /**
 177      * Causes the current thread to wait the subprocess to exit, if necessary,
 178      * until the subprocess represented by this Process object has terminated,
 179      * or the specified waiting time elapses.
 180      * @param timeout the maximum milliseconds to wait
 181      * @return true if the JavaVM has exited and false if the waiting time
 182      *         elapsed before the subprocess has exited.
 183      * @throws InterruptedException if the current thread is interrupted
 184      *         while waiting.
 185      * @throws TimeoutException if subprocess does not end after timeout
 186      *         milliseconds passed
 187      */
 188     public int waitFor(long timeout)
 189             throws InterruptedException, TimeoutException {
 190         if (vm == null)
 191             throw new IllegalStateException("can't wait for JavaVM that isn't running");
 192         long startTime = System.currentTimeMillis();
 193         long rem = timeout;
 194 
 195         do {
 196             try {
 197                 int status = vm.exitValue();
 198                 outPipe.join();
 199                 errPipe.join();
 200                 return status;
 201             } catch (IllegalThreadStateException ex) {
 202                 if (rem > 0) {
 203                     Thread.sleep(Math.min(rem, 100));
 204                 }
 205             }
 206             rem = timeout - (System.currentTimeMillis() - startTime);
 207         } while (rem > 0);
 208         throw new TimeoutException();
 209     }
 210 
 211     /**
 212      * Starts the subprocess, waits for it to exit, and returns its exit status.
 213      */
 214     public int execute() throws IOException, InterruptedException {
 215         start();
 216         return waitFor();
 217     }
 218 }