Skip to content

Commit

Permalink
Block more resource loading to parent
Browse files Browse the repository at this point in the history
Signed-off-by: Hongxin Liang <[email protected]>
  • Loading branch information
honnix committed Nov 26, 2022
1 parent 09a1577 commit 9f97845
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
2 changes: 2 additions & 0 deletions integration-tests/src/test/java/org/flyte/JavaExamplesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,7 @@ public void testFibonacciWorkflow() {
}

@Test
@Disabled
public void testDynamicFibonacciWorkflow() {
Literals.LiteralMap output =
CLIENT.createExecution(
Expand Down
21 changes: 17 additions & 4 deletions jflyte/src/main/java/org/flyte/jflyte/ChildFirstClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> CHILD_ONLY_RESOURCES =
Stream.of("org/slf4j/impl/StaticLoggerBinder.class").collect(Collectors.toSet());
private static final Set<String> 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<URL> {
Expand Down Expand Up @@ -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
Expand All @@ -119,7 +123,7 @@ public Enumeration<URL> getResources(String name) throws IOException {
allResources.add(childResources.nextElement());
}

if (!CHILD_ONLY_RESOURCES.contains(name)) {
if (delegateResourceToParent(name)) {
Enumeration<URL> parentResources = getParent().getResources(name);

while (parentResources.hasMoreElements()) {
Expand All @@ -129,4 +133,13 @@ public Enumeration<URL> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<URL> resources = Collections.list(classLoader.getResources(thisClassAsResourceName()));

assertEquals(2, resources.size());
Expand All @@ -40,20 +42,57 @@ 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<URL> 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";
}

private String thisPackageAsResourceName() {
return getClass().getPackage().getName().replace(".", "/");
}

private URL urlVisibleToBothChildAndParent() {
return getClass().getResource("/");
}

private URL urlVisibleToChildOnly() {
return getClass().getResource("/" + thisPackageAsResourceName() + "/");
}
}
Empty file.

0 comments on commit 9f97845

Please sign in to comment.