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: 33.0.2.Final
Show newest version
/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.context;

import java.util.AbstractMap;
import java.util.Map;

import org.wildfly.security.ParametricPrivilegedAction;
import org.wildfly.security.manager.WildFlySecurityManager;

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

    private static final ParametricPrivilegedAction GET_CONTEXT_CLASS_LOADER_ACTION = new ParametricPrivilegedAction<>() {
        @Override
        public ClassLoader run(Thread thread) {
            return thread.getContextClassLoader();
        }
    };

    private static final ParametricPrivilegedAction> SET_CONTEXT_CLASS_LOADER_ACTION = new ParametricPrivilegedAction<>() {
        @Override
        public Void run(Map.Entry entry) {
            entry.getKey().setContextClassLoader(entry.getValue());
            return null;
        }
    };

    @Override
    public ClassLoader apply(Thread thread) {
        return WildFlySecurityManager.doUnchecked(thread, GET_CONTEXT_CLASS_LOADER_ACTION);
    }

    @Override
    public void accept(Thread thread, ClassLoader loader) {
        WildFlySecurityManager.doUnchecked(new AbstractMap.SimpleImmutableEntry<>(thread, loader), SET_CONTEXT_CLASS_LOADER_ACTION);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy