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

io.soffa.foundation.core.context.TenantHolder Maven / Gradle / Ivy

package io.soffa.foundation.core.context;

import io.soffa.foundation.commons.Logger;
import io.soffa.foundation.commons.TextUtil;
import io.soffa.foundation.core.models.TenantId;
import lombok.SneakyThrows;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

@SuppressWarnings("PMD.ClassNamingConventions")
public final class TenantHolder {

    private static final Logger LOG = Logger.get(TenantHolder.class);

    private static final ThreadLocal CURRENT = new InheritableThreadLocal<>();

    private TenantHolder() {
    }

    public static void clear() {
        set((String)null);
    }

    public static boolean isDefault() {
        String value = CURRENT.get();
        return TextUtil.isEmpty(value) || TenantId.DEFAULT_VALUE.equalsIgnoreCase(value);
    }

    public static Optional get() {
        if (isEmpty(CURRENT.get())) {
            return Optional.empty();
        }
        return Optional.of(CURRENT.get());
    }

    private static boolean isEmpty(String value) {
        return value == null || value.isEmpty();
    }


    public static void set(TenantId tenantId) {
        if (tenantId == null) {
            set((String) null);
        } else {
            set(tenantId.getValue());
        }
    }


    public static void set(String value) {
        Logger.setTenantId(value);
        if (TextUtil.isEmpty(value)) {
            LOG.trace("Active tenant is set to: default");
            CURRENT.remove();
        } else {
            LOG.trace("Active tenant is set to: %s", value);
            CURRENT.set(value);
        }
    }

    public static  T useDefault(Supplier supplier) {
        return use((String) null, supplier);
    }

    public static void use(final String tenantId, Runnable runnable) {
        use(TenantId.of(tenantId), runnable);
    }

    @SneakyThrows
    public static void use(final TenantId tenantId, Runnable runnable) {
        use(tenantId, () -> {
            runnable.run();
            return null;
        });
    }


    @SneakyThrows
    public static  O use(final TenantId tenantId, Supplier supplier) {
        String t = tenantId == null ? null : tenantId.getValue();
        return use(t, supplier);
    }

    @SneakyThrows
    public static  O use(final String tenantId, Supplier supplier) {
        String current = CURRENT.get();
        if (Objects.equals(tenantId, current)) {
            return supplier.get();
        }
        if (TextUtil.isNotEmpty(current)) {
            LOG.trace("Tenant switch %s --> %s", current, tenantId);
        }
        try {
            set(tenantId);
            return supplier.get();
        } finally {
            if (TextUtil.isNotEmpty(current)) {
                LOG.trace("Tenant restored %s --> %s", tenantId, current);
                set(current);
            }
        }
    }

    public static boolean isNotEmpty() {
        return TextUtil.isNotEmpty(CURRENT.get());
    }

    public static boolean isEmpty() {
        return TextUtil.isEmpty(CURRENT.get());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy