Skip to content

Commit 0c38eb7

Browse files
committed
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 85b502b commit 0c38eb7

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,28 @@ 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> entry = other.getResources(name);
94+
if (entry == null) {
95+
continue;
9896
}
97+
List<URL> match = new ArrayList<>();
98+
while (entry.hasMoreElements()) {
99+
match.add(entry.nextElement());
100+
}
101+
list.add(new SPIMapping(other.loadClass(serviceName), other, match));
102+
} catch (ClassNotFoundException e) {
103+
// should not happen
104+
} catch (IOException e) {
105+
//can't be used then
99106
}
100107
}
101108
return list;
102109
});
103110
Bundle caller = Caller.getBundle(junitVersion);
104111
for (SPIMapping mapping : spis) {
105112
if (mapping.isCompatible(caller)) {
106-
result.add(mapping.getUrl());
113+
result.addAll(mapping.getUrls());
107114
}
108115
}
109116
return Collections.enumeration(result);

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

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

2425
import org.osgi.framework.Bundle;
2526

@@ -28,23 +29,27 @@ final class SPIMapping {
2829
private final Class<?> serviceClass;
2930
private final Bundle bundle;
3031
private final Set<String> classes;
31-
private final URL url;
32+
private final Collection<URL> urls;
3233

33-
SPIMapping(Class<?> serviceClass, Bundle bundle, URL url) {
34+
SPIMapping(Class<?> serviceClass, Bundle bundle, Collection<URL> urls) {
3435
this.serviceClass = serviceClass;
3536
this.bundle = bundle;
36-
this.url = url;
37-
this.classes = readClasses(url);
37+
this.urls = urls;
38+
this.classes = readClasses(urls);
3839
}
3940

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<>();
41+
private static Set<String> readClasses(Collection<URL> urls) {
42+
HashSet<String> result = new LinkedHashSet<>();
43+
for (URL url : urls) {
44+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
45+
reader.lines().forEach(result::add);
46+
} catch (IOException e) {
47+
}
4548
}
49+
return result;
4650
}
4751

52+
4853
boolean isCompatible(Bundle other) {
4954
try {
5055
return other.loadClass(serviceClass.getName()) == serviceClass;
@@ -53,8 +58,8 @@ boolean isCompatible(Bundle other) {
5358
}
5459
}
5560

56-
URL getUrl() {
57-
return url;
61+
Collection<URL> getUrls() {
62+
return urls;
5863
}
5964

6065
boolean hasService(String implementation) {

0 commit comments

Comments
 (0)