Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/aod/Utils.java
41161 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.share.aod;2324import java.io.File;2526import nsk.share.*;2728public class Utils {2930// prevent class instantiation31private Utils() {32}3334public static final long JPS_WORK_TIMEOUT = 60000; // 1 min3536/**37* Find id of VM with certain value of property key. Method findVMIdUsingJPS38* runs 'jps -v' and seeks in jps output line containing this unique string,39* discovered string should also contain VM id.40*41* @param jdkPath - path to jdk42* @param key - name of property43* @param value - value of property44* @return VM id45*/46public static String findVMIdUsingJPS(String jdkPath, String key, String value) {47try {48if (value == null) {49return null;50}51String idString = key + "=" + value;52String jpsPath = jdkPath + File.separator + "bin" + File.separator + "jps";5354while (true) {55ProcessExecutor executor = new ProcessExecutor(jpsPath + " -v", JPS_WORK_TIMEOUT, "jps -v");56executor.startProcess();57executor.waitForProcess();5859if (executor.getExitCode() != 0) {60throw new Failure("jps finished with non-zero code " + executor.getExitCode());61}6263for (String jpsOutLine : executor.getProcessOut()) {64if (jpsOutLine.contains(idString)) {65if (jpsOutLine.indexOf(' ') < 0)66throw new Failure("Unexpected format of the jps output '" + jpsOutLine + " (line doesn't contain space)");6768return jpsOutLine.substring(0, jpsOutLine.indexOf(' '));69}70}71Thread.sleep(100);72}73} catch (Failure f) {74throw f;75} catch (Throwable t) {76throw new Failure("Unexpected exception during jps execution: " + t, t);77}78}7980public static String findCurrentVMIdUsingJPS(String jdkPath) {81/*82* VM should be run with special property which allows to find VM id using jps83* (see comments for method Utils.findVMIdUsingJPS)84*/85String applicationId = System.getProperty(AODTestRunner.targetAppIdProperty);86if (applicationId == null)87throw new TestBug("Property '" + AODTestRunner.targetAppIdProperty + "' isn't defined");8889String targetVMId = Utils.findVMIdUsingJPS(jdkPath, AODTestRunner.targetAppIdProperty, applicationId);9091return targetVMId;92}9394}959697