Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions source/java/src/org/lucee/extension/osgi/OSGiServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -600,7 +609,7 @@ private boolean loadMissing(String message, File[] dirs, Set<String> 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)) {
Expand Down
64 changes: 64 additions & 0 deletions source/java/test/org/lucee/extension/osgi/BundleLocationTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}