null
.
+ * @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, Predicatenull
.
+ * @param classLoader The base class loader for this realm, may be null
to use the bootstrap class
+ * loader.
+ * @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, Predicatenull
.
+ * @param id The identifier for this realm, must not be null
.
+ * @param baseClassLoader The base class loader for this realm, may be null
to use the bootstrap class
+ * loader.
+ */
+ public FilteredClassRealm( Predicate
Once a ClassWorld
is created, realms within it
can be created. These realms effectively only allow loading
- of the core JVM classes.
+ of the core JVM classes initially.
- In order to make each ClassRealm
useful, constituent
- must be added to that each can provide certain classes.
+ In order to make each ClassRealm
useful, constituents
+ in form of URLs must be added to it where each can provide certain classes.
+ The URL must return either a JAR or a directory on the default file system.
+ ClassRealm
s can optionally be filtered to further restrict which classes/resources
+ are exposed. The filter is provided as additional argument to world.newRealm( "filteredcontainer", myPredicate );
+
Now, links between the various realms need to be created to allow
classes loaded from one to be available to classes loaded in another.
diff --git a/src/test/java/org/codehaus/plexus/classworlds/realm/ClassRealmImplTest.java b/src/test/java/org/codehaus/plexus/classworlds/realm/ClassRealmImplTest.java
index d689ab53..dfd14af2 100644
--- a/src/test/java/org/codehaus/plexus/classworlds/realm/ClassRealmImplTest.java
+++ b/src/test/java/org/codehaus/plexus/classworlds/realm/ClassRealmImplTest.java
@@ -443,7 +443,7 @@ public void testLoadClass_ClassWorldsClassRepeatedly()
}
@Test
- public void testLoadClass_Java11()
+ public void testLoadClassWithModuleName_Java9()
{
final ExtendedClassRealm mainRealm = new ExtendedClassRealm( world );
mainRealm.addURL( getJarUrl( "a.jar" ) );
@@ -503,13 +503,17 @@ public void testGetResources_SelfBeforeParent()
assertEquals( Arrays.asList( childUrl, parentUrl ), urls );
}
- // simulate new loadClass(Module,String) from java11
- // it is reversed in terms of inheritance but enables to simulate the same behavior in these tests
+ /**
+ * Simulates new {@code java.lang.ClassLoader#findClass(String,String)} introduced with Java 9.
+ * It is reversed in terms of inheritance but enables to simulate the same behavior in these tests.
+ * @see ClassLoader#findClass(String,String)
+ */
private static class ExtendedClassRealm extends ClassRealm
{
+
public ExtendedClassRealm(final ClassWorld world)
{
- super( world, "java11", Thread.currentThread().getContextClassLoader() );
+ super( world, "java9", Thread.currentThread().getContextClassLoader() );
}
public Class> simulateLoadClassFromModule(final String name)
diff --git a/src/test/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealmTest.java b/src/test/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealmTest.java
new file mode 100644
index 00000000..79b01610
--- /dev/null
+++ b/src/test/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealmTest.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.plexus.classworlds.realm;
+
+import java.io.IOException;
+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;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class FilteredClassRealmTest extends AbstractClassWorldsTestCase
+{
+ private ClassWorld world;
+ private ClassRealm realmA;
+
+ @Before
+ public void setUp() throws DuplicateRealmException
+ {
+ this.world = new ClassWorld();
+ // only allow loading resources whose names start with "a."
+ Set