Class ConfigContext
java.lang.Object
cloud.opencode.base.config.jdk25.ConfigContext
Configuration Context using JDK 25 Scoped Values
使用JDK 25作用域值的配置上下文
Provides thread-local-like configuration context using JDK 25 ScopedValue feature. Useful for multi-tenant, multi-profile, and test override scenarios.
使用JDK 25 ScopedValue特性提供类似线程本地的配置上下文。适用于多租户、多环境和测试覆盖场景。
Features | 主要功能:
- Configuration overrides per execution context - 每个执行上下文的配置覆盖
- Profile-specific context - 特定环境的上下文
- Multi-tenant configuration - 多租户配置
- Virtual thread friendly - 虚拟线程友好
Usage Examples | 使用示例:
// Run with tenant context
ConfigContext.withTenant("tenant-123", () -> {
String apiKey = OpenConfig.getString("api.key");
return processRequest(apiKey);
});
// Run with config overrides
Map<String, String> testOverrides = Map.of("database.url", "jdbc:h2:mem:test");
ConfigContext.withOverrides(testOverrides, () -> {
runTests();
return null;
});
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Inheritance-safe: Via ScopedValue semantics - 继承安全: 通过ScopedValue语义
- Since:
- JDK 25, opencode-base-config V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ScopedValue<Map<String, String>> Configuration overrides | 配置覆盖static final ScopedValue<String> Current profile | 当前环境static final ScopedValue<String> Tenant ID for multi-tenant scenarios | 多租户场景的租户ID -
Method Summary
Modifier and TypeMethodDescriptionGet current configuration overrides 获取当前配置覆盖Get current profile 获取当前环境Get current tenant ID 获取当前租户IDstatic voidrunWithOverrides(Map<String, String> overrides, Runnable task) Execute task with overrides (void returning) 使用配置覆盖执行任务(无返回值)static voidrunWithProfile(String profile, Runnable task) Execute task with profile context (void returning) 使用环境上下文执行任务(无返回值)static voidrunWithTenant(String tenantId, Runnable task) Execute task with tenant context (void returning) 使用租户上下文执行任务(无返回值)static <T, X extends Throwable>
TwithOverrides(Map<String, String> overrides, ScopedValue.CallableOp<T, X> task) Execute task with configuration overrides 使用配置覆盖执行任务static <T, X extends Throwable>
TwithProfile(String profile, ScopedValue.CallableOp<T, X> task) Execute task with profile context 使用环境上下文执行任务static <T, X extends Throwable>
TwithTenant(String tenantId, ScopedValue.CallableOp<T, X> task) Execute task with tenant context 使用租户上下文执行任务
-
Field Details
-
OVERRIDES
Configuration overrides | 配置覆盖 -
PROFILE
Current profile | 当前环境 -
TENANT_ID
Tenant ID for multi-tenant scenarios | 多租户场景的租户ID
-
-
Method Details
-
withOverrides
public static <T, X extends Throwable> T withOverrides(Map<String, String> overrides, ScopedValue.CallableOp<T, throws XX> task) Execute task with configuration overrides 使用配置覆盖执行任务- Type Parameters:
T- result type | 结果类型X- exception type | 异常类型- Parameters:
overrides- configuration overrides | 配置覆盖task- task to execute | 要执行的任务- Returns:
- task result | 任务结果
- Throws:
X- if task throws exception | 如果任务抛出异常
-
withProfile
public static <T, X extends Throwable> T withProfile(String profile, ScopedValue.CallableOp<T, X> task) throws XExecute task with profile context 使用环境上下文执行任务- Type Parameters:
T- result type | 结果类型X- exception type | 异常类型- Parameters:
profile- profile name | 环境名称task- task to execute | 要执行的任务- Returns:
- task result | 任务结果
- Throws:
X- if task throws exception | 如果任务抛出异常
-
withTenant
public static <T, X extends Throwable> T withTenant(String tenantId, ScopedValue.CallableOp<T, X> task) throws XExecute task with tenant context 使用租户上下文执行任务- Type Parameters:
T- result type | 结果类型X- exception type | 异常类型- Parameters:
tenantId- tenant identifier | 租户标识符task- task to execute | 要执行的任务- Returns:
- task result | 任务结果
- Throws:
X- if task throws exception | 如果任务抛出异常
-
runWithOverrides
-
runWithProfile
-
runWithTenant
-
currentOverrides
-
currentProfile
-
currentTenant
-