Skip to content

Commit eff80f7

Browse files
committed
add documentation
1 parent 3ff12a4 commit eff80f7

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
<report>summary</report>
208208
<report>index</report>
209209
<report>dependencies</report>
210-
<report>issue-tracking</report>
210+
<report>issue-management</report>
211211
<report>scm</report>
212212
</reports>
213213
</reportSet>

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.LinkedHashMap;
2424
import java.util.List;
2525
import java.util.Map;
26-
import java.util.Set;
26+
import java.util.function.Predicate;
2727

2828
import org.codehaus.plexus.classworlds.realm.ClassRealm;
2929
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
@@ -69,37 +69,37 @@ public ClassRealm newRealm( String id )
6969
public ClassRealm newRealm( String id, ClassLoader classLoader )
7070
throws DuplicateRealmException
7171
{
72-
return newRealm( id, classLoader, Collections.emptySet() );
72+
return newRealm( id, classLoader, null );
7373
}
7474

7575
/**
76-
* Shortcut for {@link #newRealm(String, ClassLoader, Set)} with the class loader of the current class.
76+
* Shortcut for {@link #newRealm(String, ClassLoader, Predicate)} with the class loader of the current class.
7777
* @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).
78+
* @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
7979
* @return the created class realm
8080
* @throws DuplicateRealmException in case a realm with the given id does already exist
8181
* @since 2.7.0
8282
* @see FilteredClassRealm
8383
*/
84-
public synchronized ClassRealm newRealm( String id, Set<String> allowedResourceNamePrefixes )
84+
public synchronized ClassRealm newRealm( String id, Predicate<String> filter )
8585
throws DuplicateRealmException
8686
{
87-
return newRealm( id, getClass().getClassLoader(), allowedResourceNamePrefixes );
87+
return newRealm( id, getClass().getClassLoader(), filter );
8888
}
8989

9090
/**
9191
* Adds a class realm with filtering.
92-
* Only resources/classes starting with one of the given prefixes are exposed.
92+
* Only resources/classes whose name matches a given predicate are exposed.
9393
* @param id The identifier for this realm, must not be <code>null</code>.
9494
* @param classLoader The base class loader for this realm, may be <code>null</code> to use the bootstrap class
9595
* loader.
96-
* @param allowedResourceNamePrefixes the prefixes of resource names which should be exposed. Separator '/' is used here (even for classes).
96+
* @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
9797
* @return the created class realm
9898
* @throws DuplicateRealmException in case a realm with the given id does already exist
9999
* @since 2.7.0
100100
* @see FilteredClassRealm
101101
*/
102-
public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Set<String> allowedResourceNamePrefixes )
102+
public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Predicate<String> filter )
103103
throws DuplicateRealmException
104104
{
105105
if ( realms.containsKey( id ) )
@@ -109,13 +109,13 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Set
109109

110110
ClassRealm realm;
111111

112-
if ( allowedResourceNamePrefixes.isEmpty() )
112+
if ( filter == null )
113113
{
114114
realm = new ClassRealm( this, id, classLoader );
115115
}
116116
else
117117
{
118-
realm = new FilteredClassRealm( allowedResourceNamePrefixes, this, id, classLoader );
118+
realm = new FilteredClassRealm( filter, this, id, classLoader );
119119
}
120120
realms.put( id, realm );
121121

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/site/xdoc/apiusage.xml

+9-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ClassWorld world = new ClassWorld();
3232
<p>
3333
Once a <code>ClassWorld</code> is created, realms within it
3434
can be created. These realms effectively only allow loading
35-
of the core JVM classes.
35+
of the core JVM classes initially.
3636
</p>
3737

3838
<source><![CDATA[
@@ -42,15 +42,21 @@ ClassRealm logComponentRealm = world.newRealm( "logComponent" );
4242
]]></source>
4343

4444
<p>
45-
In order to make each <code>ClassRealm</code> useful, constituent
46-
must be added to that each can provide certain classes.
45+
In order to make each <code>ClassRealm</code> useful, constituents
46+
in form of URLs must be added to it where each can provide certain classes.
47+
The URL must return either a JAR or a directory on the default file system.
4748
</p>
4849

4950
<source><![CDATA[
5051
containerRealm.addURL( containerJarUrl );
5152
logComponentRealm.addURL( logComponentJarUrl );
5253
]]></source>
5354

55+
<p>
56+
<code>ClassRealm</code>s can optionally be filtered to further restrict which classes/resources
57+
are exposed. The filter is provided as additional argument to <code>world.newRealm( "filteredcontainer", myPredicate );</code>
58+
</p>
59+
5460
<p>
5561
Now, links between the various realms need to be created to allow
5662
classes loaded from one to be available to classes loaded in another.

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)