Skip to content

Commit d9481b5

Browse files
committed
Use Predicate for filtering
1 parent 3ff12a4 commit d9481b5

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Set;
27+
import java.util.function.Predicate;
2728

2829
import org.codehaus.plexus.classworlds.realm.ClassRealm;
2930
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
@@ -69,22 +70,22 @@ public ClassRealm newRealm( String id )
6970
public ClassRealm newRealm( String id, ClassLoader classLoader )
7071
throws DuplicateRealmException
7172
{
72-
return newRealm( id, classLoader, Collections.emptySet() );
73+
return newRealm( id, classLoader, null );
7374
}
7475

7576
/**
7677
* Shortcut for {@link #newRealm(String, ClassLoader, Set)} with the class loader of the current class.
7778
* @param id The identifier for this realm, must not be <code>null</code>.
78-
* @param allowedResourceNamePrefixes the prefixes of resource names which should be exposed. Separator '/' is used here (even for classes).
79+
* @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
7980
* @return the created class realm
8081
* @throws DuplicateRealmException in case a realm with the given id does already exist
8182
* @since 2.7.0
8283
* @see FilteredClassRealm
8384
*/
84-
public synchronized ClassRealm newRealm( String id, Set<String> allowedResourceNamePrefixes )
85+
public synchronized ClassRealm newRealm( String id, Predicate<String> filter )
8586
throws DuplicateRealmException
8687
{
87-
return newRealm( id, getClass().getClassLoader(), allowedResourceNamePrefixes );
88+
return newRealm( id, getClass().getClassLoader(), filter );
8889
}
8990

