Skip to content

Commit

Permalink
Cache JRT FileSystem instances created
Browse files Browse the repository at this point in the history
- Originally submitted as
#2370
- Cleaned up code around new FileSystem creation for `jrt-fs.jar`
- Cleaned up / unified references to `jrt-fs` related code
  • Loading branch information
HannesWell authored and iloveeclipse committed Apr 22, 2024
1 parent a9b7222 commit f53ffcb
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,20 @@
public class ClasspathJep247 extends ClasspathJrt {

protected java.nio.file.FileSystem fs;
protected String compliance = null;
protected long jdklevel;
protected String releaseInHex = null;
protected String[] subReleases = null;
protected Path releasePath = null;
protected final String compliance;
protected final long jdklevel;
protected String releaseInHex;
protected String[] subReleases;
protected Path releasePath;
protected Set<String> packageCache;
protected File jdkHome;
protected String modulePath = null;
protected final File jdkHome;
protected String modulePath;

public ClasspathJep247(File jdkHome, String release, AccessRuleSet accessRuleSet) {
super(jdkHome, false, accessRuleSet, null);
super(new File(new File(jdkHome, "lib"), JRTUtil.JRT_FS_JAR), false, accessRuleSet, null); //$NON-NLS-1$
this.compliance = release;
this.jdklevel = CompilerOptions.releaseToJDKLevel(this.compliance);
this.jdkHome = jdkHome;
this.file = new File(new File(jdkHome, "lib"), "jrt-fs.jar"); //$NON-NLS-1$ //$NON-NLS-2$
}
@Override
public List<Classpath> fetchLinkedJars(FileSystem.ClasspathSectionProblemReporter problemReporter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@

@SuppressWarnings({"rawtypes", "unchecked"})
public class ClasspathJrt extends ClasspathLocation implements IMultiModuleEntry {
public File file;
public final File file;
protected ZipFile annotationZipFile;
protected boolean closeZipFileAtEnd;
protected final boolean closeZipFileAtEnd;
protected static Map<String, Map<String,IModule>> ModulesCache = new ConcurrentHashMap<>();
public final Set<String> moduleNamesCache;
//private Set<String> packageCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public static Classpath getClasspath(String classpathName, String encoding,
convertPathSeparators(destinationPath));
} else if (destinationPath == null) {
// class file only mode
if (classpathName.endsWith(JRTUtil.JRT_FS_JAR)) {
if (Util.isJrt(classpathName)) {
if (JRT_CLASSPATH_CACHE == null) {
JRT_CLASSPATH_CACHE = new HashMap<>();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ private boolean isArchive(File f) {
}

private boolean isJrt(File f) {
return f.getName().equalsIgnoreCase(JrtFileSystem.BOOT_MODULE);
return Util.isJrt(f.getName());
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -46,10 +42,6 @@

public class JrtFileSystem extends Archive {

private static URI JRT_URI = URI.create("jrt:/"); //$NON-NLS-1$

static final String BOOT_MODULE = "jrt-fs.jar"; //$NON-NLS-1$

public HashMap<String, Path> modulePathMap;
Path modules;
private java.nio.file.FileSystem jrtfs;
Expand All @@ -62,15 +54,9 @@ public JrtFileSystem(File file) throws ZipException, IOException {
public void initialize() throws IOException {
// initialize packages
this.modulePathMap = new HashMap<>();
URL jrtPath = null;

if (this.file.exists()) {
jrtPath = Paths.get(this.file.toPath().toString(), "lib", JRTUtil.JRT_FS_JAR).toUri().toURL(); //$NON-NLS-1$
try (URLClassLoader loader = new URLClassLoader(new URL[] { jrtPath })) {
HashMap<String, ?> env = new HashMap<>();
this.jrtfs = FileSystems.newFileSystem(JRT_URI, env, loader);
this.modules = this.jrtfs.getPath("/modules"); //$NON-NLS-1$
}
this.jrtfs = JRTUtil.getJrtFileSystem(this.file.getAbsolutePath());
this.modules = this.jrtfs.getPath(JRTUtil.MODULES_SUBDIR);
} else {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class JRTUtil {

public static final String JAVA_BASE = "java.base".intern(); //$NON-NLS-1$
public static final char[] JAVA_BASE_CHAR = JAVA_BASE.toCharArray();
static final String MODULES_SUBDIR = "/modules"; //$NON-NLS-1$
public static final String MODULES_SUBDIR = "/modules"; //$NON-NLS-1$
static final String[] DEFAULT_MODULE = new String[]{JAVA_BASE};
static final String[] NO_MODULE = new String[0];
static final String MULTIPLE = "MU"; //$NON-NLS-1$
Expand All @@ -76,6 +76,7 @@ public class JRTUtil {
* Map from JDK home path to ct.sym file (located in /lib in the JDK)
*/
private static final Map<Path, CtSym> ctSymFiles = new ConcurrentHashMap<>();
private static final Map<String, FileSystem> JRT_FILE_SYSTEMS = new ConcurrentHashMap<>();

static final SoftClassCache classCache = new SoftClassCache();

Expand Down Expand Up @@ -166,6 +167,28 @@ public static JrtFileSystem getJrtSystem(File image, String release) throws IOEx
}
}

/**
* @param path
* absolute path to java.home
* @return new {@link FileSystem} based on {@link #JRT_FS_JAR} for given Java home path.
* @throws IOException
* on any error
*/
public static FileSystem getJrtFileSystem(String path) throws IOException {
try {
FileSystem fs = JRT_FILE_SYSTEMS.computeIfAbsent(path, p -> {
try {
return FileSystems.newFileSystem(JRTUtil.JRT_URI, Map.of("java.home", p)); //$NON-NLS-1$
} catch (IOException e) {
throw new RuntimeIOException(e);
}
});
return fs;
} catch (RuntimeIOException e) {
throw e.getCause();
}
}

/**
* Convenient method to get access to the given archive as a {@link FileSystem}.
* <p>
Expand Down Expand Up @@ -531,9 +554,7 @@ public static JrtFileSystem getNewJrtFileSystem(Jdk jdk, String release) throws
this.jdk = jdkHome;
this.release = release;
JRTUtil.MODULE_TO_LOAD = System.getProperty("modules.to.load"); //$NON-NLS-1$
HashMap<String, String> env = new HashMap<>();
env.put("java.home", this.jdk.path); //$NON-NLS-1$
this.fs = FileSystems.newFileSystem(JRTUtil.JRT_URI, env);
this.fs = JRTUtil.getJrtFileSystem(this.jdk.path);
this.modRoot = this.fs.getPath(JRTUtil.MODULES_SUBDIR);
// Set up the root directory where modules are located
walkJrtForModules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,11 +796,10 @@ public final static boolean isJavaFileName(String name) {
}

/**
* Returns true iff str.toLowerCase().endsWith("jrt-fs.jar")
* implementation is not creating extra strings.
* @return true if name.endsWith("jrt-fs.jar")
*/
public final static boolean isJrt(String name) {
return name.endsWith(JRTUtil.JRT_FS_JAR);
return name != null && name.endsWith(JRTUtil.JRT_FS_JAR);
}

public static void reverseQuickSort(char[][] list, int left, int right) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.compiler.batch.FileSystem;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;

public class DefaultJavaRuntimeEnvironment extends FileSystem {

Expand All @@ -42,7 +41,7 @@ public static INameEnvironment[] create(String[] jreClasspaths, String release)
defaultJreClassLibs = new INameEnvironment[1];
Classpath[] classpath = new Classpath[jreClasspaths.length];
for(int i = 0; i < classpath.length; i++) {
if (jreClasspaths[i].endsWith(JRTUtil.JRT_FS_JAR)) {
if (org.eclipse.jdt.internal.compiler.util.Util.isJrt(jreClasspaths[i])) {
File file = new File(jreClasspaths[0]);
classpath[i] = FileSystem.getOlderSystemRelease(file.getParentFile().getParent(), release, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
import org.eclipse.jdt.internal.compiler.util.ObjectVector;
import org.eclipse.jdt.internal.core.DeltaProcessor.RootInfo;
import org.eclipse.jdt.internal.core.JavaProjectElementInfo.ProjectCache;
Expand Down Expand Up @@ -2937,11 +2936,11 @@ public JavaWorkspaceScope getWorkspaceScope() {
}

public static boolean isJrt(IPath path) {
return path.toString().endsWith(JRTUtil.JRT_FS_JAR);
return org.eclipse.jdt.internal.compiler.util.Util.isJrt(path.lastSegment());
}

public static boolean isJrt(String path) {
return isJrt(new Path(path));
return org.eclipse.jdt.internal.compiler.util.Util.isJrt(path);
}

public void verifyArchiveContent(IPath path) throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jdt.internal.core.search.indexing;

import static org.eclipse.jdt.internal.core.JavaModelManager.trace;
import static org.eclipse.jdt.internal.compiler.util.Util.isJrt;

import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -63,7 +64,6 @@
import org.eclipse.jdt.internal.compiler.SourceElementParser;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
import org.eclipse.jdt.internal.compiler.util.SimpleSet;
import org.eclipse.jdt.internal.core.ClasspathEntry;
Expand Down Expand Up @@ -736,9 +736,6 @@ private IndexRequest getRequest(Object target, IPath jPath, IndexLocation indexF
new AddJarFileToIndex(jPath, indexFile, this, updateIndex);
}

private boolean isJrt(String fileName) {
return fileName != null && fileName.endsWith(JRTUtil.JRT_FS_JAR);
}
/**
* Trigger addition of a library to an index
* Note: the actual operation is performed in background
Expand Down

0 comments on commit f53ffcb

Please sign in to comment.