25
25
import java .util .Iterator ;
26
26
import java .util .List ;
27
27
import java .util .Set ;
28
- import java .util .stream .Collectors ;
29
- import java .util .stream .Stream ;
30
28
31
29
/**
32
30
* {@link URLClassLoader} that loads classes into child class loader, instead of parent.
@@ -42,12 +40,11 @@ class ChildFirstClassLoader extends URLClassLoader {
42
40
43
41
// we have to load these classes in parent class loader
44
42
// it's base shared between all plugins and user code
45
- private static final String [] PARENT_FIRST_PACKAGE_PREFIXES =
46
- new String [] { "org.flyte.api.v1." , "org.flyte.jflyte.api." } ;
43
+ private static final Set < String > PARENT_FIRST_PACKAGE_PREFIXES =
44
+ Set . of ( "org.flyte.api.v1." , "org.flyte.jflyte.api." ) ;
47
45
48
- private static final Set <String > CHILD_ONLY_RESOURCE_PREFIXES =
49
- Stream .of ("org/slf4j/impl/StaticLoggerBinder.class" , "META-INF/services/" )
50
- .collect (Collectors .toSet ());
46
+ private static final Set <String > CHILD_ONLY_PREFIXES =
47
+ Set .of ("org.slf4j.impl." , "org/slf4j/impl/" , "META-INF/services/" );
51
48
52
49
@ SuppressWarnings ("JdkObsolete" )
53
50
private static class CustomEnumeration implements Enumeration <URL > {
@@ -79,15 +76,16 @@ protected synchronized Class<?> loadClass(String name, boolean resolve)
79
76
@ Var Class <?> cls = findLoadedClass (name );
80
77
81
78
if (cls == null ) {
82
- for (String prefix : PARENT_FIRST_PACKAGE_PREFIXES ) {
83
- if (name .startsWith (prefix )) {
84
- return super .loadClass (name , resolve );
85
- }
79
+ if (parentFirst (name )) {
80
+ return super .loadClass (name , resolve );
86
81
}
87
82
88
83
try {
89
84
cls = findClass (name );
90
85
} catch (ClassNotFoundException e ) {
86
+ if (childOnly (name )) {
87
+ throw e ;
88
+ }
91
89
cls = getParent ().loadClass (name );
92
90
}
93
91
}
@@ -107,10 +105,7 @@ public URL getResource(String name) {
107
105
return resource ;
108
106
}
109
107
110
- if (delegateResourceToParent (name )) {
111
- return getParent ().getResource (name );
112
- }
113
- return null ;
108
+ return childOnly (name ) ? null : getParent ().getResource (name );
114
109
}
115
110
116
111
@ Override
@@ -123,7 +118,7 @@ public Enumeration<URL> getResources(String name) throws IOException {
123
118
allResources .add (childResources .nextElement ());
124
119
}
125
120
126
- if (delegateResourceToParent (name )) {
121
+ if (! childOnly (name )) {
127
122
Enumeration <URL > parentResources = getParent ().getResources (name );
128
123
129
124
while (parentResources .hasMoreElements ()) {
@@ -134,12 +129,11 @@ public Enumeration<URL> getResources(String name) throws IOException {
134
129
return new CustomEnumeration (allResources .iterator ());
135
130
}
136
131
137
- private static boolean delegateResourceToParent (String name ) {
138
- for (String resourcePrefix : CHILD_ONLY_RESOURCE_PREFIXES ) {
139
- if (name .startsWith (resourcePrefix )) {
140
- return false ;
141
- }
142
- }
143
- return true ;
132
+ private static boolean parentFirst (String name ) {
133
+ return PARENT_FIRST_PACKAGE_PREFIXES .stream ().anyMatch (name ::startsWith );
134
+ }
135
+
136
+ private static boolean childOnly (String name ) {
137
+ return CHILD_ONLY_PREFIXES .stream ().anyMatch (name ::startsWith );
144
138
}
145
139
}
0 commit comments