From 9cad419f9a7899558fdaee1e66c210b3cd68a3c9 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 28 Sep 2022 14:08:29 +0800 Subject: [PATCH] Keep the temp init scripts on disk - The temp files will not be deleted on JVM exist, since it will cause buildship fail when get Eclipse project instance after the server restarts. - Cache the temp files for each session, to make sure there will not be too many temp files generated when reloading projects. Signed-off-by: Sheng Chen --- .../internal/managers/GradleProjectImporter.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java index 41ac7725ad..335667ae86 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -129,6 +130,12 @@ public class GradleProjectImporter extends AbstractProjectImporter { .replaceAll("\n", System.lineSeparator()); //@formatter:on + /** + * when the init scripts are generated in the temp folder, we cache them here, + * to make sure that only one piece of the scripts will be generated for each session. + */ + private static Map initScriptFileCache = new HashMap<>(); + /* (non-Javadoc) * @see org.eclipse.jdt.ls.core.internal.managers.IProjectImporter#applies(org.eclipse.core.runtime.IProgressMonitor) */ @@ -629,10 +636,16 @@ private static File getGradleInitScript(String scriptPath) { * Create a temp file as the Gradle init script. */ private static File getGradleInitScriptTempFile(String scriptPath) { + File cachedFile = initScriptFileCache.get(scriptPath); + if (cachedFile != null && cachedFile.exists() && cachedFile.length() > 0) { + return cachedFile; + } try (InputStream input = JavaLanguageServerPlugin.class.getResourceAsStream(scriptPath)) { File initScript = File.createTempFile("init", ".gradle"); - initScript.deleteOnExit(); + // TODO: need to think about how to safely remove the temp files. + // initScript.deleteOnExit(); Files.copy(input, initScript.toPath(), StandardCopyOption.REPLACE_EXISTING); + initScriptFileCache.put(scriptPath, initScript); return initScript; } catch (IOException e) { JavaLanguageServerPlugin.logException(e);