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

Child only resource discovery #145

Merged
merged 1 commit into from
Nov 8, 2022
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
7 changes: 7 additions & 0 deletions jflyte/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@
<artifactId>memoryfilesystem</artifactId>
<scope>test</scope>
</dependency>
<!-- ensure there is at least one slf4j implementation in test classpath so that we can test
org/slf4j/impl/StaticLoggerBinder.class is only discovered in child class loader -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
14 changes: 11 additions & 3 deletions jflyte/src/main/java/org/flyte/jflyte/ChildFirstClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* {@link URLClassLoader} that loads classes into child class loader, instead of parent.
Expand All @@ -42,6 +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 =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project is still targeting Java 8, so no factory method from Set to use.

Stream.of("org/slf4j/impl/StaticLoggerBinder.class").collect(Collectors.toSet());

@SuppressWarnings("JdkObsolete")
private static class CustomEnumeration implements Enumeration<URL> {

Expand Down Expand Up @@ -113,10 +119,12 @@ public Enumeration<URL> getResources(String name) throws IOException {
allResources.add(childResources.nextElement());
}

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

while (parentResources.hasMoreElements()) {
allResources.add(parentResources.nextElement());
while (parentResources.hasMoreElements()) {
allResources.add(parentResources.nextElement());
}
}

return new CustomEnumeration(allResources.iterator());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021 Flyte Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.flyte.jflyte;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;

class ChildFirstClassLoaderTest {

@Test
void testGetResourcesFromChildAndParent() throws Exception {
try (URLClassLoader classLoader =
new ChildFirstClassLoader(new URL[] {getClass().getResource("/")})) {
List<URL> resources = Collections.list(classLoader.getResources(thisClassAsResourceName()));

assertEquals(2, resources.size());
}
}

@Test
void testGetResourcesOnlyFromChild() throws IOException {
try (URLClassLoader classLoader =
new ChildFirstClassLoader(
new URL[] {getClass().getResource("/" + thisPackageAsResourceName() + "/")})) {
List<URL> resources =
Collections.list(classLoader.getResources("org/slf4j/impl/StaticLoggerBinder.class"));

assertEquals(1, resources.size());
}
}

private String thisClassAsResourceName() {
return getClass().getName().replace(".", "/") + ".class";
}

private String thisPackageAsResourceName() {
return getClass().getPackage().getName().replace(".", "/");
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
Expand Down