Skip to content

Commit

Permalink
Process EE_HOME lazy in ExecutionEnvironmentDescription
Browse files Browse the repository at this point in the history
This avoids the need to specify the 'ee.home' property for all
descriptions even if all paths are already absolute and the '${ee.home}'
variable is not used and therefore ee.home is effectively unused.
  • Loading branch information
HannesWell authored and akurtakov committed Apr 18, 2024
1 parent 68a2c3f commit f46097a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package org.eclipse.jdt.debug.tests.core;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
Expand All @@ -42,7 +46,7 @@ public class EEDefinitionTests extends AbstractDebugTest {

public static IPath TEST_EE_FILE = null;
{
if (Platform.OS_WIN32.equals(Platform.getOS())) {
if (Platform.OS.isWindows()) {
TEST_EE_FILE = new Path("testfiles/test-jre/bin/test-foundation11-win32.ee");
} else {
TEST_EE_FILE = new Path("testfiles/test-jre/bin/test-foundation11.ee");
Expand Down Expand Up @@ -155,17 +159,12 @@ public void testDefaultLibraries() {
/**
* Tests that a javadoc location can be specified.
*/
public void testJavadocLocation() throws CoreException {
public void testJavadocLocation() throws CoreException, MalformedURLException {
File file = getEEFile();
assertNotNull("Missing EE file", file);
ExecutionEnvironmentDescription ee = new ExecutionEnvironmentDescription(file);
URL location = EEVMType.getJavadocLocation(ee);
URL expectedLocation = null;
try {
expectedLocation = new URL("http://a.javadoc.location");
} catch (MalformedURLException e) {
fail();
}
URL expectedLocation = new URL("http://a.javadoc.location");
assertEquals("Incorrect javadoc location", expectedLocation, location);
}

Expand All @@ -179,12 +178,7 @@ public void testIndexLocation() throws Exception {
assertNotNull("Missing EE file", file);
ExecutionEnvironmentDescription ee = new ExecutionEnvironmentDescription(file);
URL location = EEVMType.getIndexLocation(ee);
URL expectedLocation = null;
try {
expectedLocation = new URL("http://a.index.location");
} catch (MalformedURLException e) {
fail();
}
URL expectedLocation = new URL("http://a.index.location");
assertEquals("Incorrect index location", expectedLocation, location);
}

Expand Down Expand Up @@ -292,4 +286,23 @@ public void testEmptyProperty() throws CoreException {
ExecutionEnvironmentDescription ee = new ExecutionEnvironmentDescription(file);
validateProperty("-Dee.empty", "", ee);
}

public void testNoEEHomeRequiredForAbsolutePaths() throws IOException {
IVMInstall jre = JavaRuntime.getDefaultVMInstall();
IExecutionEnvironment ee = Arrays.stream(JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments()) //
.filter(e -> e.isStrictlyCompatible(jre)).findFirst().orElseThrow();
String installLocation = jre.getInstallLocation().getCanonicalPath();
File exePath = new File(jre.getInstallLocation(), "bin/" + (Platform.OS.isWindows() ? "java.exe" : "java"));
List<String> libraryPaths = Arrays.stream(JavaRuntime.getLibraryLocations(jre)) //
.map(LibraryLocation::getSystemLibraryPath).map(IPath::toOSString).toList();

ExecutionEnvironmentDescription description = new ExecutionEnvironmentDescription(Map.of( //
ExecutionEnvironmentDescription.JAVA_HOME, installLocation, //
ExecutionEnvironmentDescription.CLASS_LIB_LEVEL, ee.getId(), //
ExecutionEnvironmentDescription.EXECUTABLE, exePath.toString(), //
ExecutionEnvironmentDescription.BOOT_CLASS_PATH, String.join(File.pathSeparator, libraryPaths)));

assertTrue(description.getLibraryLocations().length > 0);
assertNotNull(description.getExecutable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.internal.launching.EEVMType;
import org.eclipse.jdt.internal.launching.LaunchingMessages;
Expand Down Expand Up @@ -232,6 +232,10 @@ public String getProperty(String property) {
return fProperties.get(property);
}

private Optional<String> getEEHomePath() {
return Optional.ofNullable(getProperty(EE_HOME));
}

/**
* Returns the location of the system libraries defined in this execution environment.
* Libraries are generated from the endorsed directories, boot class path, additional
Expand Down Expand Up @@ -259,10 +263,10 @@ public LibraryLocation[] getLibraryLocations() {
URL url = getJavadocLocation();
URL indexurl = getIndexLocation();
for (int i = 0; i < bootpath.length; i++) {
IPath path = new Path(bootpath[i]);
IPath path = IPath.fromOSString(bootpath[i]);
File lib = path.toFile();
if (lib.exists() && lib.isFile()) {
LibraryLocation libraryLocation = new LibraryLocation(path, src, Path.EMPTY, url, indexurl);
LibraryLocation libraryLocation = new LibraryLocation(path, src, IPath.EMPTY, url, indexurl);
boot.add(libraryLocation);
}
}
Expand Down Expand Up @@ -302,7 +306,7 @@ public LibraryLocation[] getLibraryLocations() {
*/
public String getVMArguments() {
StringBuilder arguments = new StringBuilder();
String eeHome = fProperties.get(EE_HOME);
Optional<String> eeHome = getEEHomePath();
fProperties.forEach((key, value) -> {
boolean appendArgument = !key.startsWith(EE_ARG_FILTER);
if (appendArgument) {
Expand Down Expand Up @@ -390,7 +394,7 @@ private static Map<String, String> loadProperties(File eeFile) throws CoreExcept
}

private static Map<String, String> resolveEEHome(Map<String, String> eeProperties) { // resolve things with ${ee.home} in them
String eeHome = eeProperties.get(EE_HOME);
Optional<String> eeHome = Optional.ofNullable(eeProperties.get(EE_HOME));
eeProperties.replaceAll((k, value) -> value != null ? resolveHome(value, eeHome) : ""); //$NON-NLS-1$
return eeProperties;
}
Expand All @@ -402,7 +406,7 @@ private static Map<String, String> resolveEEHome(Map<String, String> eePropertie
* @param value string to process
* @return resolved string
*/
private static String resolveHome(String value, String eeHome) {
private static String resolveHome(String value, Optional<String> eeHome) {
int start = 0;
int index = value.indexOf(VAR_EE_HOME, start);
StringBuilder replaced = null;
Expand All @@ -411,7 +415,7 @@ private static String resolveHome(String value, String eeHome) {
replaced = new StringBuilder();
}
replaced.append(value.substring(start, index));
replaced.append(eeHome);
replaced.append(eeHome.orElseThrow());
start = index + VAR_EE_HOME.length();
index = value.indexOf(VAR_EE_HOME, start);
}
Expand All @@ -431,8 +435,7 @@ private static String resolveHome(String value, String eeHome) {
*/
private String[] resolvePaths(String paths) {
String[] strings = paths.split(File.pathSeparator, -1);
String eeHome = getProperty(EE_HOME);
IPath root = new Path(eeHome);
Optional<String> root = getEEHomePath();
for (int i = 0; i < strings.length; i++) {
strings[i] = makePathAbsolute(strings[i], root);
}
Expand All @@ -448,10 +451,10 @@ private String[] resolvePaths(String paths) {
* @param root root to append non-absolute paths to
* @return absolute, OS specific path
*/
private String makePathAbsolute(String pathString, IPath root){
IPath path = new Path(pathString.trim());
private String makePathAbsolute(String pathString, Optional<String> root) {
IPath path = IPath.fromOSString(pathString.trim());
if (!path.isEmpty() && !path.isAbsolute()) {
IPath filePath = root.append(path);
IPath filePath = IPath.fromOSString(root.orElseThrow()).append(path);
return filePath.toOSString();
}
return path.toOSString();
Expand Down Expand Up @@ -483,7 +486,7 @@ private Map<String, String> getSourceMap(){
for (int i = 0; i < entries.length; i++) {
int index = entries[i].indexOf('=');
if (index > 0 && index < entries[i].length()-1){
IPath root = new Path(getProperty(EE_HOME));
Optional<String> root = getEEHomePath();
String key = entries[i].substring(0,index);
String value = entries[i].substring(index+1);
key = makePathAbsolute(key, root);
Expand Down Expand Up @@ -562,7 +565,7 @@ private void addSourceLocationsToLibraries(Map<String, String> srcMap, List<Libr
if (matcher.find()) {
// Found a file that the pattern applies to, use the map to get the source location
String sourceLocation = matcher.replaceAll(value);
IPath sourcePath = new Path(sourceLocation);
IPath sourcePath = IPath.fromOSString(sourceLocation);
// Only add the source archive if it exists
if (sourcePath.toFile().exists()) {
currentLibrary.setSystemLibrarySource(sourcePath);
Expand All @@ -582,11 +585,11 @@ private void addSourceLocationsToLibraries(Map<String, String> srcMap, List<Libr
private IPath getSourceLocation() {
String src = getProperty(ExecutionEnvironmentDescription.SOURCE_DEFAULT);
if (src != null) {
String eeHome = getProperty(ExecutionEnvironmentDescription.EE_HOME);
src = makePathAbsolute(src, new Path(eeHome));
return new Path(src);
Optional<String> eeHome = getEEHomePath();
src = makePathAbsolute(src, eeHome);
return IPath.fromOSString(src);
}
return Path.EMPTY;
return IPath.EMPTY;
}

/**
Expand Down

0 comments on commit f46097a

Please sign in to comment.