Skip to content

Commit

Permalink
Always load UnionURLStreamHandler even in non-modular environments (#74)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shartte authored May 30, 2024
1 parent 13cf758 commit f970393
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 19 additions & 5 deletions src/main/java/cpw/mods/cl/ModularURLHandler.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
package cpw.mods.cl;

import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
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<String, IURLProvider> handlers;

public static void initFrom(ModuleLayer layer) {
public static void initFrom(@Nullable ModuleLayer layer) {
var handlers = new HashMap<String, IURLProvider>();

// 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;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cpw.mods.cl.ModularURLHandler;
import cpw.mods.cl.UnionURLStreamHandler;
import cpw.mods.niofs.union.UnionFileSystemProvider;

module cpw.mods.securejarhandler {
Expand All @@ -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;
}

0 comments on commit f970393

Please sign in to comment.