From a3830dbdc045ba4c407270080a54a89c12b17c5d Mon Sep 17 00:00:00 2001 From: Geert Bevin Date: Fri, 19 Jul 2024 00:02:32 -0400 Subject: [PATCH] Fix to cache so that it doesn't hold on to a parsed properties file --- src/main/java/rife/bld/BldCache.java | 43 +++++++++++-------- .../wrapper/TestWrapperExtensionResolver.java | 1 - 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/rife/bld/BldCache.java b/src/main/java/rife/bld/BldCache.java index f2c0c26..a32adcb 100644 --- a/src/main/java/rife/bld/BldCache.java +++ b/src/main/java/rife/bld/BldCache.java @@ -48,7 +48,6 @@ public class BldCache { private static final String PROPERTY_DEPENDENCIES_HASH = PROPERTY_DEPENDENCIES_PREFIX + PROPERTY_SUFFIX_HASH; private final File hashFile_; - private final Properties hashProperties_ = new Properties(); private final VersionResolution resolution_; private String extensionsHash_; private String dependenciesHash_; @@ -59,14 +58,6 @@ public BldCache(File bldLibDir, VersionResolution resolution) { new File(bldLibDir, WRAPPER_PROPERTIES_HASH).delete(); new File(bldLibDir, BLD_BUILD_HASH).delete(); - - if (hashFile_.exists()) { - try { - hashProperties_.load(new FileInputStream(hashFile_)); - } catch (IOException e) { - // no-op, we'll store a new properties file when we're writing the cache - } - } } public void fingerprintExtensions(Collection repositories, Collection extensions, boolean downloadSources, boolean downloadJavadoc) { @@ -121,16 +112,29 @@ public boolean isExtensionHashValid() { return validateExtensionsHash(extensionsHash_); } + private Properties hashProperties() { + var properties = new Properties(); + if (hashFile_.exists()) { + try { + properties.load(new FileInputStream(hashFile_)); + } catch (IOException e) { + // no-op, we'll store a new properties file when we're writing the cache + } + } + return properties; + } + private boolean validateExtensionsHash(String hash) { - if (!hashFile_.exists() || hashProperties_.isEmpty()) { + var properties = hashProperties(); + if (!hashFile_.exists() || properties.isEmpty()) { return false; } - if (!hash.equals(hashProperties_.getProperty(PROPERTY_EXTENSIONS_HASH))) { + if (!hash.equals(properties.getProperty(PROPERTY_EXTENSIONS_HASH))) { return false; } - var local_files = hashProperties_.getProperty(PROPERTY_EXTENSIONS_LOCAL); + var local_files = properties.getProperty(PROPERTY_EXTENSIONS_LOCAL); if (local_files != null && !local_files.isEmpty()) { var lines = StringUtils.split(local_files, "\n"); if (!lines.isEmpty()) { @@ -164,11 +168,12 @@ public boolean isDependenciesHashValid() { } private boolean validateDependenciesHash(String hash) { - if (!hashFile_.exists() || hashProperties_.isEmpty()) { + var properties = hashProperties(); + if (!hashFile_.exists() || properties.isEmpty()) { return false; } - return hash.equals(hashProperties_.getProperty(PROPERTY_DEPENDENCIES_HASH)); + return hash.equals(properties.getProperty(PROPERTY_DEPENDENCIES_HASH)); } public void writeCache() { @@ -176,9 +181,11 @@ public void writeCache() { } public void writeCache(List extensionsLocalArtifacts) { + var properties = hashProperties(); + try { if (extensionsHash_ != null) { - hashProperties_.put(PROPERTY_EXTENSIONS_HASH, extensionsHash_); + properties.put(PROPERTY_EXTENSIONS_HASH, extensionsHash_); } if (extensionsLocalArtifacts != null) { @@ -188,15 +195,15 @@ public void writeCache(List extensionsLocalArtifacts) { extensions_local.append("\n").append(file.lastModified()).append(':').append(file.getAbsolutePath()); } } - hashProperties_.put(PROPERTY_EXTENSIONS_LOCAL, extensions_local.toString()); + properties.put(PROPERTY_EXTENSIONS_LOCAL, extensions_local.toString()); } if (dependenciesHash_ != null) { - hashProperties_.put(PROPERTY_DEPENDENCIES_HASH, dependenciesHash_); + properties.put(PROPERTY_DEPENDENCIES_HASH, dependenciesHash_); } hashFile_.getParentFile().mkdirs(); - hashProperties_.store(new FileOutputStream(hashFile_), null); + properties.store(new FileOutputStream(hashFile_), null); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/test/java/rife/bld/wrapper/TestWrapperExtensionResolver.java b/src/test/java/rife/bld/wrapper/TestWrapperExtensionResolver.java index 576169e..02b1244 100644 --- a/src/test/java/rife/bld/wrapper/TestWrapperExtensionResolver.java +++ b/src/test/java/rife/bld/wrapper/TestWrapperExtensionResolver.java @@ -606,7 +606,6 @@ void testUpdateHash() bld.cache""", String.join("\n", files3)); FileUtils.writeString("updated", hash_file); - resolver = new WrapperExtensionResolver(tmp1, tmp2, new Properties(), new Properties(), List.of(MAVEN_CENTRAL), List.of("org.antlr:antlr4:4.11.1"), false, false); resolver.updateExtensions(); var files4 = FileUtils.getFileList(tmp2); assertEquals(10, files4.size());