Skip to content

Commit

Permalink
[highsource#188] Classloaders not being closed for mojo classloader
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentschoelens committed Aug 29, 2023
1 parent b148894 commit efd58cc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -305,26 +306,26 @@ public List<URI> getProducesURIs() {
*/
public void execute() throws MojoExecutionException {
synchronized (lock) {
injectDependencyDefaults();
resolveArtifacts();

// Install project dependencies into classloader's class path
// and execute xjc2.
final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
final ClassLoader classLoader = createClassLoader(currentClassLoader);
Thread.currentThread().setContextClassLoader(classLoader);
final Locale currentDefaultLocale = Locale.getDefault();
try {
final Locale locale = LocaleUtils.valueOf(getLocale());
Locale.setDefault(locale);
//
doExecute();

} finally {
Locale.setDefault(currentDefaultLocale);
// Set back the old classloader
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
injectDependencyDefaults();
resolveArtifacts();

// Install project dependencies into classloader's class path and execute xjc2.
final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
final Locale currentDefaultLocale = Locale.getDefault();
URLClassLoader classLoader = null;
try {
classLoader = createClassLoader(currentClassLoader);
Thread.currentThread().setContextClassLoader(classLoader);
final Locale locale = LocaleUtils.valueOf(getLocale());
Locale.setDefault(locale);
//
doExecute();
} finally {
Locale.setDefault(currentDefaultLocale);
// Set back the old classloader
Thread.currentThread().setContextClassLoader(currentClassLoader);
IOUtils.close(classLoader);
}
}
}

Expand Down Expand Up @@ -416,7 +417,7 @@ protected void resolveEpisodeArtifacts()
this.episodeFiles = ArtifactUtils.getFiles(this.episodeArtifacts);
}

protected ClassLoader createClassLoader(ClassLoader parent) {
protected URLClassLoader createClassLoader(ClassLoader parent) {

final Collection<URL> xjcPluginURLs = getXjcPluginURLs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -187,4 +188,14 @@ private static String normalizePattern(String pattern) {

return pattern;
}

public static void close(URLClassLoader classLoader) {
if (classLoader != null) {
try {
classLoader.close();
} catch (IOException io) {
// nothing
}
}
}
}

0 comments on commit efd58cc

Please sign in to comment.