Refactor TenantAware - remove TenantRunner and replace with standard Runnable / Callable (#2755)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-10-14 16:36:42 +03:00
committed by GitHub
parent 0a2f18fbad
commit 04cd9fb30d
14 changed files with 88 additions and 118 deletions

View File

@@ -9,6 +9,8 @@
*/
package org.eclipse.hawkbit.tenancy;
import java.util.concurrent.Callable;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -30,45 +32,25 @@ public interface TenantAware {
String getCurrentUsername();
/**
* Gives the possibility to run a certain code under a specific given {@code tenant}. Only the given {@link TenantRunner} is executed
* under the specific tenant e.g. under control of an {@link ThreadLocal}. After the {@link TenantRunner} it must be ensured that the
* Gives the possibility to run a certain code under a specific given {@code tenant}. Only the given {@link Callable} is executed
* under the specific tenant e.g. under control of an {@link ThreadLocal}. After the {@link Callable} it must be ensured that the
* original tenant before this invocation is reset.
*
* @param tenant the tenant which the specific code should run
* @param tenantRunner the runner which is implemented to run this specific code
* under the given tenant
* @return the return type of the {@link TenantRunner}
* @param callable the runner which is implemented to run this specific code under the given tenant
* @return the return type of the {@link Callable}
*/
<T> T runAsTenant(String tenant, TenantRunner<T> tenantRunner);
<T> T runAsTenant(String tenant, Callable<T> callable);
/**
* Gives the possibility to run a certain code under a specific given {@code tenant} and {@code username}.
* Only the given {@link TenantRunner} is executed under the specific tenant and user e.g. under control of an {@link ThreadLocal}.
* After the {@link TenantRunner} it must be ensured that the original tenant before this invocation is reset.
* Only the given {@link Runnable} is executed under the specific tenant and user e.g. under control of an {@link ThreadLocal}.
* After the {@link Runnable} it must be ensured that the original tenant before this invocation is reset.
*
* @param tenant the tenant which the specific code should run with
* @param username the username which the specific code should run with
* @param tenantRunner the runner which is implemented to run this specific code under the given tenant
* @return the return type of the {@link TenantRunner}
*/
<T> T runAsTenantAsUser(String tenant, String username, TenantRunner<T> tenantRunner);
/**
* An {@link TenantRunner} interface which allows to run specific code under a given tenant by using the
* {@link TenantAware#runAsTenant(String, TenantRunner)}.
*
* @param <T> the return type of the runner
*/
@FunctionalInterface
interface TenantRunner<T> {
/**
* Called to run specific code and a given tenant.
*
* @return the return of the code block running under a certain tenant
*/
T run();
}
void runAsTenantAsUser(String tenant, String username, Runnable runnable);
/**
* Resolves the tenant from the current context.