Skip to content

Commit f1681d6

Browse files
laeubiHannesWell
authored andcommitted
Use Bundle#getResources to discover embedded SPI resources
Currently we use bundle.getEntry but this has some limitations, e.g. fragments and embedded jars. While the first can be avoided with findEntries, the second can not. As we previously already used bundle.getResources it seems fine to continue using that method.
1 parent 1b810d7 commit f1681d6

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/SPIBundleClassLoader.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,23 @@ protected Enumeration<URL> findResources(String name) throws IOException {
8989
List<SPIMapping> spis = mappings.computeIfAbsent(name, spi -> {
9090
List<SPIMapping> list = new ArrayList<>();
9191
for (Bundle other : bundles) {
92-
URL entry = other.getEntry(name);
93-
if (entry != null) {
94-
try {
95-
list.add(new SPIMapping(other.loadClass(serviceName), other, entry));
96-
} catch (ClassNotFoundException e) {
97-
// should not happen
92+
try {
93+
Enumeration<URL> resources = other.getResources(name);
94+
if (resources != null) {
95+
list.add(new SPIMapping(other.loadClass(serviceName), other, Collections.list(resources)));
9896
}
97+
} catch (ClassNotFoundException e) {
98+
// should not happen
99+
} catch (IOException e) {
100+
//can't be used then
99101
}
100102
}
101103
return list;
102104
});
103105
Bundle caller = Caller.getBundle(junitVersion);
104106
for (SPIMapping mapping : spis) {
105107
if (mapping.isCompatible(caller)) {
106-
result.add(mapping.getUrl());
108+
result.addAll(mapping.getUrls());
107109
}
108110
}
109111
return Collections.enumeration(result);

ui/org.eclipse.pde.junit.runtime/src/org/eclipse/pde/internal/junit/runtime/SPIMapping.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import java.io.IOException;
1818
import java.io.InputStreamReader;
1919
import java.net.URL;
20-
import java.util.HashSet;
20+
import java.util.Collection;
21+
import java.util.LinkedHashSet;
2122
import java.util.Set;
22-
import java.util.stream.Collectors;
2323

2424
import org.osgi.framework.Bundle;
2525

@@ -28,23 +28,27 @@ final class SPIMapping {
2828
private final Class<?> serviceClass;
2929
private final Bundle bundle;
3030
private final Set<String> classes;
31-
private final URL url;
31+
private final Collection<URL> urls;
3232

33-
SPIMapping(Class<?> serviceClass, Bundle bundle, URL url) {
33+
SPIMapping(Class<?> serviceClass, Bundle bundle, Collection<URL> urls) {
3434
this.serviceClass = serviceClass;
3535
this.bundle = bundle;
36-
this.url = url;
37-
this.classes = readClasses(url);
36+
this.urls = urls;
37+
this.classes = readClasses(urls);
3838
}
3939

40-
private Set<String> readClasses(URL entry) {
41-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(entry.openStream()))) {
42-
return reader.lines().collect(Collectors.toSet());
43-
} catch (IOException e) {
44-
return new HashSet<>();
40+
private static Set<String> readClasses(Collection<URL> urls) {
41+
Set<String> result = new LinkedHashSet<>();
42+
for (URL url : urls) {
43+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
44+
reader.lines().forEach(result::add);
45+
} catch (IOException e) {
46+
}
4547
}
48+
return result;
4649
}
4750

51+
4852
boolean isCompatible(Bundle other) {
4953
try {
5054
return other.loadClass(serviceClass.getName()) == serviceClass;
@@ -53,8 +57,8 @@ boolean isCompatible(Bundle other) {
5357
}
5458
}
5559

56-
URL getUrl() {
57-
return url;
60+
Collection<URL> getUrls() {
61+
return urls;
5862
}
5963

6064
boolean hasService(String implementation) {

0 commit comments

Comments
 (0)