From 342b6c4df26334239ed2f791b7339b1afc064cc3 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sat, 18 May 2024 23:23:08 +0200 Subject: [PATCH 1/2] Always load UnionURLStreamHandler even in non-modular environments --- .../java/cpw/mods/cl/ModularURLHandler.java | 20 +++++++++++++++---- src/main/java/module-info.java | 2 -- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/cpw/mods/cl/ModularURLHandler.java b/src/main/java/cpw/mods/cl/ModularURLHandler.java index e365a11..67ce69b 100644 --- a/src/main/java/cpw/mods/cl/ModularURLHandler.java +++ b/src/main/java/cpw/mods/cl/ModularURLHandler.java @@ -7,24 +7,36 @@ import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; +import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; import java.util.function.Function; -import java.util.stream.Collectors; public class ModularURLHandler implements URLStreamHandlerFactory { public static final ModularURLHandler INSTANCE = new ModularURLHandler(); private Map handlers; public static void initFrom(ModuleLayer layer) { + var handlers = new HashMap(); + + // This handler is required for SJH to work. + var unionHandler = new UnionURLStreamHandler(); + handlers.put(unionHandler.protocol(), unionHandler); + if (layer == null) { - INSTANCE.handlers = null; + // Support non-modular environment for testing purposes + ServiceLoader.load(IURLProvider.class).stream() + .map(ServiceLoader.Provider::get) + .forEach(handler -> handlers.putIfAbsent(handler.protocol(), handler)); } else { - INSTANCE.handlers = ServiceLoader.load(layer, IURLProvider.class).stream() + ServiceLoader.load(layer, IURLProvider.class).stream() .map(ServiceLoader.Provider::get) - .collect(Collectors.toMap(IURLProvider::protocol, Function.identity())); + .forEach(handler -> handlers.putIfAbsent(handler.protocol(), handler)); } + + INSTANCE.handlers = Map.copyOf(handlers); } + @Override public URLStreamHandler createURLStreamHandler(final String protocol) { if (handlers == null) return null; diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 6ad8598..0c1bb31 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,5 +1,4 @@ import cpw.mods.cl.ModularURLHandler; -import cpw.mods.cl.UnionURLStreamHandler; import cpw.mods.niofs.union.UnionFileSystemProvider; module cpw.mods.securejarhandler { @@ -14,5 +13,4 @@ requires static org.jetbrains.annotations; provides java.nio.file.spi.FileSystemProvider with UnionFileSystemProvider; uses cpw.mods.cl.ModularURLHandler.IURLProvider; - provides ModularURLHandler.IURLProvider with UnionURLStreamHandler; } From bfb85701e859f0dd3d69954aacb01d087e7d41ea Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sat, 18 May 2024 23:54:06 +0200 Subject: [PATCH 2/2] Always load UnionURLStreamHandler even in non-modular environments --- src/main/java/cpw/mods/cl/ModularURLHandler.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/cpw/mods/cl/ModularURLHandler.java b/src/main/java/cpw/mods/cl/ModularURLHandler.java index 67ce69b..bdc5293 100644 --- a/src/main/java/cpw/mods/cl/ModularURLHandler.java +++ b/src/main/java/cpw/mods/cl/ModularURLHandler.java @@ -1,5 +1,7 @@ package cpw.mods.cl; +import org.jetbrains.annotations.Nullable; + import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; @@ -16,7 +18,7 @@ public class ModularURLHandler implements URLStreamHandlerFactory { public static final ModularURLHandler INSTANCE = new ModularURLHandler(); private Map handlers; - public static void initFrom(ModuleLayer layer) { + public static void initFrom(@Nullable ModuleLayer layer) { var handlers = new HashMap(); // This handler is required for SJH to work.