Class TemplateManager

java.lang.Object
cloud.opencode.base.sms.template.TemplateManager

public final class TemplateManager extends Object
Template Manager - Manages SMS templates with caching and loading 模板管理器 - 管理短信模板,支持缓存和加载

Provides comprehensive template management including loading from files, caching, validation, and message creation.

提供全面的模板管理功能,包括从文件加载、缓存、验证和消息创建。

Features | 主要功能:

  • Template registration and caching - 模板注册和缓存
  • Classpath and file-based template loading - 类路径和文件加载模板
  • Template rendering with variable substitution - 变量替换模板渲染
  • Message creation from templates - 从模板创建消息

Usage Examples | 使用示例:

TemplateManager manager = TemplateManager.create()
    .register("verify", "Your verification code is ${code}")
    .register("welcome", "Welcome, ${name}!")
    .loadFromClasspath("/templates/");

// Create message from template
SmsMessage message = manager.createMessage("verify", "13800138000",
    Map.of("code", "123456"));

// Render template
String content = manager.render("welcome", Map.of("name", "John"));

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap storage) - 线程安全: 是(ConcurrentHashMap存储)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-sms V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static TemplateManager create()
      Creates new template manager. 创建新的模板管理器。
      Returns:
      the manager | 管理器
    • register

      public TemplateManager register(SmsTemplate template)
      Registers a template. 注册模板。
      Parameters:
      template - the template | 模板
      Returns:
      this | 此对象
    • register

      public TemplateManager register(String id, String content)
      Registers a template by ID and content. 按ID和内容注册模板。
      Parameters:
      id - the template ID | 模板ID
      content - the template content | 模板内容
      Returns:
      this | 此对象
    • register

      public TemplateManager register(String id, String content, String description)
      Registers a template with description. 注册带描述的模板。
      Parameters:
      id - the template ID | 模板ID
      content - the template content | 模板内容
      description - the description | 描述
      Returns:
      this | 此对象
    • unregister

      public TemplateManager unregister(String id)
      Unregisters a template. 取消注册模板。
      Parameters:
      id - the template ID | 模板ID
      Returns:
      this | 此对象
    • clear

      public TemplateManager clear()
      Clears all templates. 清除所有模板。
      Returns:
      this | 此对象
    • loadFromDirectory

      public TemplateManager loadFromDirectory(Path directory)
      Loads templates from directory. 从目录加载模板。
      Parameters:
      directory - the directory path | 目录路径
      Returns:
      this | 此对象
    • loadFromFile

      public TemplateManager loadFromFile(Path file)
      Loads a template from file. 从文件加载模板。
      Parameters:
      file - the file path | 文件路径
      Returns:
      this | 此对象
    • loadFromClasspath

      public TemplateManager loadFromClasspath(String resourcePath)
      Loads templates from classpath directory. 从类路径目录加载模板。
      Parameters:
      resourcePath - the resource path (e.g., "/templates/") | 资源路径
      Returns:
      this | 此对象
    • loadFromResource

      public TemplateManager loadFromResource(String id, String resourcePath)
      Loads a template from classpath resource. 从类路径资源加载模板。
      Parameters:
      id - the template ID | 模板ID
      resourcePath - the resource path | 资源路径
      Returns:
      this | 此对象
    • get

      public SmsTemplate get(String id)
      Gets a template by ID. 按ID获取模板。
      Parameters:
      id - the template ID | 模板ID
      Returns:
      the template | 模板
      Throws:
      SmsTemplateException - if not found
    • find

      public Optional<SmsTemplate> find(String id)
      Finds a template by ID. 按ID查找模板。
      Parameters:
      id - the template ID | 模板ID
      Returns:
      the optional template | 可选模板
    • contains

      public boolean contains(String id)
      Checks if a template exists. 检查模板是否存在。
      Parameters:
      id - the template ID | 模板ID
      Returns:
      true if exists | 如果存在返回true
    • getAll

      public Map<String, SmsTemplate> getAll()
      Gets all templates. 获取所有模板。
      Returns:
      the templates | 模板映射
    • size

      public int size()
      Gets the template count. 获取模板数量。
      Returns:
      the count | 数量
    • getTemplateIds

      public List<String> getTemplateIds()
      Gets template IDs. 获取模板ID列表。
      Returns:
      the IDs | ID列表
    • render

      public String render(String id, Map<String,String> variables)
      Renders a template with variables. 使用变量渲染模板。
      Parameters:
      id - the template ID | 模板ID
      variables - the variables | 变量
      Returns:
      the rendered content | 渲染后的内容
    • createMessage

      public SmsMessage createMessage(String templateId, String phoneNumber, Map<String,String> variables)
      Creates a message from template. 从模板创建消息。
      Parameters:
      templateId - the template ID | 模板ID
      phoneNumber - the phone number | 手机号码
      variables - the variables | 变量
      Returns:
      the message | 消息
    • createMessages

      public List<SmsMessage> createMessages(String templateId, List<String> phoneNumbers, Map<String,String> variables)
      Creates messages for multiple recipients. 为多个接收者创建消息。
      Parameters:
      templateId - the template ID | 模板ID
      phoneNumbers - the phone numbers | 手机号码列表
      variables - the variables | 变量
      Returns:
      the messages | 消息列表
    • validateVariables

      public void validateVariables(SmsTemplate template, Map<String,String> variables)
      Validates template variables. 验证模板变量。
      Parameters:
      template - the template | 模板
      variables - the variables | 变量
      Throws:
      SmsTemplateException - if variables are missing
    • validateAll

      public TemplateManager validateAll()
      Validates all templates. 验证所有模板。
      Returns:
      this | 此对象
      Throws:
      SmsTemplateException - if any template is invalid
    • forEach

      public TemplateManager forEach(Consumer<SmsTemplate> action)
      Iterates over all templates. 迭代所有模板。
      Parameters:
      action - the action | 动作
      Returns:
      this | 此对象
    • toRegistry

      public SmsTemplateRegistry toRegistry()
      Creates a registry from this manager. 从此管理器创建注册表。
      Returns:
      the registry | 注册表
    • importFrom

      public TemplateManager importFrom(SmsTemplateRegistry registry)
      Imports templates from a registry. 从注册表导入模板。
      Parameters:
      registry - the registry | 注册表
      Returns:
      this | 此对象