Skip to content

Commit

Permalink
8248254: jpackage fails if app module is in external runtime
Browse files Browse the repository at this point in the history
Reviewed-by: herrick, almatvee
  • Loading branch information
Alexey Semenyuk committed Jun 29, 2020
1 parent 1a4f314 commit d180fb3
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,34 @@

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleReference;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static jdk.incubator.jpackage.internal.StandardBundlerParam.PREDEFINED_RUNTIME_IMAGE;

/**
* Extracts data needed to run application from parameters.
*/
final class LauncherData {
boolean isModular() {
return moduleDescriptor != null;
return moduleInfo != null;
}

String qualifiedClassName() {
Expand All @@ -61,7 +70,7 @@ String packageName() {

String moduleName() {
verifyIsModular(true);
return moduleDescriptor.name();
return moduleInfo.name;
}

List<Path> modulePath() {
Expand All @@ -80,11 +89,7 @@ List<Path> classPath() {

String getAppVersion() {
if (isModular()) {
ModuleDescriptor.Version ver = moduleDescriptor.version().orElse(null);
if (ver != null) {
return ver.toString();
}
return moduleDescriptor.rawVersion().orElse(null);
return moduleInfo.version;
}

return null;
Expand All @@ -94,7 +99,7 @@ private LauncherData() {
}

private void verifyIsModular(boolean isModular) {
if ((moduleDescriptor != null) != isModular) {
if ((moduleInfo != null) != isModular) {
throw new IllegalStateException();
}
}
Expand All @@ -103,9 +108,19 @@ static LauncherData create(Map<String, ? super Object> params) throws
ConfigException, IOException {

final String mainModule = getMainModule(params);
final LauncherData result;
if (mainModule == null) {
return createNonModular(params);
result = createNonModular(params);
} else {
result = createModular(mainModule, params);
}
result.initClasspath(params);
return result;
}

private static LauncherData createModular(String mainModule,
Map<String, ? super Object> params) throws ConfigException,
IOException {

LauncherData launcherData = new LauncherData();

Expand All @@ -119,18 +134,34 @@ static LauncherData create(Map<String, ? super Object> params) throws
}
launcherData.modulePath = getModulePath(params);

launcherData.moduleDescriptor = JLinkBundlerHelper.createModuleFinder(
launcherData.modulePath).find(moduleName).orElseThrow(
() -> new ConfigException(MessageFormat.format(I18N.getString(
"error.no-module-in-path"), moduleName), null)).descriptor();
// Try to find module in the specified module path list.
ModuleReference moduleRef = JLinkBundlerHelper.createModuleFinder(
launcherData.modulePath).find(moduleName).orElse(null);

if (moduleRef != null) {
launcherData.moduleInfo = ModuleInfo.fromModuleDescriptor(
moduleRef.descriptor());
} else if (params.containsKey(PREDEFINED_RUNTIME_IMAGE.getID())) {
// Failed to find module in the specified module path list and
// there is external runtime given to jpackage.
// Lookup module in this runtime.
Path cookedRuntime = PREDEFINED_RUNTIME_IMAGE.fetchFrom(params).toPath();
launcherData.moduleInfo = ModuleInfo.fromCookedRuntime(moduleName,
cookedRuntime);
}

if (launcherData.moduleInfo == null) {
throw new ConfigException(MessageFormat.format(I18N.getString(
"error.no-module-in-path"), moduleName), null);
}

if (launcherData.qualifiedClassName == null) {
launcherData.qualifiedClassName = launcherData.moduleDescriptor.mainClass().orElseThrow(
() -> new ConfigException(I18N.getString("ERR_NoMainClass"),
null));
launcherData.qualifiedClassName = launcherData.moduleInfo.mainClass;
if (launcherData.qualifiedClassName == null) {
throw new ConfigException(I18N.getString("ERR_NoMainClass"), null);
}
}

launcherData.initClasspath(params);
return launcherData;
}

Expand Down Expand Up @@ -191,7 +222,6 @@ private static LauncherData createNonModular(
launcherData.mainJarName));
}

launcherData.initClasspath(params);
return launcherData;
}

Expand Down Expand Up @@ -259,8 +289,17 @@ private static Path getPathParam(Map<String, ? super Object> params,

private static List<Path> getModulePath(Map<String, ? super Object> params)
throws ConfigException {
return getPathListParameter(Arguments.CLIOptions.MODULE_PATH.getId(),
params);
List<Path> modulePath = getPathListParameter(Arguments.CLIOptions.MODULE_PATH.getId(), params);

if (params.containsKey(PREDEFINED_RUNTIME_IMAGE.getID())) {
Path runtimePath = PREDEFINED_RUNTIME_IMAGE.fetchFrom(params).toPath();
runtimePath = runtimePath.resolve("lib");
modulePath = Stream.of(modulePath, List.of(runtimePath))
.flatMap(List::stream)
.collect(Collectors.toUnmodifiableList());
}

return modulePath;
}

private static List<Path> getPathListParameter(String paramName,
Expand All @@ -277,5 +316,77 @@ private static List<Path> getPathListParameter(String paramName,
private Path mainJarName;
private List<Path> classPath;
private List<Path> modulePath;
private ModuleDescriptor moduleDescriptor;
private ModuleInfo moduleInfo;

private static final class ModuleInfo {
String name;
String version;
String mainClass;

static ModuleInfo fromModuleDescriptor(ModuleDescriptor md) {
ModuleInfo result = new ModuleInfo();
result.name = md.name();
result.mainClass = md.mainClass().orElse(null);

ModuleDescriptor.Version ver = md.version().orElse(null);
if (ver != null) {
result.version = ver.toString();
} else {
result.version = md.rawVersion().orElse(null);
}

return result;
}

static ModuleInfo fromCookedRuntime(String moduleName,
Path cookedRuntime) {
Objects.requireNonNull(moduleName);

// We can't extract info about version and main class of a module
// linked in external runtime without running ModuleFinder in that
// runtime. But this is too much work as the runtime might have been
// coocked without native launchers. So just make sure the module
// is linked in the runtime by simply analysing the data
// of `release` file.

final Path releaseFile;
if (!Platform.isMac()) {
releaseFile = cookedRuntime.resolve("release");
} else {
// On Mac `cookedRuntime` can be runtime root or runtime home.
Path runtimeHome = cookedRuntime.resolve("Contents/Home");
if (!Files.isDirectory(runtimeHome)) {
runtimeHome = cookedRuntime;
}
releaseFile = runtimeHome.resolve("release");
}

try (Reader reader = Files.newBufferedReader(releaseFile)) {
Properties props = new Properties();
props.load(reader);
String moduleList = props.getProperty("MODULES");
if (moduleList == null) {
return null;
}

if ((moduleList.startsWith("\"") && moduleList.endsWith("\""))
|| (moduleList.startsWith("\'") && moduleList.endsWith(
"\'"))) {
moduleList = moduleList.substring(1, moduleList.length() - 1);
}

if (!List.of(moduleList.split("\\s+")).contains(moduleName)) {
return null;
}
} catch (IOException|IllegalArgumentException ex) {
Log.verbose(ex);
return null;
}

ModuleInfo result = new ModuleInfo();
result.name = moduleName;

return result;
}
}
}
11 changes: 6 additions & 5 deletions test/jdk/ProblemList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -924,10 +924,11 @@ jdk/jfr/event/os/TestThreadContextSwitches.java 8247776 windows-

# jdk_jpackage

tools/jpackage/share/EmptyFolderPackageTest.java 8248059 macosx-all
tools/jpackage/share/IconTest.java 8248059 macosx-all
tools/jpackage/share/AppImagePackageTest.java 8248059 macosx-all
tools/jpackage/share/SimplePackageTest.java 8248059 macosx-all
tools/jpackage/share/jdk/jpackage/tests/BasicTest.java 8248059 macosx-all
tools/jpackage/share/EmptyFolderPackageTest.java 8248059 macosx-all
tools/jpackage/share/IconTest.java 8248059 macosx-all
tools/jpackage/share/AppImagePackageTest.java 8248059 macosx-all
tools/jpackage/share/SimplePackageTest.java 8248059 macosx-all
tools/jpackage/share/jdk/jpackage/tests/BasicTest.java 8248059 macosx-all
tools/jpackage/share/jdk/jpackage/tests/ModulePathTest3.java#id0 8248418 generic-all

############################################################################
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public static JPackageCommand helloAppImage(String javaAppDesc) {
public static JPackageCommand helloAppImage(JavaAppDesc javaAppDesc) {
JPackageCommand cmd = new JPackageCommand();
cmd.setDefaultInputOutput().setDefaultAppName();
PackageType.IMAGE.applyTo(cmd);
cmd.setPackageType(PackageType.IMAGE);
new HelloApp(javaAppDesc).addTo(cmd);
return cmd;
}
Expand Down
Loading

0 comments on commit d180fb3

Please sign in to comment.