Class TemplateManager
java.lang.Object
cloud.opencode.base.sms.template.TemplateManager
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 Summary
Modifier and TypeMethodDescriptionclear()Clears all templates.booleanChecks if a template exists.static TemplateManagercreate()Creates new template manager.Creates a message from template.Creates messages for multiple recipients.Finds a template by ID.forEach(Consumer<SmsTemplate> action) Iterates over all templates.Gets a template by ID.getAll()Gets all templates.Gets template IDs.importFrom(SmsTemplateRegistry registry) Imports templates from a registry.loadFromClasspath(String resourcePath) Loads templates from classpath directory.loadFromDirectory(Path directory) Loads templates from directory.loadFromFile(Path file) Loads a template from file.loadFromResource(String id, String resourcePath) Loads a template from classpath resource.register(SmsTemplate template) Registers a template.Registers a template by ID and content.Registers a template with description.Renders a template with variables.intsize()Gets the template count.Creates a registry from this manager.unregister(String id) Unregisters a template.Validates all templates.voidvalidateVariables(SmsTemplate template, Map<String, String> variables) Validates template variables.
-
Method Details
-
create
Creates new template manager. 创建新的模板管理器。- Returns:
- the manager | 管理器
-
register
Registers a template. 注册模板。- Parameters:
template- the template | 模板- Returns:
- this | 此对象
-
register
Registers a template by ID and content. 按ID和内容注册模板。- Parameters:
id- the template ID | 模板IDcontent- the template content | 模板内容- Returns:
- this | 此对象
-
register
Registers a template with description. 注册带描述的模板。- Parameters:
id- the template ID | 模板IDcontent- the template content | 模板内容description- the description | 描述- Returns:
- this | 此对象
-
unregister
Unregisters a template. 取消注册模板。- Parameters:
id- the template ID | 模板ID- Returns:
- this | 此对象
-
clear
-
loadFromDirectory
Loads templates from directory. 从目录加载模板。- Parameters:
directory- the directory path | 目录路径- Returns:
- this | 此对象
-
loadFromFile
Loads a template from file. 从文件加载模板。- Parameters:
file- the file path | 文件路径- Returns:
- this | 此对象
-
loadFromClasspath
Loads templates from classpath directory. 从类路径目录加载模板。- Parameters:
resourcePath- the resource path (e.g., "/templates/") | 资源路径- Returns:
- this | 此对象
-
loadFromResource
Loads a template from classpath resource. 从类路径资源加载模板。- Parameters:
id- the template ID | 模板IDresourcePath- the resource path | 资源路径- Returns:
- this | 此对象
-
get
Gets a template by ID. 按ID获取模板。- Parameters:
id- the template ID | 模板ID- Returns:
- the template | 模板
- Throws:
SmsTemplateException- if not found
-
find
Finds a template by ID. 按ID查找模板。- Parameters:
id- the template ID | 模板ID- Returns:
- the optional template | 可选模板
-
contains
Checks if a template exists. 检查模板是否存在。- Parameters:
id- the template ID | 模板ID- Returns:
- true if exists | 如果存在返回true
-
getAll
-
size
public int size()Gets the template count. 获取模板数量。- Returns:
- the count | 数量
-
getTemplateIds
-
render
-
createMessage
-
createMessages
public List<SmsMessage> createMessages(String templateId, List<String> phoneNumbers, Map<String, String> variables) Creates messages for multiple recipients. 为多个接收者创建消息。- Parameters:
templateId- the template ID | 模板IDphoneNumbers- the phone numbers | 手机号码列表variables- the variables | 变量- Returns:
- the messages | 消息列表
-
validateVariables
Validates template variables. 验证模板变量。- Parameters:
template- the template | 模板variables- the variables | 变量- Throws:
SmsTemplateException- if variables are missing
-
validateAll
Validates all templates. 验证所有模板。- Returns:
- this | 此对象
- Throws:
SmsTemplateException- if any template is invalid
-
forEach
Iterates over all templates. 迭代所有模板。- Parameters:
action- the action | 动作- Returns:
- this | 此对象
-
toRegistry
Creates a registry from this manager. 从此管理器创建注册表。- Returns:
- the registry | 注册表
-
importFrom
Imports templates from a registry. 从注册表导入模板。- Parameters:
registry- the registry | 注册表- Returns:
- this | 此对象
-