Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,22 @@ protected boolean isReGenerationRequired() throws MojoExecutionException {
return stale;
}

/**
* Imports the {@link #SYSTEM_TOOLS_CLASSLOADER_PACKAGES} into the given plugin realm.
* Maven gives all modules of a build that use this plugin the same plugin realm, so schemagen
* executions in a parallel build call this at the same time. {@link ClassRealm#importFrom(ClassLoader, String)}
* is not thread-safe, so the imports are made while holding the realm's lock.
*
* @param realm the plugin realm that loaded this mojo.
*/
static void importSystemToolsPackages(final ClassRealm realm) {
synchronized (realm) {
for (String current : SYSTEM_TOOLS_CLASSLOADER_PACKAGES) {
realm.importFrom((ClassLoader) null, current);
}
}
}

/**
* {@inheritDoc}
*/
Expand All @@ -383,10 +399,7 @@ protected boolean performExecution() throws MojoExecutionException, MojoFailureE
// is delegated to that ClassLoader, to comply with SchemaGen's internal reflective loading
// of classes. Otherwise we will have ClassCastExceptions instead of proper execution.
//
final ClassRealm localRealm = (ClassRealm) getClass().getClassLoader();
for (String current : SYSTEM_TOOLS_CLASSLOADER_PACKAGES) {
localRealm.importFrom((ClassLoader) null, current);
}
importSystemToolsPackages((ClassRealm) getClass().getClassLoader());

// Configure the ThreadContextClassLoaderBuilder, to enable synthesizing a correct ClassPath for the tool.
final ThreadContextClassLoaderBuilder classLoaderBuilder = ThreadContextClassLoaderBuilder.createFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.maven.model.Resource;
import org.codehaus.mojo.jaxb2.BufferingLog;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -155,6 +162,42 @@ void validateReGenerationNotRequiredWhenOutputDirHasFilesAndStaleFileFresh(@Temp
assertTrue(mojo.isReGenerationRequired());
}

@Test
void importSystemToolsPackagesFromParallelExecutions() throws Exception {
// Maven shares one plugin realm between all modules that use the plugin, so parallel schemagen
// executions import into the same realm at the same moment. Without a lock, this corrupts the
// realm's TreeSet of imports about once per few thousand rounds.
final int threads = 2;
final int rounds = 20_000;
final ClassWorld world = new ClassWorld();
final ExecutorService pool = Executors.newFixedThreadPool(threads);
try {
for (int round = 0; round < rounds; round++) {
final ClassRealm pluginRealm = world.newRealm("plugin-" + round);
// With a build extension, Maven imports the project realm into the plugin realm first.
pluginRealm.importFrom(world.newRealm("project-" + round), "");

final CyclicBarrier start = new CyclicBarrier(threads);
final List<Future<?>> executions = new ArrayList<>();
for (int i = 0; i < threads; i++) {
executions.add(pool.submit(() -> {
start.await();
AbstractXsdGeneratorMojo.importSystemToolsPackages(pluginRealm);
return null;
}));
}
for (Future<?> execution : executions) {
execution.get();
}

world.disposeRealm("plugin-" + round);
world.disposeRealm("project-" + round);
}
} finally {
pool.shutdownNow();
}
}

private static URL createFileUrl(final File file) throws MalformedURLException {
return file.toPath().toUri().toURL();
}
Expand Down