mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
需继续为上层模块构建注册体系以适应完整的加载逻辑。
- 移除了 BaseCoordinateManager 抽象类,而是添加了 @CoordinateManager 注解 - 移除了 CapabilityHolder 抽象类,换成 @CapabilityHolder 注解 - 新增了适应新注册机制的部分类,仍需进一步推进
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package work.slhaf.partner.api;
|
||||
|
||||
public class Agent {
|
||||
public static void run(Class<?> clazz) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package work.slhaf.partner.api;
|
||||
|
||||
import work.slhaf.partner.api.common.entity.AgentRegisterContext;
|
||||
|
||||
public class AgentRegisterFactory {
|
||||
|
||||
private AgentRegisterContext context = new AgentRegisterContext();
|
||||
|
||||
private AgentRegisterFactory(){}
|
||||
|
||||
public static void launch(){
|
||||
//TODO 通过调用module与capability的注册逻辑,完成完整的注册过程,需要考虑hook机制
|
||||
AgentRegisterFactory factory = new AgentRegisterFactory();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package work.slhaf.partner.api.capability;
|
||||
|
||||
|
||||
public class BaseCoordinateManager {
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import org.reflections.util.ClasspathHelper;
|
||||
import org.reflections.util.ConfigurationBuilder;
|
||||
import work.slhaf.partner.api.capability.annotation.*;
|
||||
import work.slhaf.partner.api.capability.exception.*;
|
||||
import work.slhaf.partner.api.capability.module.CapabilityHolder;
|
||||
import work.slhaf.partner.api.capability.util.CapabilityUtil;
|
||||
import work.slhaf.partner.api.common.util.AgentUtil;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.net.URL;
|
||||
@@ -15,13 +14,11 @@ import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static work.slhaf.partner.api.capability.util.CapabilityUtil.methodSignature;
|
||||
import static work.slhaf.partner.api.common.util.AgentUtil.methodSignature;
|
||||
|
||||
|
||||
public final class CapabilityRegisterFactory {
|
||||
|
||||
public static volatile CapabilityRegisterFactory capabilityRegisterFactory;
|
||||
|
||||
private Reflections reflections;
|
||||
private final HashMap<String, Function<Object[], Object>> methodsRouterTable = new HashMap<>();
|
||||
private final HashMap<String, Function<Object[], Object>> coordinatedMethodsRouterTable = new HashMap<>();
|
||||
@@ -33,26 +30,20 @@ public final class CapabilityRegisterFactory {
|
||||
private CapabilityRegisterFactory() {
|
||||
}
|
||||
|
||||
public static CapabilityRegisterFactory getInstance() {
|
||||
if (capabilityRegisterFactory == null) {
|
||||
synchronized (CapabilityRegisterFactory.class) {
|
||||
if (capabilityRegisterFactory == null) {
|
||||
capabilityRegisterFactory = new CapabilityRegisterFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
return capabilityRegisterFactory;
|
||||
}
|
||||
|
||||
|
||||
//TODO 需决定是否分离检查与路由表生成、注入逻辑,如果分离,可进一步添加hook点,但目前似乎并非必要
|
||||
public void registerCapabilities(String scannerPath) {
|
||||
setBasicVariable(scannerPath);
|
||||
//检查可注册能力是否正常
|
||||
statusCheck();
|
||||
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
||||
generateRouterTable();
|
||||
//通过动态代理注入能力
|
||||
injectCapability();
|
||||
}
|
||||
|
||||
private void generateRouterTable() {
|
||||
generateMethodsRouterTable();
|
||||
generateCoordinatedMethodsRouterTable();
|
||||
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
||||
injectCapability();
|
||||
}
|
||||
|
||||
private void generateCoordinatedMethodsRouterTable() {
|
||||
@@ -82,9 +73,9 @@ public final class CapabilityRegisterFactory {
|
||||
|
||||
private HashMap<String, Object> getCognationManagerInstances() throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
for (Class<? extends BaseCoordinateManager> c : reflections.getSubTypesOf(BaseCoordinateManager.class)) {
|
||||
Constructor<? extends BaseCoordinateManager> constructor = c.getDeclaredConstructor();
|
||||
BaseCoordinateManager instance = constructor.newInstance();
|
||||
for (Class<?> c : reflections.getTypesAnnotatedWith(CoordinateManager.class)) {
|
||||
Constructor<?> constructor = c.getDeclaredConstructor();
|
||||
Object instance = constructor.newInstance();
|
||||
|
||||
Arrays.stream(c.getMethods())
|
||||
.filter(method -> method.isAnnotationPresent(Coordinated.class))
|
||||
@@ -186,8 +177,8 @@ public final class CapabilityRegisterFactory {
|
||||
|
||||
private void checkInjectCapability() {
|
||||
reflections.getFieldsAnnotatedWith(InjectCapability.class).forEach(field -> {
|
||||
if (!CapabilityHolder.class.isAssignableFrom(field.getDeclaringClass())) {
|
||||
throw new UnMatchedCapabilityException("InjectCapability 注解只能用于CapabilityHolder的子类");
|
||||
if (!field.getDeclaringClass().isAssignableFrom(CapabilityHolder.class)) {
|
||||
throw new UnMatchedCapabilityException("InjectCapability 注解只能用于 CapabilityHolder 注解所在类");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -223,7 +214,7 @@ public final class CapabilityRegisterFactory {
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
if (!methodsToCoordinated.isEmpty()) {
|
||||
Set<Class<? extends BaseCoordinateManager>> subTypesOfAbsCM = reflections.getSubTypesOf(BaseCoordinateManager.class);
|
||||
Set<Class<?>> subTypesOfAbsCM = reflections.getTypesAnnotatedWith(CoordinateManager.class);
|
||||
Set<String> methodsCoordinated = getMethodsCoordinated(subTypesOfAbsCM);
|
||||
if (!methodsCoordinated.equals(methodsToCoordinated)) {
|
||||
// 找出缺少的协调方法
|
||||
@@ -245,9 +236,9 @@ public final class CapabilityRegisterFactory {
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getMethodsCoordinated(Set<Class<? extends BaseCoordinateManager>> subTypesOfAbsCM) {
|
||||
private Set<String> getMethodsCoordinated(Set<Class<?>> classes) {
|
||||
Set<String> methodsCoordinated = new HashSet<>();
|
||||
for (Class<? extends BaseCoordinateManager> cm : subTypesOfAbsCM) {
|
||||
for (Class<?> cm : classes) {
|
||||
Method[] methods = cm.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (method.isAnnotationPresent(Coordinated.class)) {
|
||||
@@ -280,11 +271,11 @@ public final class CapabilityRegisterFactory {
|
||||
private LackRecord checkMethodsMatched(List<Method> methodsWithAnnotation, List<Method> capabilityMethods) {
|
||||
Set<String> collectedMethodsWithAnnotation = methodsWithAnnotation.stream()
|
||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(CapabilityUtil::methodSignature)
|
||||
.map(AgentUtil::methodSignature)
|
||||
.collect(Collectors.toSet());
|
||||
Set<String> collectedCapabilityMethods = capabilityMethods.stream()
|
||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||
.map(CapabilityUtil::methodSignature)
|
||||
.map(AgentUtil::methodSignature)
|
||||
.collect(Collectors.toSet());
|
||||
return checkMethodsMatched(collectedMethodsWithAnnotation, collectedCapabilityMethods);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package work.slhaf.partner.api.capability.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface CapabilityHolder {
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package work.slhaf.partner.api.capability.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CoordinateManager {
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package work.slhaf.partner.api.capability.module;
|
||||
|
||||
import work.slhaf.partner.api.capability.CapabilityRegisterFactory;
|
||||
|
||||
public abstract class CapabilityHolder {
|
||||
protected CapabilityHolder() {
|
||||
CapabilityRegisterFactory.getInstance().registerModule(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package work.slhaf.partner.api.common.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
@Data
|
||||
public class AgentRegisterContext {
|
||||
//TODO 抽取出必要的注册工厂共用的上下文
|
||||
private Reflections reflections;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package work.slhaf.partner.api.capability.util;
|
||||
package work.slhaf.partner.api.common.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public final class CapabilityUtil {
|
||||
public final class AgentUtil {
|
||||
public static String methodSignature(Method method) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("(");
|
||||
@@ -0,0 +1,4 @@
|
||||
package work.slhaf.partner.api.module;
|
||||
|
||||
public class ModuleRegisterFactory {
|
||||
}
|
||||
Reference in New Issue
Block a user