1 /*
   2  * Copyright (c) 2019, 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 
  24 import jdk.internal.module.ModuleInfoWriter;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.OutputStream;
  29 import java.lang.module.ModuleDescriptor;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import java.util.jar.JarEntry;
  36 import java.util.jar.JarOutputStream;
  37 import java.util.stream.Stream;
  38 
  39 /*
  40  * Launches a new Java process with a main class packed inside a module.
  41  */
  42 
  43 public class TestProcessModuleLauncher extends TestProcessLauncher {
  44 
  45     private static final Path TEST_MODULES = USER_DIR.resolve("testmodules");
  46     private static final String MODULE_NAME = "module1";
  47 
  48     public TestProcessModuleLauncher(String className) {
  49         super(className);
  50     }
  51 
  52     protected String prepareLaunch(String javaExec, String pipePort) {
  53         try {
  54             prepareModule();
  55             return javaExec + " --module-path " + TEST_MODULES.toFile().getAbsolutePath() +
  56                     " -m " + MODULE_NAME + "/" + className + " -pipe.port=" + pipePort;
  57         } catch (IOException e) {
  58             throw new RuntimeException("Failed to prepare a jar file", e);
  59         }
  60     }
  61 
  62     private void prepareModule() throws IOException {
  63         TEST_MODULES.toFile().mkdirs();
  64         Path moduleJar = TEST_MODULES.resolve("mod1.jar");
  65         ModuleDescriptor md = createModuleDescriptor();
  66         createModuleJarFile(moduleJar, md, TEST_CLASSES_DIR, Paths.get("."));
  67     }
  68 
  69     private ModuleDescriptor createModuleDescriptor() {
  70         ModuleDescriptor.Builder builder
  71                 = ModuleDescriptor.newModule(MODULE_NAME).requires("java.base");
  72         return builder.build();
  73     }
  74 
  75     private static void createModuleJarFile(Path jarfile, ModuleDescriptor md, Path dir, Path... files)
  76             throws IOException {
  77 
  78         Path parent = jarfile.getParent();
  79         if (parent != null) {
  80             Files.createDirectories(parent);
  81         }
  82 
  83         List<Path> entries = findAllRegularFiles(dir, files);
  84 
  85         try (OutputStream out = Files.newOutputStream(jarfile);
  86              JarOutputStream jos = new JarOutputStream(out)) {
  87             if (md != null) {
  88                 JarEntry je = new JarEntry("module-info.class");
  89                 jos.putNextEntry(je);
  90                 ModuleInfoWriter.write(md, jos);
  91                 jos.closeEntry();
  92             }
  93 
  94             for (Path entry : entries) {
  95                 String name = toJarEntryName(entry);
  96                 jos.putNextEntry(new JarEntry(name));
  97                 Files.copy(dir.resolve(entry), jos);
  98                 jos.closeEntry();
  99             }
 100         }
 101     }
 102 
 103     private static String toJarEntryName(Path file) {
 104         Path normalized = file.normalize();
 105         return normalized.subpath(0, normalized.getNameCount())
 106                 .toString()
 107                 .replace(File.separatorChar, '/');
 108     }
 109 
 110     private static List<Path> findAllRegularFiles(Path dir, Path[] files) throws IOException {
 111         List<Path> entries = new ArrayList<>();
 112         for (Path file : files) {
 113             try (Stream<Path> stream = Files.find(dir.resolve(file), Integer.MAX_VALUE,
 114                     (p, attrs) -> attrs.isRegularFile() && !p.getParent().equals(dir.resolve(".")))) {
 115                 stream.map(dir::relativize)
 116                         .forEach(entries::add);
 117             }
 118         }
 119         return entries;
 120     }
 121 
 122     public String getModuleName() {
 123         return MODULE_NAME;
 124     }
 125 }