9091
/**
@@ -93,13 +94,13 @@ public synchronized ClassRealm newRealm( String id, Set<String> allowedResourceN
9394
* @param id The identifier for this realm, must not be <code>null</code>.
9495
* @param classLoader The base class loader for this realm, may be <code>null</code> to use the bootstrap class
9596
* loader.
96-
* @param allowedResourceNamePrefixes the prefixes of resource names which should be exposed. Separator '/' is used here (even for classes).
97+
* @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
9798
* @return the created class realm
9899
* @throws DuplicateRealmException in case a realm with the given id does already exist
99100
* @since 2.7.0
100101
* @see FilteredClassRealm
101102
*/
102-
public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Set<String> allowedResourceNamePrefixes )
103+
public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Predicate<String> filter )
103104
throws DuplicateRealmException
104105
{
105106
if ( realms.containsKey( id ) )
@@ -109,13 +110,13 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Set
109110

110111
ClassRealm realm;
111112

112-
if ( allowedResourceNamePrefixes.isEmpty() )
113+
if ( filter == null )
113114
{
114115
realm = new ClassRealm( this, id, classLoader );
115116
}
116117
else
117118
{
118-
realm = new FilteredClassRealm( allowedResourceNamePrefixes, this, id, classLoader );
119+
realm = new FilteredClassRealm( filter, this, id, classLoader );
119120
}
120121
realms.put( id, realm );
121122

src/main/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealm.java

+8-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.net.URL;
2323
import java.util.Collections;
2424
import java.util.Enumeration;
25-
import java.util.Set;
25+
import java.util.function.Predicate;
2626

2727
import org.codehaus.plexus.classworlds.ClassWorld;
2828

@@ -32,31 +32,29 @@
3232
*/
3333
public class FilteredClassRealm extends ClassRealm
3434
{
35-
36-
// no regular expressions for performance reasons
37-
private final Set<String> allowedResourceNamePrefixes;
35+
private final Predicate<String> filter;
3836

3937
/**
4038
* Creates a new class realm.
4139
*
42-
* @param allowedResourceNamePrefixes all resources not starting with one of the given prefixes are never exposed through this class loader
40+
* @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
4341
* @param world The class world this realm belongs to, must not be <code>null</code>.
4442
* @param id The identifier for this realm, must not be <code>null</code>.
4543
* @param baseClassLoader The base class loader for this realm, may be <code>null</code> to use the bootstrap class
4644
* loader.
4745
*/
48-
public FilteredClassRealm( Set<String> allowedResourceNamePrefixes, ClassWorld world, String id, ClassLoader baseClassLoader )
46+
public FilteredClassRealm( Predicate<String> filter, ClassWorld world, String id, ClassLoader baseClassLoader )
4947
{
5048
super( world, id, baseClassLoader );
51-
this.allowedResourceNamePrefixes = allowedResourceNamePrefixes;
49+
this.filter = filter;
5250
}
5351

5452
@Override
5553
protected Class<?> findClassInternal( String name )
5654
throws ClassNotFoundException
5755
{
5856
String resourceName = name.replace( '.', '/' ).concat( ".class" );
59-
if ( !isAllowedName( resourceName ))
57+
if ( !filter.test( resourceName ) )
6058
{
6159
throw new ClassNotFoundException(name);
6260
}
@@ -66,7 +64,7 @@ protected Class<?> findClassInternal( String name )
6664
@Override
6765
public URL findResource( String name )
6866
{
69-
if ( !isAllowedName( name ))
67+
if ( !filter.test( name ) )
7068
{
7169
return null;
7270
}
@@ -77,15 +75,10 @@ public URL findResource( String name )
7775
public Enumeration<URL> findResources( String name )
7876
throws IOException
7977
{
80-
if ( !isAllowedName( name ))
78+
if ( !filter.test( name ) )
8179
{
8280
return Collections.emptyEnumeration();
8381
}
8482
return super.findResources( name );
8583
}
86-
87-
private boolean isAllowedName( String name )
88-
{
89-
return allowedResourceNamePrefixes.stream().anyMatch( name::startsWith );
90-
}
9184
}

src/test/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealmTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
package org.codehaus.plexus.classworlds.realm;
2020

2121
import java.io.IOException;
22-
import java.util.Collections;
2322
import java.util.HashSet;
2423
import java.util.Set;
24+
import java.util.function.Predicate;
2525

2626
import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase;
2727
import org.codehaus.plexus.classworlds.ClassWorld;
@@ -47,7 +47,7 @@ public void setUp() throws DuplicateRealmException
4747
Set<String> allowedResourcePrefixes = new HashSet<>();
4848
allowedResourcePrefixes.add( "a." );
4949
allowedResourcePrefixes.add( "a/Aa" );
50-
realmA = this.world.newRealm( "realmA", allowedResourcePrefixes );
50+
realmA = this.world.newRealm( "realmA", s -> allowedResourcePrefixes.stream().anyMatch( s::startsWith ) );
5151
}
5252

5353
@Test
@@ -78,7 +78,7 @@ public void testLoadClass() throws ClassNotFoundException
7878
@Test
7979
public void testLoadClassWithModule() throws IOException
8080
{
81-
try (ExtendedFilteredClassRealm realmA = new ExtendedFilteredClassRealm( world, Collections.singleton( "a/Aa" ) )) {
81+
try ( ExtendedFilteredClassRealm realmA = new ExtendedFilteredClassRealm( world, s -> s.startsWith( "a/Aa" ) ) ) {
8282
realmA.addURL( getJarUrl( "a.jar" ) );
8383
assertNotNull( realmA.simulateLoadClassFromModule( "a.Aa" ) );
8484
assertNull( realmA.simulateLoadClassFromModule( "a.A" ) );
@@ -94,9 +94,9 @@ public void testLoadClassWithModule() throws IOException
9494
static class ExtendedFilteredClassRealm extends FilteredClassRealm
9595
{
9696

97-
public ExtendedFilteredClassRealm( final ClassWorld world, Set<String> allowedResourcePrefixes )
97+
public ExtendedFilteredClassRealm( final ClassWorld world, Predicate<String> filter )
9898
{
99-
super( allowedResourcePrefixes, world, "java9", Thread.currentThread().getContextClassLoader() );
99+
super( filter, world, "java9", Thread.currentThread().getContextClassLoader() );
100100
}
101101

102102
public Class<?> simulateLoadClassFromModule(final String name)

0 commit comments

Comments
 (0)