Java Code Examples for org.springframework.util.ClassUtils#overrideThreadContextClassLoader()

The following examples show how to use org.springframework.util.ClassUtils#overrideThreadContextClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextFunctionCatalogAutoConfigurationTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void componentScanJarFunction() {
	try {
		create("greeter.jar", ComponentScanJarConfiguration.class);
		assertThat(this.context.getBean("greeter")).isInstanceOf(Function.class);
		assertThat((Function<?, ?>) this.catalog.lookup(Function.class, "greeter"))
				.isInstanceOf(Function.class);
		assertThat(this.inspector
				.getInputType(this.catalog.lookup(Function.class, "greeter")))
						.isAssignableFrom(String.class);
		assertThat(this.inspector
				.getInputWrapper(this.catalog.lookup(Function.class, "greeter")))
						.isAssignableFrom(String.class);
	}
	finally {
		ClassUtils.overrideThreadContextClassLoader(getClass().getClassLoader());
	}
}
 
Example 2
Source File: LauncherState.java    From spring-init with Apache License 2.0 5 votes vote down vote up
@Override
@TearDown(Level.Invocation)
public void close() throws IOException {
	// CachedIntrospectionResults.clearClassLoader(getClass().getClassLoader());
	if (instance != null) {
		instance.close();
	}
	if (runThread != null) {
		runThread.setContextClassLoader(null);
		runThread = null;
	}
	if (orig != null) {
		ClassUtils.overrideThreadContextClassLoader(orig);
	}
	if (loader != null) {
		try {
			loader.close();
			loader = null;
		}
		catch (Exception e) {
			System.err.println("Failed to close loader " + e);
		}
	}
	System.gc();
	for (Object key : this.args.keySet()) {
		System.clearProperty(key.toString());
	}
}
 
Example 3
Source File: LauncherState.java    From spring-init with Apache License 2.0 5 votes vote down vote up
private Class<?> loadMainClass(Class<?> type) throws ClassNotFoundException {
	URL[] urls = filterClassPath(
			((URLClassLoader) getClass().getClassLoader()).getURLs());
	loader = new URLClassLoader(urls, getClass().getClassLoader().getParent());
	orig = ClassUtils.overrideThreadContextClassLoader(loader);
	return loader.loadClass(type.getName());
}
 
Example 4
Source File: ContextFunctionCatalogAutoConfigurationTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private void create(String jarfile, Class<?> config, String... props) {
	try {
		URL[] urls = new URL[] { new ClassPathResource(jarfile).getURL() };
		ClassUtils.overrideThreadContextClassLoader(
				new URLClassLoader(urls, getClass().getClassLoader()));
		create(config, props);
	}
	catch (Exception e) {
		ReflectionUtils.rethrowRuntimeException(e);
	}
}
 
Example 5
Source File: IsolatedFunction.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public T apply(S item) {
	ClassLoader context = ClassUtils
			.overrideThreadContextClassLoader(this.classLoader);
	try {
		return this.function.apply(item);
	}
	finally {
		ClassUtils.overrideThreadContextClassLoader(context);
	}
}
 
Example 6
Source File: IsolatedConsumer.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(T item) {
	ClassLoader context = ClassUtils
			.overrideThreadContextClassLoader(this.classLoader);
	try {
		this.consumer.accept(item);
	}
	finally {
		ClassUtils.overrideThreadContextClassLoader(context);
	}
}
 
Example 7
Source File: IsolatedSupplier.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public T get() {
	ClassLoader context = ClassUtils
			.overrideThreadContextClassLoader(this.classLoader);
	try {
		return this.supplier.get();
	}
	finally {
		ClassUtils.overrideThreadContextClassLoader(context);
	}
}
 
Example 8
Source File: SpringJobBeanFactory.java    From niubi-job with Apache License 2.0 4 votes vote down vote up
/**
 * 该构造函数用于集群环境
 *
 * @throws BeansException
 */
public SpringJobBeanFactory(ClassLoader classLoader) throws BeansException {
    this.classLoader = classLoader;
    ClassUtils.overrideThreadContextClassLoader(classLoader);
    this.applicationContext = new ClassPathXmlApplicationContext(JobScanner.APPLICATION_CONTEXT_XML_PATH);
}
 
Example 9
Source File: RestartEndpoint.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
private void overrideClassLoaderForRestart() {
	ClassUtils.overrideThreadContextClassLoader(
			this.application.getClass().getClassLoader());
}
 
Example 10
Source File: RemotingSupport.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Override the thread context ClassLoader with the environment's bean ClassLoader
 * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
 * context ClassLoader already.
 * @return the original thread context ClassLoader, or {@code null} if not overridden
 */
@Nullable
protected ClassLoader overrideThreadContextClassLoader() {
	return ClassUtils.overrideThreadContextClassLoader(getBeanClassLoader());
}
 
Example 11
Source File: RemotingSupport.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Override the thread context ClassLoader with the environment's bean ClassLoader
 * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
 * context ClassLoader already.
 * @return the original thread context ClassLoader, or {@code null} if not overridden
 */
@Nullable
protected ClassLoader overrideThreadContextClassLoader() {
	return ClassUtils.overrideThreadContextClassLoader(getBeanClassLoader());
}
 
Example 12
Source File: RemotingSupport.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Override the thread context ClassLoader with the environment's bean ClassLoader
 * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
 * context ClassLoader already.
 * @return the original thread context ClassLoader, or {@code null} if not overridden
 */
protected ClassLoader overrideThreadContextClassLoader() {
	return ClassUtils.overrideThreadContextClassLoader(getBeanClassLoader());
}
 
Example 13
Source File: RemotingSupport.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Override the thread context ClassLoader with the environment's bean ClassLoader
 * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
 * context ClassLoader already.
 * @return the original thread context ClassLoader, or {@code null} if not overridden
 */
protected ClassLoader overrideThreadContextClassLoader() {
	return ClassUtils.overrideThreadContextClassLoader(getBeanClassLoader());
}