test/java/rmi/testlibrary/JavaVM.java

Print this page
rev 9281 : 8032050: Clean up for java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
Reviewed-by: smarks


   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());


 153 
 154     public void destroy() {
 155         if (vm != null) {
 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());


 154 
 155     public void destroy() {
 156         if (vm != null) {
 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 vm process to exit, if necessary,
 178      * wait until the vm process has terminated, or the specified waiting time
 179      * elapses. Release allocated input/output after vm process has terminated.
 180      * @param timeout the maximum milliseconds to wait.
 181      * @return exit value for vm process.
 182      * @throws InterruptedException if the current thread is interrupted
 183      *         while waiting.
 184      * @throws TimeoutException if subprocess does not end after timeout
 185      *         milliseconds passed
 186      */
 187     public int waitFor(long timeout)
 188             throws InterruptedException, TimeoutException {
 189         if (vm == null)
 190             throw new IllegalStateException("can't wait for JavaVM that isn't running");
 191         long startTime = System.currentTimeMillis();
 192         long rem = timeout;
 193 
 194         do {
 195             try {
 196                 int status = vm.exitValue();
 197                 outPipe.join();
 198                 errPipe.join();
 199                 return status;
 200             } catch (IllegalThreadStateException ex) {
 201                 if (rem > 0) {
 202                     Thread.sleep(Math.min(rem, 100));
 203                 }
 204             }
 205             rem = timeout - (System.currentTimeMillis() - startTime);
 206         } while (rem > 0);
 207         throw new TimeoutException();
 208     }
 209 
 210     /**
 211      * Starts the subprocess, waits for it to exit, and returns its exit status.
 212      */
 213     public int execute() throws IOException, InterruptedException {
 214         start();
 215         return waitFor();
 216     }
 217 }