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

org.wildfly.clustering.context.ContextClassLoaderReference Maven / Gradle / Ivy

There is a newer version: 5.0.7.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */
package org.wildfly.clustering.context;

import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 * Thread-aware reference for a context {@link ClassLoader}.
 * @author Paul Ferraro
 */
public enum ContextClassLoaderReference implements ThreadContextReference {
	INSTANCE;

	@Override
	public ClassLoader apply(Thread thread) {
		if (System.getSecurityManager() == null) {
			return thread.getContextClassLoader();
		}
		return AccessController.doPrivileged(new PrivilegedAction<>() {
			@Override
			public ClassLoader run() {
				return thread.getContextClassLoader();
			}
		});
	}

	@Override
	public void accept(Thread thread, ClassLoader loader) {
		if (System.getSecurityManager() == null) {
			thread.setContextClassLoader(loader);
		} else {
			AccessController.doPrivileged(new PrivilegedAction<>() {
				@Override
				public Void run() {
					thread.setContextClassLoader(loader);
					return null;
				}
			});
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy