diff --git a/src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java b/src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java index eab7a51..ac2b854 100644 --- a/src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java +++ b/src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java @@ -16,6 +16,7 @@ * limitations under the License. */ +import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -35,7 +36,7 @@ * * @author bob mcwhirter */ -public class ClassWorld +public class ClassWorld implements Closeable { private Map realms; @@ -127,6 +128,18 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader, Pre return realm; } + /** + * Closes all contained class realms. + * @since 2.7.0 + */ + @Override + public synchronized void close() + throws IOException + { + realms.values().stream().forEach( this::disposeRealm ); + realms.clear(); + } + public synchronized void disposeRealm( String id ) throws NoSuchRealmException { @@ -134,17 +147,26 @@ public synchronized void disposeRealm( String id ) if ( realm != null ) { - try - { - realm.close(); - } - catch ( IOException ignore ) - { - } - for ( ClassWorldListener listener : listeners ) - { - listener.realmDisposed( realm ); - } + disposeRealm( realm ); + } + else + { + throw new NoSuchRealmException( this, id ); + } + } + + private void disposeRealm( ClassRealm realm ) + { + try + { + realm.close(); + } + catch ( IOException ignore ) + { + } + for ( ClassWorldListener listener : listeners ) + { + listener.realmDisposed( realm ); } }