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

org.wildfly.security.auth.server.Scoped Maven / Gradle / Ivy

There is a newer version: 2.4.1.Final
Show 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.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.LongFunction;
import java.util.function.ObjIntConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.wildfly.common.function.ExceptionBiConsumer;
import org.wildfly.common.function.ExceptionBiFunction;
import org.wildfly.common.function.ExceptionBiPredicate;
import org.wildfly.common.function.ExceptionConsumer;
import org.wildfly.common.function.ExceptionFunction;
import org.wildfly.common.function.ExceptionIntFunction;
import org.wildfly.common.function.ExceptionLongFunction;
import org.wildfly.common.function.ExceptionObjIntConsumer;
import org.wildfly.common.function.ExceptionPredicate;
import org.wildfly.common.function.ExceptionSupplier;

/**
 * An identity configuration which can be applied on a scoped basis.
 *
 * @author David M. Lloyd
 */
public interface Scoped {
    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     */
    default void runAs(Runnable action) {
        if (action == null) return;
        runAsConsumer(Runnable::run, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     * @param  the action return type
     * @return the action result (may be {@code null})
     * @throws Exception if the action fails
     */
    default  T runAs(Callable action) throws Exception {
        if (action == null) return null;
        return runAsFunctionEx(Callable::call, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action parameter type
     * @return the action result (may be {@code null})
     */
    default  R runAsFunction(Function action, T parameter) {
        if (action == null) return null;
        return runAsFunction(Function::apply, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action first parameter type
     * @param  the action second parameter type
     * @return the action result (may be {@code null})
     */
     R runAsFunction(BiFunction action, T parameter1, U parameter2);

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action parameter type
     */
    default  void runAsConsumer(Consumer action, T parameter) {
        if (action == null) return;
        runAsConsumer(Consumer::accept, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     * @param  the action second parameter type
     */
     void runAsConsumer(BiConsumer action, T parameter1, U parameter2);

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     */
     void runAsObjIntConsumer(ObjIntConsumer action, T parameter1, int parameter2);

    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     * @param  the action return type
     * @return the action result (may be {@code null})
     */
    default  T runAsSupplier(Supplier action) {
        if (action == null) return null;
        return runAsFunction(Supplier::get, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action parameter type
     * @param  the action exception type
     * @return the action result (may be {@code null})
     * @throws E if the action throws this exception
     */
    default  R runAsFunctionEx(ExceptionFunction action, T parameter) throws E {
        if (action == null) return null;
        return runAsFunctionEx(ExceptionFunction::apply, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action return type
     * @param  the action first parameter type
     * @param  the action second parameter type
     * @param  the action exception type
     * @return the action result (may be {@code null})
     * @throws E if the action throws this exception
     */
     R runAsFunctionEx(ExceptionBiFunction action, T parameter1, U parameter2) throws E;

    /**
     * Run an action under this identity.
     *
     * @param parameter the parameter to pass to the action
     * @param action the action to run
     * @param  the action parameter type
     * @param  the action exception type
     * @throws E if the action throws this exception
     */
    default  void runAsConsumerEx(ExceptionConsumer action, T parameter) throws E {
        if (action == null) return;
        runAsConsumerEx(ExceptionConsumer::accept, action, parameter);
    }

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     * @param  the action second parameter type
     * @param  the action exception type
     * @throws E if the action throws this exception
     */
     void runAsConsumerEx(ExceptionBiConsumer action, T parameter1, U parameter2) throws E;

    /**
     * Run an action under this identity.
     *
     * @param parameter1 the first parameter to pass to the action
     * @param parameter2 the second parameter to pass to the action
     * @param action the action to run
     * @param  the action first parameter type
     * @param  the action exception type
     * @throws E if the action throws this exception
     */
     void runAsObjIntConsumerEx(ExceptionObjIntConsumer action, T parameter1, int parameter2) throws E;

    /**
     * Run an action under this identity.
     *
     * @param action the action to run
     * @param  the action return type
     * @param  the action exception type
     * @return the action result (may be {@code null})
     * @throws E if the action throws this exception
     */
    default  T runAsSupplierEx(ExceptionSupplier action) throws E {
        if (action == null) return null;
        return runAsFunctionEx(ExceptionSupplier::get, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the task to run (must not be {@code null})
     * @param  the return value type
     * @return the action return value
     */
    default  R runAsAction(PrivilegedAction action) {
        if (action == null) return null;
        return runAsFunction(PrivilegedAction::run, action);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the task to run (must not be {@code null})
     * @param  the return value type
     * @return the action return value
     * @throws PrivilegedActionException if the action fails with an exception
     */
    default  R runAsExceptionAction(PrivilegedExceptionAction action) throws PrivilegedActionException {
        if (action == null) return null;
        try {
            return runAsFunctionEx(PrivilegedExceptionAction::run, action);
        } catch (Exception e) {
            throw new PrivilegedActionException(e);
        }
    }

    /**
     * Run an action under this identity.
     *
     * @param predicate the task to run (must not be {@code null})
     * @param param1 the first parameter to pass to the task
     * @param param2 the second parameter to pass to the task
     * @param  the first parameter type
     * @param  the second parameter type
     * @return the action return value
     * @throws UnsupportedOperationException if this operation is not implemented
     */
    default  boolean runAsBiPredicate(BiPredicate predicate, T param1, U param2) {
        throw new UnsupportedOperationException();
    }

    /**
     * Run an action under this identity.
     *
     * @param predicate the task to run (must not be {@code null})
     * @param param1 the first parameter to pass to the task
     * @param param2 the second parameter to pass to the task
     * @param  the first parameter type
     * @param  the second parameter type
     * @param  the exception type
     * @return the action return value
     * @throws E if an exception occurs in the task
     * @throws UnsupportedOperationException if this operation is not implemented
     */
    default  boolean runAsExBiPredicate(ExceptionBiPredicate predicate, T param1, U param2) throws E {
        throw new UnsupportedOperationException();
    }

    /**
     * Run an action under this identity.
     *
     * @param predicate the task to run (must not be {@code null})
     * @param param the parameter to pass to the task
     * @param  the first parameter type
     * @return the action return value
     */
    default  boolean runAsPredicate(Predicate predicate, T param) {
        if (predicate == null) return false;
        return runAsBiPredicate(Predicate::test, predicate, param);
    }

    /**
     * Run an action under this identity.
     *
     * @param predicate the task to run (must not be {@code null})
     * @param param the parameter to pass to the task
     * @param  the first parameter type
     * @param  the exception type
     * @return the action return value
     * @throws E if an exception occurs in the task
     */
    default  boolean runAsExPredicate(ExceptionPredicate predicate, T param) throws E {
        if (predicate == null) return false;
        return runAsExBiPredicate(ExceptionPredicate::test, predicate, param);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the task to run (must not be {@code null})
     * @param value the parameter to pass to the task
     * @param  the return value type
     * @return the action return value
     */
    default  T runAsIntFunction(IntFunction action, int value) {
        if (action == null) return null;
        return runAsFunction(IntFunction::apply, action, value);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the task to run (must not be {@code null})
     * @param value the parameter to pass to the task
     * @param  the return value type
     * @param  the exception type
     * @return the action return value
     * @throws E if an exception occurs in the task
     */
    default  T runAsExIntFunction(ExceptionIntFunction action, int value) throws E {
        if (action == null) return null;
        return runAsFunctionEx(ExceptionIntFunction::apply, action, value);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the task to run (must not be {@code null})
     * @param value the parameter to pass to the task
     * @param  the return value type
     * @return the action return value
     */
    default  T runAsLongFunction(LongFunction action, long value) {
        if (action == null) return null;
        return runAsFunction(LongFunction::apply, action, value);
    }

    /**
     * Run an action under this identity.
     *
     * @param action the task to run (must not be {@code null})
     * @param value the parameter to pass to the task
     * @param  the return value type
     * @param  the exception type
     * @return the action return value
     * @throws E if an exception occurs in the task
     */
    default  T runAsExLongFunction(ExceptionLongFunction action, long value) throws E {
        if (action == null) return null;
        return runAsFunctionEx(ExceptionLongFunction::apply, action, value);
    }
}