Skip to content

Commit f970393

Browse files
authored
Always load UnionURLStreamHandler even in non-modular environments (#74)
The use case for this is the use of SJH inside of FML unit tests where SJH itself was not loaded as a module. It is perfectly capable of being used, but fails due to the `union` protocol handler not being loaded. This change makes SJH always load its own protocol handler regardless of layer and supports the extension point in non modular environments too. As an aside: The extension point does not seem to be used by anyone else according to a GH search and can likely be deprecated/removed at some point.
1 parent 13cf758 commit f970393

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/main/java/cpw/mods/cl/ModularURLHandler.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
package cpw.mods.cl;
22

3+
import org.jetbrains.annotations.Nullable;
4+
35
import java.io.IOException;
46
import java.io.InputStream;
57
import java.io.UncheckedIOException;
68
import java.net.URL;
79
import java.net.URLConnection;
810
import java.net.URLStreamHandler;
911
import java.net.URLStreamHandlerFactory;
12+
import java.util.HashMap;
1013
import java.util.Map;
1114
import java.util.ServiceLoader;
1215
import java.util.function.Function;
13-
import java.util.stream.Collectors;
1416

1517
public class ModularURLHandler implements URLStreamHandlerFactory {
1618
public static final ModularURLHandler INSTANCE = new ModularURLHandler();
1719
private Map<String, IURLProvider> handlers;
1820

19-
public static void initFrom(ModuleLayer layer) {
21+
public static void initFrom(@Nullable ModuleLayer layer) {
22+
var handlers = new HashMap<String, IURLProvider>();
23+
24+
// This handler is required for SJH to work.
25+
var unionHandler = new UnionURLStreamHandler();
26+
handlers.put(unionHandler.protocol(), unionHandler);
27+
2028
if (layer == null) {
21-
INSTANCE.handlers = null;
29+
// Support non-modular environment for testing purposes
30+
ServiceLoader.load(IURLProvider.class).stream()
31+
.map(ServiceLoader.Provider::get)
32+
.forEach(handler -> handlers.putIfAbsent(handler.protocol(), handler));
2233
} else {
23-
INSTANCE.handlers = ServiceLoader.load(layer, IURLProvider.class).stream()
34+
ServiceLoader.load(layer, IURLProvider.class).stream()
2435
.map(ServiceLoader.Provider::get)
25-
.collect(Collectors.toMap(IURLProvider::protocol, Function.identity()));
36+
.forEach(handler -> handlers.putIfAbsent(handler.protocol(), handler));
2637
}
38+
39+
INSTANCE.handlers = Map.copyOf(handlers);
2740
}
41+
2842
@Override
2943
public URLStreamHandler createURLStreamHandler(final String protocol) {
3044
if (handlers == null) return null;

src/main/java/module-info.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cpw.mods.cl.ModularURLHandler;
2-
import cpw.mods.cl.UnionURLStreamHandler;
32
import cpw.mods.niofs.union.UnionFileSystemProvider;
43

54
module cpw.mods.securejarhandler {
@@ -14,5 +13,4 @@
1413
requires static org.jetbrains.annotations;
1514
provides java.nio.file.spi.FileSystemProvider with UnionFileSystemProvider;
1615
uses cpw.mods.cl.ModularURLHandler.IURLProvider;
17-
provides ModularURLHandler.IURLProvider with UnionURLStreamHandler;
1816
}

0 commit comments

Comments
 (0)