Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessModuleLauncher.java
41155 views
/*1* Copyright (c) 2019, 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 jdk.internal.module.ModuleInfoWriter;2425import java.io.File;26import java.io.IOException;27import java.io.OutputStream;28import java.lang.module.ModuleDescriptor;29import java.nio.file.Files;30import java.nio.file.Path;31import java.nio.file.Paths;32import java.util.ArrayList;33import java.util.List;34import java.util.jar.JarEntry;35import java.util.jar.JarOutputStream;36import java.util.stream.Stream;3738/*39* Launches a new Java process with a main class packed inside a module.40*/4142public class TestProcessModuleLauncher extends TestProcessLauncher {4344private static final Path TEST_MODULES = USER_DIR.resolve("testmodules");45private static final String MODULE_NAME = "module1";4647public TestProcessModuleLauncher(String className) {48super(className);49}5051protected String prepareLaunch(String javaExec, String pipePort) {52try {53prepareModule();54return javaExec + " --module-path " + TEST_MODULES.toFile().getAbsolutePath() +55" -m " + MODULE_NAME + "/" + className + " -pipe.port=" + pipePort;56} catch (IOException e) {57throw new RuntimeException("Failed to prepare a jar file", e);58}59}6061private void prepareModule() throws IOException {62TEST_MODULES.toFile().mkdirs();63Path moduleJar = TEST_MODULES.resolve("mod1.jar");64ModuleDescriptor md = createModuleDescriptor();65createModuleJarFile(moduleJar, md, TEST_CLASSES_DIR, Paths.get("."));66}6768private ModuleDescriptor createModuleDescriptor() {69ModuleDescriptor.Builder builder70= ModuleDescriptor.newModule(MODULE_NAME).requires("java.base");71return builder.build();72}7374private static void createModuleJarFile(Path jarfile, ModuleDescriptor md, Path dir, Path... files)75throws IOException {7677Path parent = jarfile.getParent();78if (parent != null) {79Files.createDirectories(parent);80}8182List<Path> entries = findAllRegularFiles(dir, files);8384try (OutputStream out = Files.newOutputStream(jarfile);85JarOutputStream jos = new JarOutputStream(out)) {86if (md != null) {87JarEntry je = new JarEntry("module-info.class");88jos.putNextEntry(je);89ModuleInfoWriter.write(md, jos);90jos.closeEntry();91}9293for (Path entry : entries) {94String name = toJarEntryName(entry);95jos.putNextEntry(new JarEntry(name));96Files.copy(dir.resolve(entry), jos);97jos.closeEntry();98}99}100}101102private static String toJarEntryName(Path file) {103Path normalized = file.normalize();104return normalized.subpath(0, normalized.getNameCount())105.toString()106.replace(File.separatorChar, '/');107}108109private static List<Path> findAllRegularFiles(Path dir, Path[] files) throws IOException {110List<Path> entries = new ArrayList<>();111for (Path file : files) {112try (Stream<Path> stream = Files.find(dir.resolve(file), Integer.MAX_VALUE,113(p, attrs) -> attrs.isRegularFile() && !p.getParent().equals(dir.resolve(".")))) {114stream.map(dir::relativize)115.forEach(entries::add);116}117}118return entries;119}120121public String getModuleName() {122return MODULE_NAME;123}124}125126127