Skip to content

Commit 99fd054

Browse files
committed
Make ClassWorld implement Closeable
This closes #71
1 parent 1862fc6 commit 99fd054

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

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

+35-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,43 @@ public synchronized ClassRealm newRealm( String id, ClassLoader classLoader )
8688
return realm;
8789
}
8890

89-
public synchronized void disposeRealm( String id )
90-
throws NoSuchRealmException
91+
/**
92+
* Closes all contained class realms.
93+
*/
94+
@Override
95+
public synchronized void close()
96+
throws IOException
97+
{
98+
realms.values().stream().forEach( this::disposeRealm );
99+
realms.clear();
100+
}
101+
102+
public synchronized void disposeRealm( String id ) throws NoSuchRealmException
91103
{
92104
ClassRealm realm = realms.remove( id );
93105

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

0 commit comments

Comments
 (0)