All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.gen5.console.tasks.CustomContextClassLoaderExecutor Maven / Gradle / Ivy

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