25
25
import java .util .LinkedHashMap ;
26
26
import java .util .List ;
27
27
import java .util .Map ;
28
+ import java .util .function .Predicate ;
28
29
29
30
import org .codehaus .plexus .classworlds .realm .ClassRealm ;
30
31
import org .codehaus .plexus .classworlds .realm .DuplicateRealmException ;
32
+ import org .codehaus .plexus .classworlds .realm .FilteredClassRealm ;
31
33
import org .codehaus .plexus .classworlds .realm .NoSuchRealmException ;
32
34
33
35
/**
@@ -66,7 +68,40 @@ public ClassRealm newRealm( String id )
66
68
return newRealm ( id , getClass ().getClassLoader () );
67
69
}
68
70
69
- public synchronized ClassRealm newRealm ( String id , ClassLoader classLoader )
71
+ public ClassRealm newRealm ( String id , ClassLoader classLoader )
72
+ throws DuplicateRealmException
73
+ {
74
+ return newRealm ( id , classLoader , null );
75
+ }
76
+
77
+ /**
78
+ * Shortcut for {@link #newRealm(String, ClassLoader, Predicate)} with the class loader of the current class.
79
+ * @param id The identifier for this realm, must not be <code>null</code>.
80
+ * @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
81
+ * @return the created class realm
82
+ * @throws DuplicateRealmException in case a realm with the given id does already exist
83
+ * @since 2.7.0
84
+ * @see FilteredClassRealm
85
+ */
86
+ public synchronized ClassRealm newRealm ( String id , Predicate <String > filter )
87
+ throws DuplicateRealmException
88
+ {
89
+ return newRealm ( id , getClass ().getClassLoader (), filter );
90
+ }
91
+
92
+ /**
93
+ * Adds a class realm with filtering.
94
+ * Only resources/classes whose name matches a given predicate are exposed.
95
+ * @param id The identifier for this realm, must not be <code>null</code>.
96
+ * @param classLoader The base class loader for this realm, may be <code>null</code> to use the bootstrap class
97
+ * loader.
98
+ * @param filter a predicate to apply to each resource name to determine if it should be loaded through this class loader
99
+ * @return the created class realm
100
+ * @throws DuplicateRealmException in case a realm with the given id does already exist
101
+ * @since 2.7.0
102
+ * @see FilteredClassRealm
103
+ */
104
+ public synchronized ClassRealm newRealm ( String id , ClassLoader classLoader , Predicate <String > filter )
70
105
throws DuplicateRealmException
71
106
{
72
107
if ( realms .containsKey ( id ) )
@@ -76,8 +111,14 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader )
76
111
77
112
ClassRealm realm ;
78
113
79
- realm = new ClassRealm ( this , id , classLoader );
80
-
114
+ if ( filter == null )
115
+ {
116
+ realm = new ClassRealm ( this , id , classLoader );
117
+ }
118
+ else
119
+ {
120
+ realm = new FilteredClassRealm ( filter , this , id , classLoader );
121
+ }
81
122
realms .put ( id , realm );
82
123
83
124
for ( ClassWorldListener listener : listeners )
0 commit comments