Skip to content
Closed
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 @@ -168,7 +168,7 @@ public void close() throws Exception {
*/
protected synchronized IIsolationData getLoader(Predicate<FileCollection> predicate,
Supplier<FileCollection> supplier,
String uniqueId) {
String uniqueId, boolean withContext) {
Optional<IIsolationData> d = this.internalRunners.stream().filter(x -> !x.isRunning())
.filter(x -> predicate.test(x.getExtraData())).findAny();
if (d.isPresent()) {
Expand All @@ -183,7 +183,7 @@ protected synchronized IIsolationData getLoader(Predicate<FileCollection> predic
this.cleanupOld(closeThreshold);
IsolationData data = new IsolationData();
data.classLoader =
getClassLoader((URLClassLoader) Thread.currentThread().getContextClassLoader(), supplier);
getClassLoader((URLClassLoader) Thread.currentThread().getContextClassLoader(), supplier, withContext);
data.running = true;
data.extraData = supplier.get();
stats.track(CachedIsolationStats.EventKind.CREATE, data.getUUID(), maximumLoadersFromConfig, this.internalRunners, uniqueId);
Expand Down Expand Up @@ -246,15 +246,20 @@ protected synchronized void cleanupOld(long pCloseThreshold) {
}

protected ClassLoader getClassLoader(URLClassLoader contextClassLoader,
Supplier<FileCollection> supplier) {
Supplier<FileCollection> supplier,
boolean doMergeContextURLs) {
// Merge the extra classpath with the current classpath
URL[] urls = Stream.concat(supplier.get().getFiles().stream().map(f -> {
Stream<URL> streamOfUrls = supplier.get().getFiles().stream().map(f -> {
try {
return f.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}), Arrays.stream(contextClassLoader.getURLs())).toArray(URL[]::new);
});
if (doMergeContextURLs)
streamOfUrls = Stream.concat(streamOfUrls, Arrays.stream(contextClassLoader.getURLs()));

URL[] urls = streamOfUrls.toArray(URL[]::new);
return new IsolatedURLClassLoader(urls, contextClassLoader, getPassThroughPackages());
}

Expand Down Expand Up @@ -354,7 +359,15 @@ void doExecuteWorkAction(ActualTaskInfo<?> info, long timeWaitedForSemaphore) {
}, prefix, uniqueId,
f -> f.minus(info.classPath).getFiles().isEmpty() && info.classPath.minus(f).getFiles()
.isEmpty(), // check if the difference between the classpaths is empty
() -> info.classPath);
() -> info.classPath,
info.withContext);
}

@Deprecated
protected void executeInClassloader(Runnable runnable, @Nullable String prefix,
@Nullable String uniqueId,
Predicate<FileCollection> predicate, Supplier<FileCollection> supplier) {
this.executeInClassloader(runnable, prefix, uniqueId, predicate, supplier, true);
}

/**
Expand All @@ -366,14 +379,16 @@ void doExecuteWorkAction(ActualTaskInfo<?> info, long timeWaitedForSemaphore) {
* @param uniqueId Uniqueid for tracking via the stats
* @param predicate allows to check for additional elements, such as classpath
* @param supplier allows to set the additional arguments for a new loader
* @param withContext if the URLs of the context classloader should be merged
*/
protected void executeInClassloader(Runnable runnable, @Nullable String prefix,
@Nullable String uniqueId,
Predicate<FileCollection> predicate, Supplier<FileCollection> supplier) {
Predicate<FileCollection> predicate, Supplier<FileCollection> supplier,
boolean withContext) {
final Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
UUID uuid = null;
try (IIsolationData isolationData = getLoader(predicate, supplier, uniqueId)) {
try (IIsolationData isolationData = getLoader(predicate, supplier, uniqueId, withContext)) {
uuid = isolationData.getUUID();
currentThread.setContextClassLoader(isolationData.getClassLoader());

Expand Down Expand Up @@ -500,7 +515,7 @@ public <T extends WorkParameters> void register(UUID uuid,
this.taskInfoMap.put(uuid,
new CachedQueueService.ActualTaskInfo(workActionClass, parameterAction,
Objects.requireNonNull(instantiatorFactory, "instantiatorFactory"),
Objects.requireNonNull(serviceRegistry, "serviceRegistry"), classPath));
Objects.requireNonNull(serviceRegistry, "serviceRegistry"), classPath, false));
}

public static class ActualTaskInfo<T extends WorkParameters> {
Expand All @@ -510,15 +525,17 @@ public static class ActualTaskInfo<T extends WorkParameters> {
public InstantiatorFactory instantiatorFactory;
public ServiceRegistry services;
public FileCollection classPath;
public boolean withContext;

public ActualTaskInfo(Class<? extends WorkAction<T>> workActionClass,
Action<? super WorkParameters> parameterAction, InstantiatorFactory instantiatorFactory,
ServiceRegistry serviceRegistry, FileCollection classPath) {
ServiceRegistry serviceRegistry, FileCollection classPath, boolean withContext) {
this.workActionClass = workActionClass;
this.parameterAction = parameterAction;
this.instantiatorFactory = instantiatorFactory;
this.services = serviceRegistry;
this.classPath = classPath;
this.withContext = withContext;
}
}

Expand Down
Loading