test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessModuleLauncher.java
author dtitov
Mon, 08 Apr 2019 17:09:02 +0000
changeset 54460 6733a9176cce
permissions -rw-r--r--
8221730: jcmd process name matching broken Reviewed-by: jcbeyler, dholmes, cjplummer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     1
/*
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     4
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     8
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    13
 * accompanied this code).
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    14
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    18
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    21
 * questions.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    22
 */
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    23
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    24
import jdk.internal.module.ModuleInfoWriter;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    25
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    26
import java.io.File;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    27
import java.io.IOException;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    28
import java.io.OutputStream;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    30
import java.nio.file.Files;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    31
import java.nio.file.Path;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    32
import java.nio.file.Paths;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    33
import java.util.ArrayList;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    34
import java.util.List;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    35
import java.util.jar.JarEntry;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    36
import java.util.jar.JarOutputStream;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    37
import java.util.stream.Stream;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    38
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    39
/*
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    40
 * Launches a new Java process with a main class packed inside a module.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    41
 */
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    42
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    43
public class TestProcessModuleLauncher extends TestProcessLauncher {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    44
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    45
    private static final Path TEST_MODULES = USER_DIR.resolve("testmodules");
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    46
    private static final String MODULE_NAME = "module1";
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    47
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    48
    public TestProcessModuleLauncher(String className) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    49
        super(className);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    50
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    51
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    52
    protected String prepareLaunch(String javaExec, String pipePort) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    53
        try {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    54
            prepareModule();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    55
            return javaExec + " --module-path " + TEST_MODULES.toFile().getAbsolutePath() +
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    56
                    " -m " + MODULE_NAME + "/" + className + " -pipe.port=" + pipePort;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    57
        } catch (IOException e) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    58
            throw new RuntimeException("Failed to prepare a jar file", e);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    59
        }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    60
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    61
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    62
    private void prepareModule() throws IOException {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    63
        TEST_MODULES.toFile().mkdirs();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    64
        Path moduleJar = TEST_MODULES.resolve("mod1.jar");
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    65
        ModuleDescriptor md = createModuleDescriptor();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    66
        createModuleJarFile(moduleJar, md, TEST_CLASSES_DIR, Paths.get("."));
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    67
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    68
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    69
    private ModuleDescriptor createModuleDescriptor() {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    70
        ModuleDescriptor.Builder builder
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    71
                = ModuleDescriptor.newModule(MODULE_NAME).requires("java.base");
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    72
        return builder.build();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    73
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    74
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    75
    private static void createModuleJarFile(Path jarfile, ModuleDescriptor md, Path dir, Path... files)
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    76
            throws IOException {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    77
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    78
        Path parent = jarfile.getParent();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    79
        if (parent != null) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    80
            Files.createDirectories(parent);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    81
        }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    82
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    83
        List<Path> entries = findAllRegularFiles(dir, files);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    84
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    85
        try (OutputStream out = Files.newOutputStream(jarfile);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    86
             JarOutputStream jos = new JarOutputStream(out)) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    87
            if (md != null) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    88
                JarEntry je = new JarEntry("module-info.class");
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    89
                jos.putNextEntry(je);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    90
                ModuleInfoWriter.write(md, jos);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    91
                jos.closeEntry();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    92
            }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    93
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    94
            for (Path entry : entries) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    95
                String name = toJarEntryName(entry);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    96
                jos.putNextEntry(new JarEntry(name));
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    97
                Files.copy(dir.resolve(entry), jos);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    98
                jos.closeEntry();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    99
            }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   100
        }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   101
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   102
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   103
    private static String toJarEntryName(Path file) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   104
        Path normalized = file.normalize();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   105
        return normalized.subpath(0, normalized.getNameCount())
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   106
                .toString()
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   107
                .replace(File.separatorChar, '/');
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   108
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   109
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   110
    private static List<Path> findAllRegularFiles(Path dir, Path[] files) throws IOException {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   111
        List<Path> entries = new ArrayList<>();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   112
        for (Path file : files) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   113
            try (Stream<Path> stream = Files.find(dir.resolve(file), Integer.MAX_VALUE,
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   114
                    (p, attrs) -> attrs.isRegularFile() && !p.getParent().equals(dir.resolve(".")))) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   115
                stream.map(dir::relativize)
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   116
                        .forEach(entries::add);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   117
            }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   118
        }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   119
        return entries;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   120
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   121
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   122
    public String getModuleName() {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   123
        return MODULE_NAME;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   124
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
   125
}