From 117df855e2591231a1efd1079d1e37b5e6f5a60a Mon Sep 17 00:00:00 2001 From: Igor Fedorenko Date: Thu, 5 Feb 2015 19:44:45 -0500 Subject: [PATCH] dispose dynamically created class realms this is mostly to help integration tests reuse the same realm ids, but plugging resource leaks is generally a good thing. Signed-off-by: Igor Fedorenko --- .../java/org/apache/maven/cli/MavenCli.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 238be22c92..3878dae0db 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -27,10 +27,13 @@ import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import java.util.StringTokenizer; import org.apache.commons.cli.CommandLine; @@ -81,6 +84,7 @@ import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.classworlds.realm.NoSuchRealmException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.logging.LoggerManager; import org.codehaus.plexus.util.StringUtils; @@ -190,6 +194,20 @@ public class MavenCli PrintStream oldout = System.out; PrintStream olderr = System.err; + final Set realms; + if ( classWorld != null ) + { + realms = new HashSet(); + for ( ClassRealm realm : classWorld.getRealms() ) + { + realms.add( realm.getId() ); + } + } + else + { + realms = Collections.emptySet(); + } + try { if ( stdout != null ) @@ -208,6 +226,24 @@ public class MavenCli } finally { + if ( classWorld != null ) + { + for ( ClassRealm realm : new ArrayList( classWorld.getRealms() ) ) + { + String realmId = realm.getId(); + if ( !realms.contains( realmId ) ) + { + try + { + classWorld.disposeRealm( realmId ); + } + catch ( NoSuchRealmException ignored ) + { + // can't happen + } + } + } + } System.setOut( oldout ); System.setErr( olderr ); }