Skip to content

Commit bb3530a

Browse files
committed
Implement Closeable for ClassWorld
This closes #71
1 parent 1862fc6 commit bb3530a

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

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

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.codehaus.plexus.classworlds;
22

3+
import java.io.Closeable;
4+
35
/*
46
* Copyright 2001-2006 Codehaus Foundation.
57
*
@@ -33,7 +35,7 @@
3335
*
3436
* @author <a href="mailto:[email protected]">bob mcwhirter</a>
3537
*/
36-
public class ClassWorld
38+
public class ClassWorld implements Closeable
3739
{
3840
private Map<String, ClassRealm> realms;
3941

@@ -86,24 +88,40 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader )
8688
return realm;
8789
}
8890

89-
public synchronized void disposeRealm( String id )
90-
throws NoSuchRealmException
91+
@Override
92+
public void close()
93+
throws IOException
94+
{
95+
realms.values().stream().forEach( this::disposeRealm );
96+
realms.clear();
97+
}
98+
99+
public synchronized void disposeRealm( String id ) throws NoSuchRealmException
91100
{
92101
ClassRealm realm = realms.remove( id );
93102

94103
if ( realm != null )
95104
{
96-
try
97-
{
98-
realm.close();
99-
}
100-
catch ( IOException ignore )
101-
{
102-
}
103-
for ( ClassWorldListener listener : listeners )
104-
{
105-
listener.realmDisposed( realm );
106-
}
105+
disposeRealm( realm );
106+
}
107+
else
108+
{
109+
throw new NoSuchRealmException( this, id );
110+
}
111+
}
112+
113+
private void disposeRealm( ClassRealm realm )
114+
{
115+
try
116+
{
117+
realm.close();
118+
}
119+
catch ( IOException ignore )
120+
{
121+
}
122+
for ( ClassWorldListener listener : listeners )
123+
{
124+
listener.realmDisposed( realm );
107125
}
108126
}
109127

0 commit comments

Comments
 (0)