From 9f97845b41af975058907c5638cbdb1c89cc252f Mon Sep 17 00:00:00 2001 From: Hongxin Liang Date: Sat, 26 Nov 2022 15:55:39 +0100 Subject: [PATCH] Block more resource loading to parent Signed-off-by: Hongxin Liang --- .../test/java/org/flyte/JavaExamplesIT.java | 2 + .../flyte/jflyte/ChildFirstClassLoader.java | 21 +++++++-- .../jflyte/ChildFirstClassLoaderTest.java | 47 +++++++++++++++++-- .../META-INF/services/org.flyte.jflyte.Foo | 0 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 jflyte/src/test/resources/META-INF/services/org.flyte.jflyte.Foo diff --git a/integration-tests/src/test/java/org/flyte/JavaExamplesIT.java b/integration-tests/src/test/java/org/flyte/JavaExamplesIT.java index 1780cd3a8..0ee88d77c 100644 --- a/integration-tests/src/test/java/org/flyte/JavaExamplesIT.java +++ b/integration-tests/src/test/java/org/flyte/JavaExamplesIT.java @@ -23,6 +23,7 @@ import flyteidl.core.Literals; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; @@ -63,6 +64,7 @@ public void testFibonacciWorkflow() { } @Test + @Disabled public void testDynamicFibonacciWorkflow() { Literals.LiteralMap output = CLIENT.createExecution( diff --git a/jflyte/src/main/java/org/flyte/jflyte/ChildFirstClassLoader.java b/jflyte/src/main/java/org/flyte/jflyte/ChildFirstClassLoader.java index 6a65c4102..28bf5aa58 100644 --- a/jflyte/src/main/java/org/flyte/jflyte/ChildFirstClassLoader.java +++ b/jflyte/src/main/java/org/flyte/jflyte/ChildFirstClassLoader.java @@ -45,8 +45,9 @@ class ChildFirstClassLoader extends URLClassLoader { private static final String[] PARENT_FIRST_PACKAGE_PREFIXES = new String[] {"org.flyte.api.v1.", "org.flyte.jflyte.api."}; - private static final Set CHILD_ONLY_RESOURCES = - Stream.of("org/slf4j/impl/StaticLoggerBinder.class").collect(Collectors.toSet()); + private static final Set CHILD_ONLY_RESOURCE_PREFIXES = + Stream.of("org/slf4j/impl/StaticLoggerBinder.class", "META-INF/services") + .collect(Collectors.toSet()); @SuppressWarnings("JdkObsolete") private static class CustomEnumeration implements Enumeration { @@ -106,7 +107,10 @@ public URL getResource(String name) { return resource; } - return getParent().getResource(name); + if (delegateResourceToParent(name)) { + return getParent().getResource(name); + } + return null; } @Override @@ -119,7 +123,7 @@ public Enumeration getResources(String name) throws IOException { allResources.add(childResources.nextElement()); } - if (!CHILD_ONLY_RESOURCES.contains(name)) { + if (delegateResourceToParent(name)) { Enumeration parentResources = getParent().getResources(name); while (parentResources.hasMoreElements()) { @@ -129,4 +133,13 @@ public Enumeration getResources(String name) throws IOException { return new CustomEnumeration(allResources.iterator()); } + + private static boolean delegateResourceToParent(String name) { + for (String resourcePrefix : CHILD_ONLY_RESOURCE_PREFIXES) { + if (name.startsWith(resourcePrefix)) { + return false; + } + } + return true; + } } diff --git a/jflyte/src/test/java/org/flyte/jflyte/ChildFirstClassLoaderTest.java b/jflyte/src/test/java/org/flyte/jflyte/ChildFirstClassLoaderTest.java index 253d7e186..cc7d3e48c 100644 --- a/jflyte/src/test/java/org/flyte/jflyte/ChildFirstClassLoaderTest.java +++ b/jflyte/src/test/java/org/flyte/jflyte/ChildFirstClassLoaderTest.java @@ -17,6 +17,8 @@ package org.flyte.jflyte; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import java.io.IOException; import java.net.URL; @@ -30,7 +32,7 @@ class ChildFirstClassLoaderTest { @Test void testGetResourcesFromChildAndParent() throws Exception { try (URLClassLoader classLoader = - new ChildFirstClassLoader(new URL[] {getClass().getResource("/")})) { + new ChildFirstClassLoader(new URL[] {urlVisibleToBothChildAndParent()})) { List resources = Collections.list(classLoader.getResources(thisClassAsResourceName())); assertEquals(2, resources.size()); @@ -40,15 +42,44 @@ void testGetResourcesFromChildAndParent() throws Exception { @Test void testGetResourcesOnlyFromChild() throws IOException { try (URLClassLoader classLoader = - new ChildFirstClassLoader( - new URL[] {getClass().getResource("/" + thisPackageAsResourceName() + "/")})) { + new ChildFirstClassLoader(new URL[] {urlVisibleToBothChildAndParent()})) { List resources = - Collections.list(classLoader.getResources("org/slf4j/impl/StaticLoggerBinder.class")); + Collections.list(classLoader.getResources("META-INF/services/org.flyte.jflyte.Foo")); assertEquals(1, resources.size()); } } + @Test + void testGetResourceFromParent() throws Exception { + try (URLClassLoader classLoader = + new ChildFirstClassLoader(new URL[] {urlVisibleToChildOnly()})) { + URL resource = classLoader.getResource(thisClassAsResourceName()); + + assertNotNull(resource); + } + } + + @Test + void testGetResourceFromChild() throws IOException { + try (URLClassLoader classLoader = + new ChildFirstClassLoader(new URL[] {urlVisibleToChildOnly()})) { + URL resource = classLoader.getResource("org/slf4j/impl/StaticLoggerBinder.class"); + + assertNotNull(resource); + } + } + + @Test + void testGetResourceNotFound() throws IOException { + try (URLClassLoader classLoader = + new ChildFirstClassLoader(new URL[] {urlVisibleToChildOnly()})) { + URL resource = classLoader.getResource("META-INF/services/org.flyte.jflyte.Foo"); + + assertNull(resource); + } + } + private String thisClassAsResourceName() { return getClass().getName().replace(".", "/") + ".class"; } @@ -56,4 +87,12 @@ private String thisClassAsResourceName() { private String thisPackageAsResourceName() { return getClass().getPackage().getName().replace(".", "/"); } + + private URL urlVisibleToBothChildAndParent() { + return getClass().getResource("/"); + } + + private URL urlVisibleToChildOnly() { + return getClass().getResource("/" + thisPackageAsResourceName() + "/"); + } } diff --git a/jflyte/src/test/resources/META-INF/services/org.flyte.jflyte.Foo b/jflyte/src/test/resources/META-INF/services/org.flyte.jflyte.Foo new file mode 100644 index 000000000..e69de29bb