< prev index next >

test/lib/jdk/test/lib/process/ProcessTools.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 2019, Oracle and/or its affiliates. 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 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.test.lib.process;
  25 

  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.io.OutputStream;
  29 import java.io.PrintStream;
  30 import java.nio.charset.Charset;

  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.Collections;
  34 import java.util.concurrent.CountDownLatch;
  35 import java.util.Map;
  36 import java.util.concurrent.ExecutionException;
  37 import java.util.concurrent.Future;
  38 import java.util.concurrent.TimeUnit;
  39 import java.util.concurrent.TimeoutException;
  40 import java.util.function.Predicate;
  41 import java.util.function.Consumer;
  42 import java.util.stream.Collectors;
  43 import java.security.AccessController;
  44 import java.security.PrivilegedActionException;
  45 import java.security.PrivilegedExceptionAction;
  46 
  47 import jdk.test.lib.JDKToolFinder;

  48 import jdk.test.lib.Utils;
  49 
  50 public final class ProcessTools {
  51     private static final class LineForwarder extends StreamPumper.LinePump {
  52         private final PrintStream ps;
  53         private final String prefix;
  54         LineForwarder(String prefix, PrintStream os) {
  55             this.ps = os;
  56             this.prefix = prefix;
  57         }
  58         @Override
  59         protected void processLine(String line) {
  60             ps.println("[" + prefix + "] " + line);
  61         }
  62     }
  63 
  64     private ProcessTools() {
  65     }
  66 
  67     /**


 473      * Executes a process, waits for it to finish, prints the process output
 474      * to stdout and returns the process output.
 475      *
 476      * The process will have exited before this method returns.
 477      *
 478      * @param pb The ProcessBuilder to execute.
 479      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 480      */
 481     public static OutputAnalyzer executeCommand(ProcessBuilder pb)
 482             throws Throwable {
 483         String cmdLine = pb.command().stream()
 484                 .map(x -> (x.contains(" ") || x.contains("$"))
 485                         ? ("'" + x + "'") : x)
 486                 .collect(Collectors.joining(" "));
 487         System.out.println("Command line: [" + cmdLine + "]");
 488         OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
 489         System.out.println(analyzer.getOutput());
 490         return analyzer;
 491     }
 492 






































 493     private static Process privilegedStart(ProcessBuilder pb) throws IOException {
 494         try {
 495             return AccessController.doPrivileged(
 496                 (PrivilegedExceptionAction<Process>) () -> pb.start());
 497         } catch (PrivilegedActionException e) {
 498             IOException t = (IOException) e.getException();
 499             throw t;
 500         }
 501     }
 502 
 503     private static class ProcessImpl extends Process {
 504 
 505         private final Process p;
 506         private final Future<Void> stdoutTask;
 507         private final Future<Void> stderrTask;
 508 
 509         public ProcessImpl(Process p, Future<Void> stdoutTask, Future<Void> stderrTask) {
 510             this.p = p;
 511             this.stdoutTask = stdoutTask;
 512             this.stderrTask = stderrTask;


   1 /*
   2  * Copyright (c) 2013, 2020, Oracle and/or its affiliates. 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 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.test.lib.process;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.io.OutputStream;
  30 import java.io.PrintStream;
  31 import java.nio.charset.Charset;
  32 import java.nio.file.Paths;
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.concurrent.CountDownLatch;
  37 import java.util.Map;
  38 import java.util.concurrent.ExecutionException;
  39 import java.util.concurrent.Future;
  40 import java.util.concurrent.TimeUnit;
  41 import java.util.concurrent.TimeoutException;
  42 import java.util.function.Predicate;
  43 import java.util.function.Consumer;
  44 import java.util.stream.Collectors;
  45 import java.security.AccessController;
  46 import java.security.PrivilegedActionException;
  47 import java.security.PrivilegedExceptionAction;
  48 
  49 import jdk.test.lib.JDKToolFinder;
  50 import jdk.test.lib.Platform;
  51 import jdk.test.lib.Utils;
  52 
  53 public final class ProcessTools {
  54     private static final class LineForwarder extends StreamPumper.LinePump {
  55         private final PrintStream ps;
  56         private final String prefix;
  57         LineForwarder(String prefix, PrintStream os) {
  58             this.ps = os;
  59             this.prefix = prefix;
  60         }
  61         @Override
  62         protected void processLine(String line) {
  63             ps.println("[" + prefix + "] " + line);
  64         }
  65     }
  66 
  67     private ProcessTools() {
  68     }
  69 
  70     /**


 476      * Executes a process, waits for it to finish, prints the process output
 477      * to stdout and returns the process output.
 478      *
 479      * The process will have exited before this method returns.
 480      *
 481      * @param pb The ProcessBuilder to execute.
 482      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 483      */
 484     public static OutputAnalyzer executeCommand(ProcessBuilder pb)
 485             throws Throwable {
 486         String cmdLine = pb.command().stream()
 487                 .map(x -> (x.contains(" ") || x.contains("$"))
 488                         ? ("'" + x + "'") : x)
 489                 .collect(Collectors.joining(" "));
 490         System.out.println("Command line: [" + cmdLine + "]");
 491         OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
 492         System.out.println(analyzer.getOutput());
 493         return analyzer;
 494     }
 495 
 496     /**
 497      * Helper method to create a process builder for launching native executable
 498      * test that uses/loads JVM.
 499      *
 500      * @param executableName The name of an executable to be launched.
 501      * @param args Arguments for the executable.
 502      * @return New ProcessBuilder instance representing the command.
 503      */
 504     public static ProcessBuilder createNativeTestProcessBuilder(String executableName,
 505                                                                 String... args) throws Exception {
 506         String executable = Paths.get(System.getProperty("test.nativepath"), executableName)
 507             .toAbsolutePath()
 508             .toString();
 509 
 510         ProcessBuilder pb = new ProcessBuilder(executable);
 511         pb.command().addAll(Arrays.asList(args));
 512         addJvmLib(pb);
 513         return pb;
 514     }
 515 
 516     /**
 517      * Adds JVM library path to the native library path.
 518      *
 519      * @param pb ProcessBuilder to be updated with JVM library path.
 520      */
 521     public static void addJvmLib(ProcessBuilder pb) throws Exception {
 522         String jvmLibDir = Platform.jvmLibDir().toString();
 523         String libPathVar = Platform.sharedLibraryPathVariableName();
 524         String currentLibPath = pb.environment().get(libPathVar);
 525 
 526         String newLibPath = jvmLibDir;
 527         if ( (currentLibPath != null) && !currentLibPath.isEmpty() ) {
 528             newLibPath = currentLibPath + File.pathSeparator + jvmLibDir;
 529         }
 530 
 531         pb.environment().put(libPathVar, newLibPath);
 532     }
 533 
 534     private static Process privilegedStart(ProcessBuilder pb) throws IOException {
 535         try {
 536             return AccessController.doPrivileged(
 537                 (PrivilegedExceptionAction<Process>) () -> pb.start());
 538         } catch (PrivilegedActionException e) {
 539             IOException t = (IOException) e.getException();
 540             throw t;
 541         }
 542     }
 543 
 544     private static class ProcessImpl extends Process {
 545 
 546         private final Process p;
 547         private final Future<Void> stdoutTask;
 548         private final Future<Void> stderrTask;
 549 
 550         public ProcessImpl(Process p, Future<Void> stdoutTask, Future<Void> stderrTask) {
 551             this.p = p;
 552             this.stdoutTask = stdoutTask;
 553             this.stderrTask = stderrTask;


< prev index next >