test/java/rmi/testlibrary/JavaVM.java

Print this page
rev 15773 : 8085192: java/rmi/activation/Activatable tests fail intermittently due to "Port already in use"
Reviewed-by:


   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 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     public static final long POLLTIME_MS = 100L;
  38 
  39     protected Process vm = null;
  40 
  41     private String classname = "";
  42     private String args = "";
  43     private String options = "";
  44     private OutputStream outputStream = System.out;
  45     private OutputStream errorStream = System.err;
  46     private String policyFileName = null;
  47     private StreamPipe outPipe;
  48     private StreamPipe errPipe;
  49 
  50     private static void mesg(Object mesg) {
  51         System.err.println("JAVAVM: " + mesg.toString());
  52     }
  53 
  54     /** string name of the program execd by JavaVM */
  55     private static String javaProgram = "java";
  56 
  57     static {
  58         try {
  59             javaProgram = TestLibrary.getProperty("java.home", "") +
  60                 File.separator + "bin" + File.separator + javaProgram;
  61         } catch (SecurityException se) {
  62         }
  63     }


  96         newArgs += " ";
  97         args = newArgs + args;
  98     }
  99 
 100     public void setPolicyFile(String policyFileName) {
 101         this.policyFileName = policyFileName;
 102     }
 103 
 104     /**
 105      * This method is used for setting VM options on spawned VMs.
 106      * It returns the extra command line options required
 107      * to turn on jcov code coverage analysis.
 108      */
 109     protected static String getCodeCoverageOptions() {
 110         return TestLibrary.getExtraProperty("jcov.options","");
 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)
 119             throw new IllegalStateException("JavaVM already started");
 120 
 121         /*
 122          * If specified, add option for policy file
 123          */
 124         if (policyFileName != null) {
 125             String option = "-Djava.security.policy=" + policyFileName;
 126             addOptions(new String[] { option });
 127         }
 128 
 129         addOptions(new String[] {
 130             getCodeCoverageOptions(),
 131             TestParams.testJavaOpts,
 132             TestParams.testVmOpts
 133         });
 134 
 135         StringTokenizer optionsTokenizer = new StringTokenizer(options);
 136         StringTokenizer argsTokenizer = new StringTokenizer(args);
 137         int optionsCount = optionsTokenizer.countTokens();
 138         int argsCount = argsTokenizer.countTokens();
 139 
 140         String javaCommand[] = new String[optionsCount + argsCount + 2];
 141         int count = 0;
 142 
 143         javaCommand[count++] = JavaVM.javaProgram;
 144         while (optionsTokenizer.hasMoreTokens()) {
 145             javaCommand[count++] = optionsTokenizer.nextToken();
 146         }
 147         javaCommand[count++] = classname;
 148         while (argsTokenizer.hasMoreTokens()) {
 149             javaCommand[count++] = argsTokenizer.nextToken();
 150         }
 151 
 152         mesg("command = " + Arrays.asList(javaCommand).toString());
 153 
 154         vm = Runtime.getRuntime().exec(javaCommand);




 155 
 156         /* output from the execed process may optionally be captured. */
 157         outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
 158         errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);


































 159     }
 160 
 161     public void destroy() {
 162         if (vm != null) {
 163             vm.destroy();
 164         }
 165         vm = null;
 166     }
 167 
 168     /**
 169      * Destroys the VM, waits for it to terminate, and returns
 170      * its exit status.
 171      *
 172      * @throws IllegalStateException if the VM has already been destroyed
 173      * @throws InterruptedException if the caller is interrupted while waiting
 174      */
 175     public int terminate() throws InterruptedException {
 176         if (vm == null) {
 177             throw new IllegalStateException("JavaVM already destroyed");
 178         }




   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 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.BufferedReader;
  25 import java.io.DataInputStream;
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.InputStreamReader;
  29 import java.io.OutputStream;
  30 import java.util.Arrays;
  31 import java.util.StringTokenizer;
  32 import java.util.concurrent.TimeoutException;
  33 
  34 /**
  35  * RMI regression test utility class that uses Runtime.exec to spawn a
  36  * java process that will run a named java class.
  37  */
  38 public class JavaVM {
  39 
  40     public static final long POLLTIME_MS = 100L;
  41 
  42     protected Process vm = null;
  43 
  44     private String classname = "";
  45     protected String args = "";
  46     protected String options = "";
  47     private OutputStream outputStream = System.out;
  48     private OutputStream errorStream = System.err;
  49     private String policyFileName = null;
  50     private StreamPipe outPipe;
  51     private StreamPipe errPipe;
  52 
  53     private static void mesg(Object mesg) {
  54         System.err.println("JAVAVM: " + mesg.toString());
  55     }
  56 
  57     /** string name of the program execd by JavaVM */
  58     private static String javaProgram = "java";
  59 
  60     static {
  61         try {
  62             javaProgram = TestLibrary.getProperty("java.home", "") +
  63                 File.separator + "bin" + File.separator + javaProgram;
  64         } catch (SecurityException se) {
  65         }
  66     }


  99         newArgs += " ";
 100         args = newArgs + args;
 101     }
 102 
 103     public void setPolicyFile(String policyFileName) {
 104         this.policyFileName = policyFileName;
 105     }
 106 
 107     /**
 108      * This method is used for setting VM options on spawned VMs.
 109      * It returns the extra command line options required
 110      * to turn on jcov code coverage analysis.
 111      */
 112     protected static String getCodeCoverageOptions() {
 113         return TestLibrary.getExtraProperty("jcov.options","");
 114     }
 115 
 116     /**
 117      * Exec the VM as specified in this object's constructor.
 118      */
 119     private void start0() throws IOException {
 120 
 121         if (vm != null)
 122             throw new IllegalStateException("JavaVM already started");
 123 
 124         /*
 125          * If specified, add option for policy file
 126          */
 127         if (policyFileName != null) {
 128             String option = "-Djava.security.policy=" + policyFileName;
 129             addOptions(new String[] { option });
 130         }
 131 
 132         addOptions(new String[] {
 133             getCodeCoverageOptions(),
 134             TestParams.testJavaOpts,
 135             TestParams.testVmOpts
 136         });
 137 
 138         StringTokenizer optionsTokenizer = new StringTokenizer(options);
 139         StringTokenizer argsTokenizer = new StringTokenizer(args);
 140         int optionsCount = optionsTokenizer.countTokens();
 141         int argsCount = argsTokenizer.countTokens();
 142 
 143         String javaCommand[] = new String[optionsCount + argsCount + 2];
 144         int count = 0;
 145 
 146         javaCommand[count++] = JavaVM.javaProgram;
 147         while (optionsTokenizer.hasMoreTokens()) {
 148             javaCommand[count++] = optionsTokenizer.nextToken();
 149         }
 150         javaCommand[count++] = classname;
 151         while (argsTokenizer.hasMoreTokens()) {
 152             javaCommand[count++] = argsTokenizer.nextToken();
 153         }
 154 
 155         mesg("command = " + Arrays.asList(javaCommand).toString());
 156 
 157         vm = Runtime.getRuntime().exec(javaCommand);
 158     }
 159 
 160     public void start() throws IOException {
 161         start0();
 162 
 163         /* output from the exec'ed process may optionally be captured. */
 164         outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
 165         errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
 166     }
 167 
 168     public int startAndGetPort() throws IOException {
 169         start0();
 170 
 171         int port = -1;
 172         if (options.contains("java.nio.channels.spi.SelectorProvider=RMIDSelectorProvider")) {
 173             // Obtain the server socket channel's ephemeral port number of the
 174             // child rmid process.
 175             BufferedReader reader = new BufferedReader(
 176                     new InputStreamReader(vm.getInputStream()));
 177             String s;
 178             while ((s = reader.readLine()) != null) {
 179                 System.out.println(s);
 180                 int i = s.indexOf(RMID.EPHEMERAL_MSG);
 181                 if (i != -1) {
 182                     String v = s.substring(RMID.EPHEMERAL_MSG.length());
 183                     port = Integer.valueOf(v);
 184                     break;
 185                 }
 186             }
 187             if (port == -1) {
 188                 // something failed
 189                 reader = new BufferedReader(new InputStreamReader(vm.getErrorStream()));
 190                 while ((s = reader.readLine()) != null)
 191                     System.err.println(s);
 192             }
 193         }
 194 
 195         /* output from the exec'ed process may optionally be captured. */
 196         outPipe = StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
 197         errPipe = StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
 198 
 199         return port;
 200     }
 201 
 202     public void destroy() {
 203         if (vm != null) {
 204             vm.destroy();
 205         }
 206         vm = null;
 207     }
 208 
 209     /**
 210      * Destroys the VM, waits for it to terminate, and returns
 211      * its exit status.
 212      *
 213      * @throws IllegalStateException if the VM has already been destroyed
 214      * @throws InterruptedException if the caller is interrupted while waiting
 215      */
 216     public int terminate() throws InterruptedException {
 217         if (vm == null) {
 218             throw new IllegalStateException("JavaVM already destroyed");
 219         }