< prev index next >

test/lib/testlibrary/jdk/testlibrary/ProcessTools.java

Print this page




  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 package jdk.testlibrary;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.io.PrintStream;
  30 import java.lang.management.ManagementFactory;
  31 import java.lang.management.RuntimeMXBean;
  32 import java.lang.reflect.Field;
  33 import java.lang.reflect.Method;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.concurrent.CountDownLatch;
  38 import java.util.Map;
  39 import java.util.concurrent.ExecutionException;
  40 import java.util.concurrent.Future;
  41 import java.util.concurrent.TimeUnit;
  42 import java.util.concurrent.TimeoutException;
  43 import java.util.function.Predicate;
  44 import java.util.function.Consumer;
  45 import java.util.stream.Collectors;
  46 
  47 public final class ProcessTools {
  48     private static final class LineForwarder extends StreamPumper.LinePump {
  49         private final PrintStream ps;
  50         private final String prefix;
  51         LineForwarder(String prefix, PrintStream os) {


 236      *                      properly warmed-up.
 237      *                      It can be null - in that case the warmup is skipped.
 238      * @return Returns the initialized {@linkplain Process}
 239      * @throws IOException
 240      * @throws InterruptedException
 241      * @throws TimeoutException
 242      */
 243     @SuppressWarnings("overloads")
 244     public static Process startProcess(String name,
 245                                        ProcessBuilder processBuilder,
 246                                        final Predicate<String> linePredicate)
 247     throws IOException, InterruptedException, TimeoutException {
 248         return startProcess(name, processBuilder, linePredicate, 0, TimeUnit.SECONDS);
 249     }
 250 
 251     /**
 252      * Get the process id of the current running Java process
 253      *
 254      * @return Process id
 255      */
 256     public static int getProcessId() throws Exception {
 257         RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
 258         int pid = Integer.parseInt(runtime.getName().split("@")[0]);
 259 
 260         return pid;


 261     }
 262 
 263     /**
 264      * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris)
 265      *
 266      * @return String[] with platform specific arguments, empty if there are
 267      *         none
 268      */
 269     public static String[] getPlatformSpecificVMArgs() {
 270         String osName = System.getProperty("os.name");
 271         String dataModel = System.getProperty("sun.arch.data.model");
 272 
 273         if (osName.equals("SunOS") && dataModel.equals("64")) {
 274             return new String[] { "-d64" };
 275         }
 276 
 277         return new String[] {};
 278     }
 279 
 280     /**




  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 package jdk.testlibrary;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.io.PrintStream;


  30 import java.lang.reflect.Field;
  31 import java.lang.reflect.Method;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Collections;
  35 import java.util.concurrent.CountDownLatch;
  36 import java.util.Map;
  37 import java.util.concurrent.ExecutionException;
  38 import java.util.concurrent.Future;
  39 import java.util.concurrent.TimeUnit;
  40 import java.util.concurrent.TimeoutException;
  41 import java.util.function.Predicate;
  42 import java.util.function.Consumer;
  43 import java.util.stream.Collectors;
  44 
  45 public final class ProcessTools {
  46     private static final class LineForwarder extends StreamPumper.LinePump {
  47         private final PrintStream ps;
  48         private final String prefix;
  49         LineForwarder(String prefix, PrintStream os) {


 234      *                      properly warmed-up.
 235      *                      It can be null - in that case the warmup is skipped.
 236      * @return Returns the initialized {@linkplain Process}
 237      * @throws IOException
 238      * @throws InterruptedException
 239      * @throws TimeoutException
 240      */
 241     @SuppressWarnings("overloads")
 242     public static Process startProcess(String name,
 243                                        ProcessBuilder processBuilder,
 244                                        final Predicate<String> linePredicate)
 245     throws IOException, InterruptedException, TimeoutException {
 246         return startProcess(name, processBuilder, linePredicate, 0, TimeUnit.SECONDS);
 247     }
 248 
 249     /**
 250      * Get the process id of the current running Java process
 251      *
 252      * @return Process id
 253      */
 254     public static int getProcessId() {
 255         long pid = ProcessHandle.current().getPid();
 256         if(pid == (int)pid) {
 257             return (int)pid;
 258         } else {
 259             throw new RuntimeException("int does not suit pid: " + pid);
 260         }
 261     }
 262 
 263     /**
 264      * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris)
 265      *
 266      * @return String[] with platform specific arguments, empty if there are
 267      *         none
 268      */
 269     public static String[] getPlatformSpecificVMArgs() {
 270         String osName = System.getProperty("os.name");
 271         String dataModel = System.getProperty("sun.arch.data.model");
 272 
 273         if (osName.equals("SunOS") && dataModel.equals("64")) {
 274             return new String[] { "-d64" };
 275         }
 276 
 277         return new String[] {};
 278     }
 279 
 280     /**


< prev index next >