org.wildfly.security.auth.server.FlexibleIdentityAssociation Maven / Gradle / Ivy
The newest version!
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.security.auth.server;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.ObjIntConsumer;
import java.util.function.Supplier;
import org.wildfly.common.Assert;
import org.wildfly.common.function.ExceptionBiConsumer;
import org.wildfly.common.function.ExceptionBiFunction;
import org.wildfly.common.function.ExceptionBiPredicate;
import org.wildfly.common.function.ExceptionObjIntConsumer;
import org.wildfly.security.auth.server._private.ElytronMessages;
/**
* A flexible identity association which can have its current identity modified. Modifying the identity association
* will affect the current identity of any thread which is currently executing within the scope of this association.
*
* @see SecurityIdentity#createFlexibleAssociation
* @author David M. Lloyd
*/
public final class FlexibleIdentityAssociation implements Scoped, Supplier {
private final SecurityDomain securityDomain;
private volatile SecurityIdentity securityIdentity;
FlexibleIdentityAssociation(final SecurityDomain securityDomain, final SecurityIdentity securityIdentity) {
assert securityIdentity != null && securityDomain == securityIdentity.getSecurityDomain();
this.securityDomain = securityDomain;
this.securityIdentity = securityIdentity;
}
/**
* Set the current associated identity.
*
* @param securityIdentity the current associated identity (must not be {@code null})
*/
public void setIdentity(SecurityIdentity securityIdentity) {
Assert.checkNotNullParam("securityIdentity", securityIdentity);
if (securityIdentity.getSecurityDomain() != securityDomain) {
throw ElytronMessages.log.securityDomainMismatch();
}
this.securityIdentity = securityIdentity;
}
/**
* Get the current associated identity.
*
* @return the current associated identity (not {@code null})
*/
public SecurityIdentity get() {
return securityIdentity;
}
public R runAsFunction(final BiFunction action, final T parameter1, final U parameter2) {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
return action.apply(parameter1, parameter2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public void runAsConsumer(final BiConsumer action, final T parameter1, final U parameter2) {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
action.accept(parameter1, parameter2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public void runAsObjIntConsumer(final ObjIntConsumer action, final T parameter1, final int parameter2) {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
action.accept(parameter1, parameter2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public R runAsFunctionEx(final ExceptionBiFunction action, final T parameter1, final U parameter2) throws E {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
return action.apply(parameter1, parameter2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public void runAsConsumerEx(final ExceptionBiConsumer action, final T parameter1, final U parameter2) throws E {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
action.accept(parameter1, parameter2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public void runAsObjIntConsumerEx(final ExceptionObjIntConsumer action, final T parameter1, final int parameter2) throws E {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
action.accept(parameter1, parameter2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public boolean runAsBiPredicate(BiPredicate action, T param1, U param2) {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
return action.test(param1, param2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
public boolean runAsExBiPredicate(ExceptionBiPredicate action, T param1, U param2) throws E {
final Supplier old = securityDomain.getAndSetCurrentSecurityIdentity(this);
try {
return action.test(param1, param2);
} finally {
securityDomain.setCurrentSecurityIdentity(old);
}
}
}