mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(framework): migrate AgentRegisterFactory to Kotlin object and preserve agent registration flow
This commit is contained in:
@@ -1,99 +0,0 @@
|
|||||||
package work.slhaf.partner.api.agent.factory;
|
|
||||||
|
|
||||||
import org.reflections.util.ClasspathHelper;
|
|
||||||
import work.slhaf.partner.api.agent.factory.capability.CapabilityAnnotationValidatorFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.capability.CapabilityInjectorFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.capability.CapabilityRegisterFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.component.ComponentAnnotationValidatorFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.component.ComponentInitHookExecuteFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.component.ComponentInjectorFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.component.ComponentRegisterFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.config.ConfigLoaderFactory;
|
|
||||||
import work.slhaf.partner.api.agent.factory.context.AgentRegisterContext;
|
|
||||||
import work.slhaf.partner.api.agent.factory.exception.ExternalModuleLoadFailedException;
|
|
||||||
import work.slhaf.partner.api.agent.factory.exception.ExternalModulePathNotExistException;
|
|
||||||
import work.slhaf.partner.api.agent.runtime.config.AgentConfigLoader;
|
|
||||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.AgentRunningFlow;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <h2>Agent 注册工厂</h2>
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* 具体流程依次按照 {@link AgentRegisterFactory#launch(String)} 方法顺序执行,最终将执行模块列表对应实例交给 {@link AgentConfigLoader} ,传递给 {@link AgentRunningFlow} 针对交互做出调用
|
|
||||||
* <p/>
|
|
||||||
*/
|
|
||||||
public class AgentRegisterFactory {
|
|
||||||
|
|
||||||
private static final List<URL> urls = new ArrayList<>();
|
|
||||||
|
|
||||||
private AgentRegisterFactory() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void launch(String packageName) {
|
|
||||||
urls.addAll(packageNameToURL(packageName));
|
|
||||||
AgentRegisterContext registerContext = new AgentRegisterContext(urls);
|
|
||||||
//流程
|
|
||||||
//0. 加载配置
|
|
||||||
new ConfigLoaderFactory().execute(registerContext);
|
|
||||||
//1. 校验 Component 级别注解是否合规,避免注入到异常位置
|
|
||||||
new ComponentAnnotationValidatorFactory().execute(registerContext);
|
|
||||||
//2. 收集所有的 AgentComponent 实例
|
|
||||||
new ComponentRegisterFactory().execute(registerContext);
|
|
||||||
//3. 对模块与额外组件进行模块依赖注入
|
|
||||||
new ComponentInjectorFactory().execute(registerContext);
|
|
||||||
//4. 加载检查Capability层内容后进行能力层的内容注册
|
|
||||||
new CapabilityAnnotationValidatorFactory().execute(registerContext);
|
|
||||||
//5. 根据 Capability 相关的扫描结果构造 Capability 实例
|
|
||||||
new CapabilityRegisterFactory().execute(registerContext);
|
|
||||||
//6. 将 Capability 实例注入至各个 AgentComponent 中
|
|
||||||
new CapabilityInjectorFactory().execute(registerContext);
|
|
||||||
//7. 执行模块PreHook逻辑
|
|
||||||
new ComponentInitHookExecuteFactory().execute(registerContext);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加可扫描包
|
|
||||||
*
|
|
||||||
* @param packageName 指定的包名
|
|
||||||
*/
|
|
||||||
public static void addScanPackage(String packageName) {
|
|
||||||
urls.addAll(packageNameToURL(packageName));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加外部模块目录
|
|
||||||
*
|
|
||||||
* @param externalPackagePath 指定的外部模块目录路径
|
|
||||||
*/
|
|
||||||
public static void addScanDir(String externalPackagePath) {
|
|
||||||
File file = new File(externalPackagePath);
|
|
||||||
if (!file.exists() || !file.isDirectory()) {
|
|
||||||
throw new ExternalModulePathNotExistException("不存在的外部模块目录: " + externalPackagePath);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
File[] files = file.listFiles();
|
|
||||||
if (files == null || files.length == 0) {
|
|
||||||
throw new ExternalModulePathNotExistException("外部模块目录为空: " + externalPackagePath);
|
|
||||||
}
|
|
||||||
for (File f : files) {
|
|
||||||
if (f.getName().endsWith(".jar")) {
|
|
||||||
urls.add(f.toURI().toURL());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ExternalModuleLoadFailedException("外部模块URL获取失败: " + externalPackagePath, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<URL> packageNameToURL(String packageName) {
|
|
||||||
return ClasspathHelper.forPackage(packageName).stream().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package work.slhaf.partner.api.agent.factory
|
||||||
|
|
||||||
|
import org.reflections.util.ClasspathHelper
|
||||||
|
import work.slhaf.partner.api.agent.factory.capability.CapabilityAnnotationValidatorFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.capability.CapabilityInjectorFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.capability.CapabilityRegisterFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.component.ComponentAnnotationValidatorFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.component.ComponentInitHookExecuteFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.component.ComponentInjectorFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.component.ComponentRegisterFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.config.ConfigLoaderFactory
|
||||||
|
import work.slhaf.partner.api.agent.factory.context.AgentRegisterContext
|
||||||
|
import work.slhaf.partner.api.agent.factory.exception.ExternalModuleLoadFailedException
|
||||||
|
import work.slhaf.partner.api.agent.factory.exception.ExternalModulePathNotExistException
|
||||||
|
import java.io.File
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
object AgentRegisterFactory {
|
||||||
|
private val urls: MutableList<URL> = mutableListOf()
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun launch(packageName: String) {
|
||||||
|
urls.addAll(packageNameToURL(packageName))
|
||||||
|
val registerContext = AgentRegisterContext(urls)
|
||||||
|
// 0. 加载配置
|
||||||
|
ConfigLoaderFactory().execute(registerContext)
|
||||||
|
// 1. 校验 Component 级别注解是否合规,避免注入到异常位置
|
||||||
|
ComponentAnnotationValidatorFactory().execute(registerContext)
|
||||||
|
// 2. 收集所有的 AgentComponent 实例
|
||||||
|
ComponentRegisterFactory().execute(registerContext)
|
||||||
|
// 3. 对模块与额外组件进行模块依赖注入
|
||||||
|
ComponentInjectorFactory().execute(registerContext)
|
||||||
|
// 4. 校验 Capability 注解与方法关系
|
||||||
|
CapabilityAnnotationValidatorFactory().execute(registerContext)
|
||||||
|
// 5. 根据 Capability 相关的扫描结果构造 Capability 实例
|
||||||
|
CapabilityRegisterFactory().execute(registerContext)
|
||||||
|
// 6. 将 Capability 实例注入至各个 AgentComponent 中
|
||||||
|
CapabilityInjectorFactory().execute(registerContext)
|
||||||
|
// 7. 执行模块 Init Hook 逻辑
|
||||||
|
ComponentInitHookExecuteFactory().execute(registerContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun addScanPackage(packageName: String) {
|
||||||
|
urls.addAll(packageNameToURL(packageName))
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun addScanDir(externalPackagePath: String) {
|
||||||
|
val file = File(externalPackagePath)
|
||||||
|
if (!file.exists() || !file.isDirectory) {
|
||||||
|
throw ExternalModulePathNotExistException("不存在的外部模块目录: $externalPackagePath")
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
val files = file.listFiles()
|
||||||
|
?: throw ExternalModulePathNotExistException("外部模块目录为空: $externalPackagePath")
|
||||||
|
if (files.isEmpty()) {
|
||||||
|
throw ExternalModulePathNotExistException("外部模块目录为空: $externalPackagePath")
|
||||||
|
}
|
||||||
|
files.asSequence()
|
||||||
|
.filter { it.name.endsWith(".jar") }
|
||||||
|
.forEach { urls.add(it.toURI().toURL()) }
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw ExternalModuleLoadFailedException("外部模块URL获取失败: $externalPackagePath", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun packageNameToURL(packageName: String): List<URL> {
|
||||||
|
return ClasspathHelper.forPackage(packageName).toList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user