From f92f9ae0d79b420df5e192384e0351500951fe10 Mon Sep 17 00:00:00 2001 From: Michael Offner Date: Fri, 20 Nov 2026 17:55:57 +0100 Subject: [PATCH] LDEV-6627 accept a bundle jar as additional bundle location, not only a directory --- .../lucee/extension/osgi/OSGiServiceImpl.java | 17 +++-- .../extension/osgi/BundleLocationTest.java | 64 +++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 source/java/test/org/lucee/extension/osgi/BundleLocationTest.java diff --git a/source/java/src/org/lucee/extension/osgi/OSGiServiceImpl.java b/source/java/src/org/lucee/extension/osgi/OSGiServiceImpl.java index a39f70e..b7d2342 100644 --- a/source/java/src/org/lucee/extension/osgi/OSGiServiceImpl.java +++ b/source/java/src/org/lucee/extension/osgi/OSGiServiceImpl.java @@ -295,8 +295,17 @@ private static Object[] read(File jar) { } } - private static File findJar(File dir, String name, String version) { - File[] jars = dir == null ? null : dir.listFiles((d, n) -> n.endsWith(".jar")); + /** + * the bundle jars of an additional bundle location: the jars in a directory, or the jar itself when the location is a jar file (this.javasettings.bundles accepts both) + */ + static File[] jars(File location) { + if (location == null) return null; + if (location.isFile()) return location.getName().endsWith(".jar") ? new File[] { location } : null; + return location.listFiles((d, n) -> n.endsWith(".jar")); + } + + static File findJar(File dir, String name, String version) { + File[] jars = jars(dir); if (jars == null) return null; File best = null; org.osgi.framework.Version bestVersion = null; @@ -329,7 +338,7 @@ private static File findJar(File dir, String name, VersionRange range) { } private static JarVersion findJarVersion(File dir, String name, VersionRange range) { - File[] jars = dir == null ? null : dir.listFiles((d, n) -> n.endsWith(".jar")); + File[] jars = jars(dir); if (jars == null) return null; File best = null; org.osgi.framework.Version bestVersion = null; @@ -600,7 +609,7 @@ private boolean loadMissing(String message, File[] dirs, Set tried, List /** a bundle in the directory that exports the package in a version inside the range */ private static File findExporting(File dir, String pkg, VersionRange range) { - File[] jars = dir == null ? null : dir.listFiles((d, n) -> n.endsWith(".jar")); + File[] jars = jars(dir); if (jars == null) return null; for (File jar: jars) { try (JarFile jf = new JarFile(jar)) { diff --git a/source/java/test/org/lucee/extension/osgi/BundleLocationTest.java b/source/java/test/org/lucee/extension/osgi/BundleLocationTest.java new file mode 100644 index 0000000..a5c5ca6 --- /dev/null +++ b/source/java/test/org/lucee/extension/osgi/BundleLocationTest.java @@ -0,0 +1,64 @@ +package org.lucee.extension.osgi; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.util.jar.Attributes; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** an additional bundle location (this.javasettings.bundles) can be a directory with bundle jars or a bundle jar */ +class BundleLocationTest { + + @TempDir + File dir; + + private File bundle(String fileName, String symbolicName, String version) throws IOException { + Manifest mf = new Manifest(); + Attributes a = mf.getMainAttributes(); + a.put(Attributes.Name.MANIFEST_VERSION, "1.0"); + a.putValue("Bundle-ManifestVersion", "2"); + a.putValue("Bundle-SymbolicName", symbolicName); + a.putValue("Bundle-Version", version); + File jar = new File(dir, fileName); + new JarOutputStream(new FileOutputStream(jar), mf).close(); + return jar; + } + + @Test + void findsABundleInADirectory() throws IOException { + File jar = bundle("a.jar", "test.a", "1.0.0"); + assertEquals(jar, OSGiServiceImpl.findJar(dir, "test.a", "1.0.0")); + } + + @Test + void findsABundleWhenTheLocationIsTheJarItself() throws IOException { + File jar = bundle("a.jar", "test.a", "1.0.0"); + assertEquals(jar, OSGiServiceImpl.findJar(jar, "test.a", "1.0.0")); + } + + @Test + void aJarLocationOnlyMatchesItsOwnBundle() throws IOException { + File jar = bundle("a.jar", "test.a", "1.0.0"); + assertNull(OSGiServiceImpl.findJar(jar, "test.b", "1.0.0")); + assertNull(OSGiServiceImpl.findJar(jar, "test.a", "2.0.0")); + } + + @Test + void locationsThatAreNoBundleSource() throws IOException { + File txt = new File(dir, "a.txt"); + Files.write(txt.toPath(), new byte[0]); + assertNull(OSGiServiceImpl.jars(txt)); + assertNull(OSGiServiceImpl.jars(null)); + assertNull(OSGiServiceImpl.jars(new File(dir, "missing"))); + assertArrayEquals(new File[0], OSGiServiceImpl.jars(dir)); + } +}