Skip to content

Commit

Permalink
[JENKINS-40621] - Close classRealm after reading the files
Browse files Browse the repository at this point in the history
After it it stops leaking the files… on my machine
  • Loading branch information
oleg-nenashev committed Jul 3, 2017
1 parent 626ddba commit 069e84e
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/java/hudson/maven/MavenEmbedderUtils.java
Expand Up @@ -20,6 +20,7 @@
* under the License.
*/

import java.io.Closeable;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
Expand Down Expand Up @@ -193,13 +194,16 @@ public static MavenInformation getMavenVersion(@Nonnull File mavenHome) throws M
/*package*/ static MavenInformation getMavenVersion(@Nonnull File mavenHome,
@CheckForNull MavenEmbedderCallable preopertiesPreloadHook) throws MavenEmbedderException {

ClassRealm realm = buildClassRealm( mavenHome, null, null );
if (debug) {
debugMavenVersion(realm);
}
ClassLoader original = Thread.currentThread().getContextClassLoader();
MavenInformation information = null;
ClassLoader original = null;
ClassRealm realm = null;
try {
realm = buildClassRealm( mavenHome, null, null );
if (debug) {
debugMavenVersion(realm);
}
original = Thread.currentThread().getContextClassLoader();

Thread.currentThread().setContextClassLoader( realm );
// TODO is this really intending to use findResource rather than getResource? Cf. https://github.com/sonatype/plexus-classworlds/pull/8
URL resource = realm.findResource( POM_PROPERTIES_PATH );
Expand All @@ -224,11 +228,23 @@ public static MavenInformation getMavenVersion(@Nonnull File mavenHome) throws M
throw new MavenEmbedderException( e.getMessage(), e );
} finally {
Thread.currentThread().setContextClassLoader( original );
closeIt(realm);
}

return information;
}

private static final void closeIt(@CheckForNull Closeable obj) throws MavenEmbedderException {
if (obj == null) {
return;
}
try {
obj.close();
} catch(IOException ex) {
throw new MavenEmbedderException("Failed to close " + obj, ex);
}
}

public static boolean isAtLeastMavenVersion(File mavenHome, String version) throws MavenEmbedderException {
ComparableVersion found = new ComparableVersion( getMavenVersion( mavenHome ).getVersion() );
ComparableVersion testedOne = new ComparableVersion( version );
Expand Down

0 comments on commit 069e84e

Please sign in to comment.