1 /* 2 * Copyright (c) 2008, 2018, 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 package nsk.share.aod; 24 25 import java.io.File; 26 27 import nsk.share.*; 28 29 public class Utils { 30 31 // prevent class instantiation Utils()32 private Utils() { 33 } 34 35 public static final long JPS_WORK_TIMEOUT = 60000; // 1 min 36 37 /** 38 * Find id of VM with certain value of property key. Method findVMIdUsingJPS 39 * runs 'jps -v' and seeks in jps output line containing this unique string, 40 * discovered string should also contain VM id. 41 * 42 * @param jdkPath - path to jdk 43 * @param key - name of property 44 * @param value - value of property 45 * @return VM id 46 */ findVMIdUsingJPS(String jdkPath, String key, String value)47 public static String findVMIdUsingJPS(String jdkPath, String key, String value) { 48 try { 49 if (value == null) { 50 return null; 51 } 52 String idString = key + "=" + value; 53 String jpsPath = jdkPath + File.separator + "bin" + File.separator + "jps"; 54 55 while (true) { 56 ProcessExecutor executor = new ProcessExecutor(jpsPath + " -v", JPS_WORK_TIMEOUT, "jps -v"); 57 executor.startProcess(); 58 executor.waitForProcess(); 59 60 if (executor.getExitCode() != 0) { 61 throw new Failure("jps finished with non-zero code " + executor.getExitCode()); 62 } 63 64 for (String jpsOutLine : executor.getProcessOut()) { 65 if (jpsOutLine.contains(idString)) { 66 if (jpsOutLine.indexOf(' ') < 0) 67 throw new Failure("Unexpected format of the jps output '" + jpsOutLine + " (line doesn't contain space)"); 68 69 return jpsOutLine.substring(0, jpsOutLine.indexOf(' ')); 70 } 71 } 72 Thread.sleep(100); 73 } 74 } catch (Failure f) { 75 throw f; 76 } catch (Throwable t) { 77 throw new Failure("Unexpected exception during jps execution: " + t, t); 78 } 79 } 80 findCurrentVMIdUsingJPS(String jdkPath)81 public static String findCurrentVMIdUsingJPS(String jdkPath) { 82 /* 83 * VM should be run with special property which allows to find VM id using jps 84 * (see comments for method Utils.findVMIdUsingJPS) 85 */ 86 String applicationId = System.getProperty(AODTestRunner.targetAppIdProperty); 87 if (applicationId == null) 88 throw new TestBug("Property '" + AODTestRunner.targetAppIdProperty + "' isn't defined"); 89 90 String targetVMId = Utils.findVMIdUsingJPS(jdkPath, AODTestRunner.targetAppIdProperty, applicationId); 91 92 return targetVMId; 93 } 94 95 } 96