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