diff --git a/biz.aQute.bndlib.tests/test/aQute/bnd/osgi/ActivelyClosingClassLoaderTest.java b/biz.aQute.bndlib.tests/test/aQute/bnd/osgi/ActivelyClosingClassLoaderTest.java index 662670e42f..8a38bb9734 100644 --- a/biz.aQute.bndlib.tests/test/aQute/bnd/osgi/ActivelyClosingClassLoaderTest.java +++ b/biz.aQute.bndlib.tests/test/aQute/bnd/osgi/ActivelyClosingClassLoaderTest.java @@ -13,6 +13,7 @@ import java.net.URL; import java.util.Enumeration; import java.util.Optional; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; @@ -38,8 +39,8 @@ public void happycase() throws Exception { .get(osgi); assertEquals(wrapper.file, osgi); - Thread.sleep(100); - ac.purge(System.currentTimeMillis()); + Thread.sleep(100L); + ac.purge(0L); synchronized (wrapper) { assertNull(wrapper.jarFile); } @@ -120,8 +121,8 @@ public void testWorkspace() throws Exception { Wrapper wrapper = cl.wrappers.get() .get(IO.getFile("jar/thinlet.jar")); - cl.autopurge(10); - Thread.sleep(500); + cl.autopurge(TimeUnit.MILLISECONDS.toNanos(10L)); + Thread.sleep(500L); synchronized (wrapper) { assertNull(wrapper.jarFile); } diff --git a/biz.aQute.bndlib/src/aQute/bnd/osgi/ActivelyClosingClassLoader.java b/biz.aQute.bndlib/src/aQute/bnd/osgi/ActivelyClosingClassLoader.java index fffe3095f7..c875920b5e 100644 --- a/biz.aQute.bndlib/src/aQute/bnd/osgi/ActivelyClosingClassLoader.java +++ b/biz.aQute.bndlib/src/aQute/bnd/osgi/ActivelyClosingClassLoader.java @@ -1,6 +1,6 @@ package aQute.bnd.osgi; -import static java.util.Collections.enumeration; +import static aQute.lib.collections.Enumerations.enumeration; import static java.util.stream.Collectors.toList; import java.io.Closeable; @@ -69,7 +69,7 @@ synchronized byte[] getData(String name) { if (resource == null) { return null; } - lastAccess = System.currentTimeMillis(); + lastAccess = System.nanoTime(); return IO.read(resource.openInputStream()); } catch (Exception e) { processor.exception(e, "while loading resource %s from %s: %s", name, file, e.getMessage()); @@ -157,23 +157,21 @@ public String getContentType() { @Override public Enumeration findResources(String name) { - List resources = dataStream(name).map(data -> createURL(name, data)) - .collect(toList()); - return enumeration(resources); + return enumeration(dataStream(name).spliterator(), data -> createURL(name, data)); } /** - * This method will close any open files that have not been accessed since - * purgeTime + * This method will close any open files that have not been accessed within + * the specified delta cutoff time. * - * @param purgeTime the absolute cutoff time + * @param purgeTime The delta cutoff time in nanoseconds. */ - void purge(long purgeTime) { + long now = System.nanoTime(); wrappers.get() .values() .stream() - .filter(w -> w.lastAccess < purgeTime) + .filter(w -> now - w.lastAccess >= purgeTime) .forEach(Wrapper::close); } @@ -235,11 +233,16 @@ public Class loadClass(String name) throws ClassNotFoundException { } } + /** + * This method will schedule closing any open files that have not been + * accessed within the specified time interval. + * + * @param freshPeriod The time interval in nanoseconds. + */ void autopurge(long freshPeriod) { schedule = Processor.getScheduledExecutor() - .scheduleWithFixedDelay(() -> { - purge(System.currentTimeMillis() - freshPeriod); - }, freshPeriod, freshPeriod, TimeUnit.MILLISECONDS); + .scheduleWithFixedDelay(() -> purge(freshPeriod), freshPeriod, freshPeriod, + TimeUnit.NANOSECONDS); } } diff --git a/biz.aQute.bndlib/src/aQute/bnd/osgi/Processor.java b/biz.aQute.bndlib/src/aQute/bnd/osgi/Processor.java index f374e3efe9..e24e24749d 100644 --- a/biz.aQute.bndlib/src/aQute/bnd/osgi/Processor.java +++ b/biz.aQute.bndlib/src/aQute/bnd/osgi/Processor.java @@ -1476,7 +1476,7 @@ private CloseableMemoize newPluginLoader() { return CloseableMemoize.closeableSupplier(() -> { CL pluginLoader = new CL(this); if (IO.isWindows() && isInteractive()) { - pluginLoader.autopurge(5000); + pluginLoader.autopurge(TimeUnit.SECONDS.toNanos(5L)); } return pluginLoader; });