Skip to content

Commit

Permalink
Use Predicate for filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Nov 5, 2022
1 parent 3ff12a4 commit d9481b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
17 changes: 9 additions & 8 deletions src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
Expand Down Expand Up @@ -69,22 +70,22 @@ public ClassRealm newRealm( String id )
public ClassRealm newRealm( String id, ClassLoader classLoader )
throws DuplicateRealmException
{
return newRealm( id, classLoader, Collections.emptySet() );
return newRealm( id, classLoader, null );
}

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

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

ClassRealm realm;

if ( allowedResourceNamePrefixes.isEmpty() )
if ( filter == null )
{
realm = new ClassRealm( this, id, classLoader );
}
else
{
realm = new FilteredClassRealm( allowedResourceNamePrefixes, this, id, classLoader );
realm = new FilteredClassRealm( filter, this, id, classLoader );
}
realms.put( id, realm );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Set;
import java.util.function.Predicate;

import org.codehaus.plexus.classworlds.ClassWorld;

Expand All @@ -32,31 +32,29 @@
*/
public class FilteredClassRealm extends ClassRealm
{

// no regular expressions for performance reasons
private final Set<String> allowedResourceNamePrefixes;
private final Predicate<String> filter;

/**
* Creates a new class realm.
*
* @param allowedResourceNamePrefixes all resources not starting with one of the given prefixes are never exposed through this class loader
* @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
* @param world The class world this realm belongs to, must not be <code>null</code>.
* @param id The identifier for this realm, must not be <code>null</code>.
* @param baseClassLoader The base class loader for this realm, may be <code>null</code> to use the bootstrap class
* loader.
*/
public FilteredClassRealm( Set<String> allowedResourceNamePrefixes, ClassWorld world, String id, ClassLoader baseClassLoader )
public FilteredClassRealm( Predicate<String> filter, ClassWorld world, String id, ClassLoader baseClassLoader )
{
super( world, id, baseClassLoader );
this.allowedResourceNamePrefixes = allowedResourceNamePrefixes;
this.filter = filter;
}

@Override
protected Class<?> findClassInternal( String name )
throws ClassNotFoundException
{
String resourceName = name.replace( '.', '/' ).concat( ".class" );
if ( !isAllowedName( resourceName ))
if ( !filter.test( resourceName ) )
{
throw new ClassNotFoundException(name);
}
Expand All @@ -66,7 +64,7 @@ protected Class<?> findClassInternal( String name )
@Override
public URL findResource( String name )
{
if ( !isAllowedName( name ))
if ( !filter.test( name ) )
{
return null;
}
Expand All @@ -77,15 +75,10 @@ public URL findResource( String name )
public Enumeration<URL> findResources( String name )
throws IOException
{
if ( !isAllowedName( name ))
if ( !filter.test( name ) )
{
return Collections.emptyEnumeration();
}
return super.findResources( name );
}

private boolean isAllowedName( String name )
{
return allowedResourceNamePrefixes.stream().anyMatch( name::startsWith );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.codehaus.plexus.classworlds.realm;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

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

@Test
Expand Down Expand Up @@ -78,7 +78,7 @@ public void testLoadClass() throws ClassNotFoundException
@Test
public void testLoadClassWithModule() throws IOException
{
try (ExtendedFilteredClassRealm realmA = new ExtendedFilteredClassRealm( world, Collections.singleton( "a/Aa" ) )) {
try ( ExtendedFilteredClassRealm realmA = new ExtendedFilteredClassRealm( world, s -> s.startsWith( "a/Aa" ) ) ) {
realmA.addURL( getJarUrl( "a.jar" ) );
assertNotNull( realmA.simulateLoadClassFromModule( "a.Aa" ) );
assertNull( realmA.simulateLoadClassFromModule( "a.A" ) );
Expand All @@ -94,9 +94,9 @@ public void testLoadClassWithModule() throws IOException
static class ExtendedFilteredClassRealm extends FilteredClassRealm
{

public ExtendedFilteredClassRealm( final ClassWorld world, Set<String> allowedResourcePrefixes )
public ExtendedFilteredClassRealm( final ClassWorld world, Predicate<String> filter )
{
super( allowedResourcePrefixes, world, "java9", Thread.currentThread().getContextClassLoader() );
super( filter, world, "java9", Thread.currentThread().getContextClassLoader() );
}

public Class<?> simulateLoadClassFromModule(final String name)
Expand Down

0 comments on commit d9481b5

Please sign in to comment.