Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always load UnionURLStreamHandler even in non-modular environments #74

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading