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

com.atlassian.bamboo.specs.maven.sandbox.PrivilegedThreads Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
package com.atlassian.bamboo.specs.maven.sandbox;

import com.google.common.annotations.VisibleForTesting;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public final class PrivilegedThreads {
    private static volatile ThreadPermissionVerifier defaultPermissionVerfier;

    private static AtomicReference> permissionVerifiers = new AtomicReference<>();

    private PrivilegedThreads() {
    }

    static ThreadPermissionVerifier getThreadPermissionChecker() {
        final ThreadPermissionVerifier threadPermissionVerifier = permissionVerifiers.get().get(Thread.currentThread());
        return threadPermissionVerifier!=null ? threadPermissionVerifier : defaultPermissionVerfier;
    }

    static void setThreadPermissionCheckers(final Map verifiers, final ThreadPermissionVerifier defaultPermissionVerifier) {
        if (!permissionVerifiers.compareAndSet(null, verifiers)) {
            throw new IllegalStateException("Cannot nest secure method invocations");
        }
        defaultPermissionVerfier = defaultPermissionVerifier;
    }

    @VisibleForTesting
    public static void resetThreadPermissionCheckers() {
        if (permissionVerifiers.get() != null) {
            getThreadPermissionChecker().checkPermission(new RuntimePermission("setSecurityManager"));
            defaultPermissionVerfier = null;
            permissionVerifiers.set(null);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy