Class ConfigContext

java.lang.Object
cloud.opencode.base.config.jdk25.ConfigContext

public final class ConfigContext extends Object
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 Details

    • OVERRIDES

      public static final ScopedValue<Map<String,String>> OVERRIDES
      Configuration overrides | 配置覆盖
    • PROFILE

      public static final ScopedValue<String> PROFILE
      Current profile | 当前环境
    • TENANT_ID

      public static final ScopedValue<String> 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,X> task) throws X
      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 X
      Execute 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 X
      Execute 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

      public static void runWithOverrides(Map<String,String> overrides, Runnable task)
      Execute task with overrides (void returning) 使用配置覆盖执行任务(无返回值)
      Parameters:
      overrides - configuration overrides | 配置覆盖
      task - task to execute | 要执行的任务
    • runWithProfile

      public static void runWithProfile(String profile, Runnable task)
      Execute task with profile context (void returning) 使用环境上下文执行任务(无返回值)
      Parameters:
      profile - profile name | 环境名称
      task - task to execute | 要执行的任务
    • runWithTenant

      public static void runWithTenant(String tenantId, Runnable task)
      Execute task with tenant context (void returning) 使用租户上下文执行任务(无返回值)
      Parameters:
      tenantId - tenant identifier | 租户标识符
      task - task to execute | 要执行的任务
    • currentOverrides

      public static Optional<Map<String,String>> currentOverrides()
      Get current configuration overrides 获取当前配置覆盖
      Returns:
      optional overrides map | 可选的覆盖映射
    • currentProfile

      public static Optional<String> currentProfile()
      Get current profile 获取当前环境
      Returns:
      optional profile name | 可选的环境名称
    • currentTenant

      public static Optional<String> currentTenant()
      Get current tenant ID 获取当前租户ID
      Returns:
      optional tenant ID | 可选的租户ID