Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/bootstrap/CustomLauncherTest.java
38867 views
/*1* Copyright (c) 2013, 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*/2223import java.io.File;24import java.io.IOException;25import java.nio.file.FileSystem;26import java.nio.file.FileSystems;27import java.nio.file.Files;28import java.nio.file.LinkOption;29import java.nio.file.Path;30import java.nio.file.StandardCopyOption;31import java.nio.file.attribute.PosixFilePermission;32import java.util.HashSet;33import java.util.Set;34import java.util.concurrent.TimeUnit;35import java.util.concurrent.atomic.AtomicReference;3637import jdk.testlibrary.ProcessTools;3839/**40* @test41* @bug 6434402 800492642* @library /lib/testlibrary43* @build jdk.testlibrary.* TestManager TestApplication CustomLauncherTest44* @run main/othervm CustomLauncherTest45* @author Jaroslav Bachorik46*/47public class CustomLauncherTest {48private static final String TEST_CLASSPATH = System.getProperty("test.class.path");49private static final String TEST_JDK = System.getProperty("test.jdk");50private static final String WORK_DIR = System.getProperty("user.dir");5152private static final String TEST_SRC = System.getProperty("test.src");53private static final String OSNAME = System.getProperty("os.name");54private static final String ARCH;55private static final String LIBARCH;5657static {58// magic with os.arch59String osarch = System.getProperty("os.arch");60switch (osarch) {61case "i386":62case "i486":63case "i586":64case "i686":65case "i786":66case "i886":67case "i986": {68ARCH = "i586";69break;70}71case "x86_64":72case "amd64": {73ARCH = "amd64";74break;75}76case "sparc":77ARCH = "sparcv9";78break;79default: {80ARCH = osarch;81}82}83LIBARCH = ARCH.equals("i586") ? "i386" : ARCH;84}8586public static void main(String[] args) throws Exception {87if (TEST_CLASSPATH == null || TEST_CLASSPATH.isEmpty()) {88System.out.println("Test is designed to be run from jtreg only");89return;90}9192if (getPlatform() == null) {93System.out.println("Test not designed to run on this operating " +94"system (" + OSNAME + "), skipping...");95return;96}9798final FileSystem FS = FileSystems.getDefault();99100Path libjvmPath = findLibjvm(FS);101if (libjvmPath == null) {102throw new Error("Unable to locate 'libjvm.so' in " + TEST_JDK);103}104105Process serverPrc = null, clientPrc = null;106107try {108String[] launcher = getLauncher();109110System.out.println("Starting custom launcher:");111System.out.println("=========================");112System.out.println(" launcher : " + launcher[0]);113System.out.println(" libjvm : " + libjvmPath.toString());114System.out.println(" classpath : " + TEST_CLASSPATH);115ProcessBuilder server = new ProcessBuilder(116launcher[1],117libjvmPath.toString(),118TEST_CLASSPATH,119"TestApplication"120);121122final AtomicReference<String> port = new AtomicReference<>();123final AtomicReference<String> pid = new AtomicReference<>();124125serverPrc = ProcessTools.startProcess(126"Launcher",127server,128(String line) -> {129if (line.startsWith("port:")) {130port.set(line.split("\\:")[1]);131} else if (line.startsWith("pid:")) {132pid.set(line.split("\\:")[1]);133} else if (line.startsWith("waiting")) {134return true;135}136return false;137},1385,139TimeUnit.SECONDS140);141142System.out.println("Attaching test manager:");143System.out.println("=========================");144System.out.println(" PID : " + pid.get());145System.out.println(" shutdown port : " + port.get());146147ProcessBuilder client = ProcessTools.createJavaProcessBuilder(148"-cp",149TEST_CLASSPATH +150File.pathSeparator +151TEST_JDK +152File.separator +153"lib" +154File.separator +155"tools.jar",156"TestManager",157pid.get(),158port.get(),159"true"160);161162clientPrc = ProcessTools.startProcess(163"TestManager",164client,165(String line) -> line.startsWith("Starting TestManager for PID"),16610,167TimeUnit.SECONDS168);169170int clientExitCode = clientPrc.waitFor();171int serverExitCode = serverPrc.waitFor();172173if (clientExitCode != 0 || serverExitCode != 0) {174throw new Error("Test failed");175}176} finally {177if (clientPrc != null) {178clientPrc.destroy();179clientPrc.waitFor();180}181if (serverPrc != null) {182serverPrc.destroy();183serverPrc.waitFor();184}185}186}187188private static Path findLibjvm(FileSystem FS) {189Path libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "jre", "lib", LIBARCH));190if (libjvmPath == null) {191libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "lib", LIBARCH));192}193return libjvmPath;194}195196private static Path findLibjvm(Path libPath) {197// ARCH/libjvm.so -> ARCH/server/libjvm.so -> ARCH/client/libjvm.so198Path libjvmPath = libPath.resolve("libjvm.so");199if (isFileOk(libjvmPath)) {200return libjvmPath;201}202libjvmPath = libPath.resolve("server/libjvm.so");203if (isFileOk(libjvmPath)) {204return libjvmPath;205}206libjvmPath = libPath.resolve("client/libjvm.so");207if (isFileOk(libPath)) {208return libjvmPath;209}210211return null;212}213214private static boolean isFileOk(Path path) {215return Files.isRegularFile(path) && Files.isReadable(path);216}217218private static String getPlatform() {219String platform = null;220switch (OSNAME.toLowerCase()) {221case "linux": {222platform = "linux";223break;224}225case "sunos": {226platform = "solaris";227break;228}229default: {230platform = null;231}232}233234return platform;235}236237private static String[] getLauncher() throws IOException {238String platform = getPlatform();239if (platform == null) {240return null;241}242243String launcher = TEST_SRC + File.separator + platform + "-" + ARCH +244File.separator + "launcher";245246final FileSystem FS = FileSystems.getDefault();247Path launcherPath = FS.getPath(launcher);248249final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&&250Files.isReadable(launcherPath);251if (!hasLauncher) {252System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test.");253return null;254}255256// It is impossible to store an executable file in the source control257// We need to copy the launcher to the working directory258// and set the executable flag259Path localLauncherPath = FS.getPath(WORK_DIR, "launcher");260Files.copy(launcherPath, localLauncherPath,261StandardCopyOption.REPLACE_EXISTING,262StandardCopyOption.COPY_ATTRIBUTES);263if (!Files.isExecutable(localLauncherPath)) {264Set<PosixFilePermission> perms = new HashSet<>(265Files.getPosixFilePermissions(266localLauncherPath,267LinkOption.NOFOLLOW_LINKS268)269);270perms.add(PosixFilePermission.OWNER_EXECUTE);271Files.setPosixFilePermissions(localLauncherPath, perms);272}273return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()};274}275}276277278