test/testlibrary/com/oracle/java/testlibrary/ProcessTools.java

Print this page




  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 com.oracle.java.testlibrary;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.IOException;
  28 import java.lang.management.ManagementFactory;
  29 import java.lang.management.RuntimeMXBean;
  30 import java.lang.reflect.Field;
  31 import java.lang.reflect.Method;
  32 import java.util.ArrayList;
  33 import java.util.Collections;

  34 
  35 import sun.management.VMManagement;
  36 
  37 public final class ProcessTools {
  38 
  39   private ProcessTools() {
  40   }
  41 
  42   /**
  43    * Pumps stdout and stderr from running the process into a String.
  44    *
  45    * @param processHandler ProcessHandler to run.
  46    * @return Output from process.
  47    * @throws IOException If an I/O error occurs.
  48    */
  49   public static OutputBuffer getOutput(ProcessBuilder processBuilder) throws IOException {
  50     return getOutput(processBuilder.start());
  51   }
  52 
  53   /**


  90    */
  91   public static int getProcessId() throws Exception {
  92 
  93     // Get the current process id using a reflection hack
  94     RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
  95     Field jvm = runtime.getClass().getDeclaredField("jvm");
  96 
  97     jvm.setAccessible(true);
  98     VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
  99 
 100     Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
 101 
 102     pid_method.setAccessible(true);
 103 
 104     int pid = (Integer) pid_method.invoke(mgmt);
 105 
 106     return pid;
 107   }
 108 
 109   /**
















 110    * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris)
 111    *
 112    * @return String[] with platform specific arguments, empty if there are none
 113    */
 114   public static String[] getPlatformSpecificVMArgs() {
 115     String osName = System.getProperty("os.name");
 116     String dataModel = System.getProperty("sun.arch.data.model");
 117 
 118     if (osName.equals("SunOS") && dataModel.equals("64")) {
 119       return new String[] { "-d64" };
 120     }
 121 
 122     return new String[] {};
 123   }
 124 
 125   /**
 126    * Create ProcessBuilder using the java launcher from the jdk to be tested and
 127    * with any platform specific arguments prepended
 128    */
 129   public static ProcessBuilder createJavaProcessBuilder(String... command) throws Exception {
 130     String javapath = JDKToolFinder.getJDKTool("java");
 131 
 132     ArrayList<String> args = new ArrayList<>();
 133     args.add(javapath);
 134     Collections.addAll(args, getPlatformSpecificVMArgs());
 135     Collections.addAll(args, command);
 136 
 137     return new ProcessBuilder(args.toArray(new String[args.size()]));




 138 

 139   }
 140 
 141 }


  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 com.oracle.java.testlibrary;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.IOException;
  28 import java.lang.management.ManagementFactory;
  29 import java.lang.management.RuntimeMXBean;
  30 import java.lang.reflect.Field;
  31 import java.lang.reflect.Method;
  32 import java.util.ArrayList;
  33 import java.util.Collections;
  34 import java.util.List;
  35 
  36 import sun.management.VMManagement;
  37 
  38 public final class ProcessTools {
  39 
  40   private ProcessTools() {
  41   }
  42 
  43   /**
  44    * Pumps stdout and stderr from running the process into a String.
  45    *
  46    * @param processHandler ProcessHandler to run.
  47    * @return Output from process.
  48    * @throws IOException If an I/O error occurs.
  49    */
  50   public static OutputBuffer getOutput(ProcessBuilder processBuilder) throws IOException {
  51     return getOutput(processBuilder.start());
  52   }
  53 
  54   /**


  91    */
  92   public static int getProcessId() throws Exception {
  93 
  94     // Get the current process id using a reflection hack
  95     RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
  96     Field jvm = runtime.getClass().getDeclaredField("jvm");
  97 
  98     jvm.setAccessible(true);
  99     VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
 100 
 101     Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
 102 
 103     pid_method.setAccessible(true);
 104 
 105     int pid = (Integer) pid_method.invoke(mgmt);
 106 
 107     return pid;
 108   }
 109 
 110   /**
 111    * Get the string containing input arguments passed to the VM
 112    *
 113    * @return arguments
 114    */
 115   public static String getVmInputArguments() {
 116     RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
 117 
 118     List<String> args = runtime.getInputArguments();
 119     StringBuilder result = new StringBuilder();
 120     for (String arg : args)
 121         result.append(arg).append(' ');
 122 
 123     return result.toString();
 124   }
 125 
 126   /**
 127    * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris)
 128    *
 129    * @return String[] with platform specific arguments, empty if there are none
 130    */
 131   public static String[] getPlatformSpecificVMArgs() {


 132 
 133     if (Platform.is64bit() && Platform.isSolaris()) {
 134       return new String[] { "-d64" };
 135     }
 136 
 137     return new String[] {};
 138   }
 139 
 140   /**
 141    * Create ProcessBuilder using the java launcher from the jdk to be tested and
 142    * with any platform specific arguments prepended
 143    */
 144   public static ProcessBuilder createJavaProcessBuilder(String... command) throws Exception {
 145     String javapath = JDKToolFinder.getJDKTool("java");
 146 
 147     ArrayList<String> args = new ArrayList<>();
 148     args.add(javapath);
 149     Collections.addAll(args, getPlatformSpecificVMArgs());
 150     Collections.addAll(args, command);
 151 
 152     // Reporting
 153     StringBuilder cmdLine = new StringBuilder();
 154     for (String cmd : args)
 155         cmdLine.append(cmd).append(' ');
 156     System.out.println("Command line: [" + cmdLine.toString() + "]");
 157 
 158     return new ProcessBuilder(args.toArray(new String[args.size()]));
 159   }
 160 
 161 }