
org.junit.gen5.console.tasks.CustomContextClassLoaderExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-console Show documentation
Show all versions of junit-console Show documentation
Module "junit-console" of JUnit 5.
The newest version!
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.gen5.console.tasks;
import java.util.Optional;
import java.util.concurrent.Callable;
class CustomContextClassLoaderExecutor {
private final Optional customClassLoader;
CustomContextClassLoaderExecutor(Optional customClassLoader) {
this.customClassLoader = customClassLoader;
}
T invoke(Callable callable) throws Exception {
if (customClassLoader.isPresent()) {
// Only get/set context class loader when necessary to prevent problems with
// security managers
return replaceThreadContextClassLoaderAndInvoke(customClassLoader.get(), callable);
}
return callable.call();
}
private T replaceThreadContextClassLoaderAndInvoke(ClassLoader customClassLoader, Callable callable)
throws Exception {
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(customClassLoader);
return callable.call();
}
finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy