mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
核心服务注册机制完成,Partner待适配
- 将`methodSignature`抽取至工具类中 - 新增了数个异常类,适配工厂注册时的异常处理 - 完善了核心服务的注解检测、函数路由表生成以及代理动态注入实现。
This commit is contained in:
20
.xcodemap/config/xcodemap-class-filter.yaml
Normal file
20
.xcodemap/config/xcodemap-class-filter.yaml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
autoDetectedPackages:
|
||||||
|
- work.slhaf
|
||||||
|
enableAutoDetect: true
|
||||||
|
entryDisplayConfig: null
|
||||||
|
funcDisplayConfig: null
|
||||||
|
ignoreSameClassCall: null
|
||||||
|
ignoreSamePackageCall: null
|
||||||
|
includedPackagePrefixes: null
|
||||||
|
includedParentClasses: null
|
||||||
|
maxColSize: null
|
||||||
|
maxNumFirst: null
|
||||||
|
maxNumFirstImportant: null
|
||||||
|
maxNumHash: null
|
||||||
|
maxNumHashImportant: null
|
||||||
|
maxObjectDepth: null
|
||||||
|
maxStrSize: null
|
||||||
|
name: xcodemap-filter
|
||||||
|
recordMode: null
|
||||||
|
sourceDisplayConfig: null
|
||||||
|
startOnDebug: null
|
||||||
12
Partner-Capability-Demo/src/main/java/work/slhaf/Main.java
Normal file
12
Partner-Capability-Demo/src/main/java/work/slhaf/Main.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package work.slhaf;
|
||||||
|
|
||||||
|
import work.slhaf.demo.TestModule;
|
||||||
|
import work.slhaf.demo.capability.CapabilityRegisterFactory;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) throws ClassNotFoundException {
|
||||||
|
TestModule testModule = new TestModule();
|
||||||
|
CapabilityRegisterFactory.getInstance().registerCapabilities(Main.class.getPackage().getName());
|
||||||
|
testModule.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
package work.slhaf.demo;
|
package work.slhaf.demo;
|
||||||
|
|
||||||
import work.slhaf.demo.capability.BaseCognationManager;
|
import work.slhaf.demo.capability.BaseCognationManager;
|
||||||
import work.slhaf.demo.capability.interfaces.Coordinated;
|
import work.slhaf.demo.capability.annotation.Coordinated;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MyCognationManager extends BaseCognationManager {
|
public class MyCognationManager extends BaseCognationManager {
|
||||||
|
|
||||||
@Coordinated(capability = "memory")
|
@Coordinated(capability = "memory")
|
||||||
public List<String> selectMemory(String path) {
|
public List<String> selectMemory(String path) {
|
||||||
return null;
|
return List.of("1", "2", path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package work.slhaf.demo;
|
||||||
|
|
||||||
|
import work.slhaf.demo.ability.CacheCapability;
|
||||||
|
import work.slhaf.demo.ability.MemoryCapability;
|
||||||
|
import work.slhaf.demo.capability.annotation.InjectCapability;
|
||||||
|
import work.slhaf.demo.capability.module.CapabilityHolder;
|
||||||
|
|
||||||
|
public class TestModule extends CapabilityHolder {
|
||||||
|
@InjectCapability
|
||||||
|
private MemoryCapability capability;
|
||||||
|
|
||||||
|
public void execute(){
|
||||||
|
System.out.println("111");
|
||||||
|
System.out.println(capability.selectMemory("zjw"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package work.slhaf.demo.capability.ability;
|
package work.slhaf.demo.ability;
|
||||||
|
|
||||||
|
|
||||||
import work.slhaf.demo.capability.interfaces.Capability;
|
import work.slhaf.demo.capability.annotation.Capability;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -13,5 +13,5 @@ public interface CacheCapability {
|
|||||||
ConcurrentHashMap<LocalDateTime, String> getUserDialogMap(String userId);
|
ConcurrentHashMap<LocalDateTime, String> getUserDialogMap(String userId);
|
||||||
void updateDialogMap(LocalDateTime dateTime, String newDialogCache);
|
void updateDialogMap(LocalDateTime dateTime, String newDialogCache);
|
||||||
String getDialogMapStr();
|
String getDialogMapStr();
|
||||||
String getUserDialogMapStr(String userId);
|
String getUserDialogMapStr(String userId,int id);
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package work.slhaf.demo.capability.ability;
|
package work.slhaf.demo.ability;
|
||||||
|
|
||||||
import work.slhaf.demo.capability.interfaces.Capability;
|
import work.slhaf.demo.capability.annotation.Capability;
|
||||||
import work.slhaf.demo.capability.interfaces.ToCoordinated;
|
import work.slhaf.demo.capability.annotation.ToCoordinated;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package work.slhaf.demo.capability.ability;
|
package work.slhaf.demo.ability;
|
||||||
|
|
||||||
import work.slhaf.demo.capability.interfaces.Capability;
|
import work.slhaf.demo.capability.annotation.Capability;
|
||||||
|
|
||||||
@Capability(value = "perceive")
|
@Capability(value = "perceive")
|
||||||
public interface PerceiveCapability {
|
public interface PerceiveCapability {
|
||||||
@@ -1,24 +1,33 @@
|
|||||||
package work.slhaf.demo.capability;
|
package work.slhaf.demo.capability;
|
||||||
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.reflections.scanners.Scanners;
|
import org.reflections.scanners.Scanners;
|
||||||
import org.reflections.util.ClasspathHelper;
|
import org.reflections.util.ClasspathHelper;
|
||||||
import org.reflections.util.ConfigurationBuilder;
|
import org.reflections.util.ConfigurationBuilder;
|
||||||
import work.slhaf.demo.capability.exception.*;
|
import work.slhaf.demo.capability.exception.*;
|
||||||
import work.slhaf.demo.capability.interfaces.*;
|
import work.slhaf.demo.capability.annotation.*;
|
||||||
|
import work.slhaf.demo.capability.module.CapabilityHolder;
|
||||||
|
import work.slhaf.demo.capability.util.CapabilityUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.*;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class CapabilityRegisterFactory {
|
import static work.slhaf.demo.capability.util.CapabilityUtil.methodSignature;
|
||||||
|
|
||||||
|
public final class CapabilityRegisterFactory {
|
||||||
|
|
||||||
public static volatile CapabilityRegisterFactory capabilityRegisterFactory;
|
public static volatile CapabilityRegisterFactory capabilityRegisterFactory;
|
||||||
|
|
||||||
@Setter
|
|
||||||
private Reflections reflections;
|
private Reflections reflections;
|
||||||
|
private final HashMap<String, Function<Object[], Object>> methodsRouterTable = new HashMap<>();
|
||||||
|
private final HashMap<String, Function<Object[], Object>> coordinatedMethodsRouterTable = new HashMap<>();
|
||||||
|
private final HashMap<Class<?>, Object> capabilityCoreInstances = new HashMap<>();
|
||||||
|
private final HashMap<Class<?>, Object> capabilityHolderInstances = new HashMap<>();
|
||||||
|
private Set<Class<?>> cores;
|
||||||
|
private Set<Class<?>> capabilities;
|
||||||
|
|
||||||
private CapabilityRegisterFactory() {
|
private CapabilityRegisterFactory() {
|
||||||
}
|
}
|
||||||
@@ -28,39 +37,180 @@ public class CapabilityRegisterFactory {
|
|||||||
synchronized (CapabilityRegisterFactory.class) {
|
synchronized (CapabilityRegisterFactory.class) {
|
||||||
if (capabilityRegisterFactory == null) {
|
if (capabilityRegisterFactory == null) {
|
||||||
capabilityRegisterFactory = new CapabilityRegisterFactory();
|
capabilityRegisterFactory = new CapabilityRegisterFactory();
|
||||||
capabilityRegisterFactory.setReflections(getReflections());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return capabilityRegisterFactory;
|
return capabilityRegisterFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Reflections getReflections() {
|
|
||||||
|
public void registerCapabilities(String scannerPath) {
|
||||||
|
setBasicVariable(scannerPath);
|
||||||
|
//检查可注册能力是否正常
|
||||||
|
statusCheck();
|
||||||
|
generateMethodsRouterTable();
|
||||||
|
generateCoordinatedMethodsRouterTable();
|
||||||
|
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
||||||
|
injectCapability();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateCoordinatedMethodsRouterTable() {
|
||||||
|
Set<Method> methodsAnnotatedWith = reflections.getMethodsAnnotatedWith(Coordinated.class);
|
||||||
|
if (methodsAnnotatedWith.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
//获取所有CM实例
|
||||||
|
HashMap<String, Object> cognationManagerInstances = getCognationManagerInstances();
|
||||||
|
methodsAnnotatedWith.forEach(method -> {
|
||||||
|
String key = method.getAnnotation(Coordinated.class).capability() + "." + methodSignature(method);
|
||||||
|
Function<Object[], Object> function = args -> {
|
||||||
|
try {
|
||||||
|
return method.invoke(cognationManagerInstances.get(key), args);
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
coordinatedMethodsRouterTable.put(key, function);
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new FactoryExecuteFailedException("创建协调方法路由表出错", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private HashMap<String, Object> getCognationManagerInstances() throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||||
|
HashMap<String, Object> map = new HashMap<>();
|
||||||
|
for (Class<? extends BaseCognationManager> c : reflections.getSubTypesOf(BaseCognationManager.class)) {
|
||||||
|
Constructor<? extends BaseCognationManager> constructor = c.getDeclaredConstructor();
|
||||||
|
BaseCognationManager instance = constructor.newInstance();
|
||||||
|
|
||||||
|
Arrays.stream(c.getMethods())
|
||||||
|
.filter(method -> method.isAnnotationPresent(Coordinated.class))
|
||||||
|
.forEach(method -> {
|
||||||
|
String key = method.getAnnotation(Coordinated.class).capability() + "." + methodSignature(method);
|
||||||
|
map.put(key, instance);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setBasicVariable(String scannerPath) {
|
||||||
|
setReflections(scannerPath);
|
||||||
|
setAnnotatedClasses();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAnnotatedClasses() {
|
||||||
|
cores = reflections.getTypesAnnotatedWith(CapabilityCore.class);
|
||||||
|
capabilities = reflections.getTypesAnnotatedWith(Capability.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setReflections(String scannerPath) {
|
||||||
//后续可替换为根据传入的启动类获取路径
|
//后续可替换为根据传入的启动类获取路径
|
||||||
Collection<URL> urls = ClasspathHelper.forJavaClassPath();
|
Collection<URL> urls = ClasspathHelper.forPackage(scannerPath);
|
||||||
return new Reflections(
|
reflections = new Reflections(
|
||||||
new ConfigurationBuilder()
|
new ConfigurationBuilder()
|
||||||
.setUrls(urls)
|
.setUrls(urls)
|
||||||
.setScanners(Scanners.TypesAnnotated, Scanners.MethodsAnnotated, Scanners.SubTypes)
|
.setScanners(
|
||||||
|
Scanners.TypesAnnotated,
|
||||||
|
Scanners.MethodsAnnotated,
|
||||||
|
Scanners.SubTypes,
|
||||||
|
Scanners.FieldsAnnotated
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerCapabilities() {
|
private void generateMethodsRouterTable() {
|
||||||
//检查可注册能力是否正常
|
//扫描`@Capability`与`@CapabilityMethod`注解的类与方法
|
||||||
statusCheck();
|
//将`capabilityValue.methodSignature`作为key,函数对象为通过反射拿到的core实例对应的方法
|
||||||
//扫描现有Capability, value为键,返回函数路由表, 函数路由表内部通过反射调用对应core的方法
|
cores.forEach(core -> Arrays.stream(core.getMethods())
|
||||||
//扫描时也需要排除掉
|
.filter(method -> method.isAnnotationPresent(CapabilityMethod.class))
|
||||||
|
.forEach(method -> {
|
||||||
|
Function<Object[], Object> function = args -> {
|
||||||
|
try {
|
||||||
|
return method.invoke(capabilityCoreInstances.get(core), args);
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
String key = core.getAnnotation(CapabilityCore.class).value() + "." + methodSignature(method);
|
||||||
|
if (methodsRouterTable.containsKey(key)) {
|
||||||
|
throw new DuplicateMethodException("重复注册能力方法: " + core.getPackage().getName() + "." + core.getSimpleName() + "#" + method.getName());
|
||||||
|
}
|
||||||
|
methodsRouterTable.put(key, function);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void injectCapability() {
|
||||||
|
//获取现有的`@InjectCapability`注解所在字段,并获取对应的类,通过动态代理注入对象
|
||||||
|
Set<Field> fields = reflections.getFieldsAnnotatedWith(InjectCapability.class);
|
||||||
|
//在动态代理内部,通过函数路由表调用对应的方法
|
||||||
|
createProxy(fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createProxy(Set<Field> fields) {
|
||||||
|
try {
|
||||||
|
for (Field field : fields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Class<?> fieldType = field.getType();
|
||||||
|
Object instance = Proxy.newProxyInstance(
|
||||||
|
fieldType.getClassLoader(),
|
||||||
|
new Class[]{fieldType},
|
||||||
|
(proxy, method, objects) -> {
|
||||||
|
if (method.isAnnotationPresent(ToCoordinated.class)) {
|
||||||
|
String key = method.getDeclaringClass().getAnnotation(Capability.class).value() + "." + methodSignature(method);
|
||||||
|
return coordinatedMethodsRouterTable.get(key).apply(objects);
|
||||||
|
}
|
||||||
|
String key = fieldType.getAnnotation(Capability.class).value() + "." + methodSignature(method);
|
||||||
|
return methodsRouterTable.get(key).apply(objects);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
field.set(capabilityHolderInstances.get(field.getDeclaringClass()), instance);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ProxySetFailedException("代理设置失败", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void statusCheck() {
|
private void statusCheck() {
|
||||||
Set<Class<?>> cores = reflections.getTypesAnnotatedWith(CapabilityCore.class);
|
capabilityHolderCheck();
|
||||||
Set<Class<?>> capabilities = reflections.getTypesAnnotatedWith(Capability.class);
|
checkCountAndCapabilities();
|
||||||
checkCountAndCapabilities(cores, capabilities);
|
checkCapabilityMethods();
|
||||||
checkCapabilityMethods(cores, capabilities);
|
checkCoordinatedMethods();
|
||||||
checkCoordinatedMethods(capabilities);
|
checkInjectCapability();
|
||||||
|
//检查完毕,设置core的实例类
|
||||||
|
setCapabilityCoreInstances();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkCoordinatedMethods(Set<Class<?>> capabilities) {
|
private void checkInjectCapability() {
|
||||||
|
reflections.getFieldsAnnotatedWith(InjectCapability.class).forEach(field -> {
|
||||||
|
if (!CapabilityHolder.class.isAssignableFrom(field.getDeclaringClass())) {
|
||||||
|
throw new UnMatchedCapabilityException("InjectCapability 注解只能用于CapabilityHolder的子类");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void capabilityHolderCheck() {
|
||||||
|
if (capabilityHolderInstances.isEmpty()) {
|
||||||
|
throw new EmptyCapabilityHolderException("Capability 持有者实例为空");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setCapabilityCoreInstances() {
|
||||||
|
try {
|
||||||
|
for (Class<?> core : cores) {
|
||||||
|
Constructor<?> constructor = core.getDeclaredConstructor();
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
capabilityCoreInstances.put(core, constructor.newInstance());
|
||||||
|
}
|
||||||
|
} catch (InvocationTargetException | NoSuchMethodException | InstantiationException |
|
||||||
|
IllegalAccessException e) {
|
||||||
|
throw new CoreInstancesCreateFailedException("core实例创建失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkCoordinatedMethods() {
|
||||||
//检查各个capability中是否含有ToCoordinated注解
|
//检查各个capability中是否含有ToCoordinated注解
|
||||||
//如果含有,则需要查找AbstractCognationManager的子类,看这里是否有对应的Coordinated注解所在方法
|
//如果含有,则需要查找AbstractCognationManager的子类,看这里是否有对应的Coordinated注解所在方法
|
||||||
Set<String> methodsToCoordinated = capabilities.stream()
|
Set<String> methodsToCoordinated = capabilities.stream()
|
||||||
@@ -108,7 +258,7 @@ public class CapabilityRegisterFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void checkCapabilityMethods(Set<Class<?>> cores, Set<Class<?>> capabilities) {
|
private void checkCapabilityMethods() {
|
||||||
HashMap<String, List<Method>> capabilitiesMethods = getCapabilityMethods(capabilities);
|
HashMap<String, List<Method>> capabilitiesMethods = getCapabilityMethods(capabilities);
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (Class<?> core : cores) {
|
for (Class<?> core : cores) {
|
||||||
@@ -129,11 +279,11 @@ public class CapabilityRegisterFactory {
|
|||||||
private LackRecord checkMethodsMatched(List<Method> methodsWithAnnotation, List<Method> capabilityMethods) {
|
private LackRecord checkMethodsMatched(List<Method> methodsWithAnnotation, List<Method> capabilityMethods) {
|
||||||
Set<String> collectedMethodsWithAnnotation = methodsWithAnnotation.stream()
|
Set<String> collectedMethodsWithAnnotation = methodsWithAnnotation.stream()
|
||||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||||
.map(this::methodSignature)
|
.map(CapabilityUtil::methodSignature)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
Set<String> collectedCapabilityMethods = capabilityMethods.stream()
|
Set<String> collectedCapabilityMethods = capabilityMethods.stream()
|
||||||
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
.filter(method -> !method.isAnnotationPresent(ToCoordinated.class))
|
||||||
.map(this::methodSignature)
|
.map(CapabilityUtil::methodSignature)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
return checkMethodsMatched(collectedMethodsWithAnnotation, collectedCapabilityMethods);
|
return checkMethodsMatched(collectedMethodsWithAnnotation, collectedCapabilityMethods);
|
||||||
}
|
}
|
||||||
@@ -157,19 +307,6 @@ public class CapabilityRegisterFactory {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String methodSignature(Method method) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("(");
|
|
||||||
sb.append(method.getReturnType().getName()).append(" ");
|
|
||||||
sb.append(method.getName()).append("(");
|
|
||||||
Class<?>[] paramTypes = method.getParameterTypes();
|
|
||||||
for (int i = 0; i < paramTypes.length; i++) {
|
|
||||||
sb.append(paramTypes[i].getName());
|
|
||||||
if (i < paramTypes.length - 1) sb.append(",");
|
|
||||||
}
|
|
||||||
sb.append(")").append(")");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private HashMap<String, List<Method>> getCapabilityMethods(Set<Class<?>> capabilities) {
|
private HashMap<String, List<Method>> getCapabilityMethods(Set<Class<?>> capabilities) {
|
||||||
HashMap<String, List<Method>> capabilityMethods = new HashMap<>();
|
HashMap<String, List<Method>> capabilityMethods = new HashMap<>();
|
||||||
@@ -179,7 +316,7 @@ public class CapabilityRegisterFactory {
|
|||||||
return capabilityMethods;
|
return capabilityMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkCountAndCapabilities(Set<Class<?>> cores, Set<Class<?>> capabilities) {
|
private void checkCountAndCapabilities() {
|
||||||
if (cores.size() != capabilities.size()) {
|
if (cores.size() != capabilities.size()) {
|
||||||
throw new UnMatchedCapabilityException("Capability 注册异常: 已存在的CapabilityCore与Capability数量不匹配!");
|
throw new UnMatchedCapabilityException("Capability 注册异常: 已存在的CapabilityCore与Capability数量不匹配!");
|
||||||
}
|
}
|
||||||
@@ -213,6 +350,10 @@ public class CapabilityRegisterFactory {
|
|||||||
return coresValues.equals(capabilitiesValues);
|
return coresValues.equals(capabilitiesValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void registerModule(CapabilityHolder capabilityHolder) {
|
||||||
|
capabilityHolderInstances.put(capabilityHolder.getClass(), capabilityHolder);
|
||||||
|
}
|
||||||
|
|
||||||
record LackRecord(List<String> coreLack, List<String> capLack) {
|
record LackRecord(List<String> coreLack, List<String> capLack) {
|
||||||
public boolean hasNotEmptyRecord() {
|
public boolean hasNotEmptyRecord() {
|
||||||
return !coreLack.isEmpty() || !capLack.isEmpty();
|
return !coreLack.isEmpty() || !capLack.isEmpty();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.demo.capability.interfaces;
|
package work.slhaf.demo.capability.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.demo.capability.interfaces;
|
package work.slhaf.demo.capability.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.demo.capability.interfaces;
|
package work.slhaf.demo.capability.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.demo.capability.interfaces;
|
package work.slhaf.demo.capability.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.demo.capability.interfaces;
|
package work.slhaf.demo.capability.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package work.slhaf.demo.capability.interfaces;
|
package work.slhaf.demo.capability.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
@@ -4,4 +4,8 @@ public class CapabilityCheckFailedException extends RuntimeException {
|
|||||||
public CapabilityCheckFailedException(String message) {
|
public CapabilityCheckFailedException(String message) {
|
||||||
super("Capability注册失败: " + message);
|
super("Capability注册失败: " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CapabilityCheckFailedException(String message, Throwable cause) {
|
||||||
|
super("Capability注册失败: " + message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package work.slhaf.demo.capability.exception;
|
||||||
|
|
||||||
|
public class CoreInstancesCreateFailedException extends FactoryExecuteFailedException{
|
||||||
|
public CoreInstancesCreateFailedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CoreInstancesCreateFailedException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,4 +4,8 @@ public class DuplicateCapabilityException extends CapabilityCheckFailedException
|
|||||||
public DuplicateCapabilityException(String message) {
|
public DuplicateCapabilityException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DuplicateCapabilityException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package work.slhaf.demo.capability.exception;
|
||||||
|
|
||||||
|
public class DuplicateMethodException extends CapabilityCheckFailedException{
|
||||||
|
public DuplicateMethodException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DuplicateMethodException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package work.slhaf.demo.capability.exception;
|
||||||
|
|
||||||
|
public class EmptyCapabilityHolderException extends CapabilityCheckFailedException{
|
||||||
|
public EmptyCapabilityHolderException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmptyCapabilityHolderException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package work.slhaf.demo.capability.exception;
|
||||||
|
|
||||||
|
public class FactoryExecuteFailedException extends RuntimeException {
|
||||||
|
public FactoryExecuteFailedException(String message) {
|
||||||
|
super("CapabilityRegisterFactory 执行失败: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FactoryExecuteFailedException(String message, Throwable cause) {
|
||||||
|
super("CapabilityRegisterFactory 执行失败: " + message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package work.slhaf.demo.capability.exception;
|
||||||
|
|
||||||
|
public class ProxySetFailedException extends FactoryExecuteFailedException{
|
||||||
|
public ProxySetFailedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProxySetFailedException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,4 +4,8 @@ public class UnMatchedCapabilityException extends CapabilityCheckFailedException
|
|||||||
public UnMatchedCapabilityException(String message) {
|
public UnMatchedCapabilityException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UnMatchedCapabilityException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,8 @@ public class UnMatchedCapabilityMethodException extends CapabilityCheckFailedExc
|
|||||||
public UnMatchedCapabilityMethodException(String message) {
|
public UnMatchedCapabilityMethodException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UnMatchedCapabilityMethodException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,8 @@ public class UnMatchedCoordinatedMethodException extends CapabilityCheckFailedEx
|
|||||||
public UnMatchedCoordinatedMethodException(String message) {
|
public UnMatchedCoordinatedMethodException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UnMatchedCoordinatedMethodException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package work.slhaf.demo.capability.module;
|
||||||
|
|
||||||
|
import work.slhaf.demo.capability.CapabilityRegisterFactory;
|
||||||
|
|
||||||
|
public abstract class CapabilityHolder {
|
||||||
|
protected CapabilityHolder(){
|
||||||
|
CapabilityRegisterFactory.getInstance().registerModule(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package work.slhaf.demo.capability.util;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public final class CapabilityUtil {
|
||||||
|
public static String methodSignature(Method method) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("(");
|
||||||
|
sb.append(method.getReturnType().getName()).append(" ");
|
||||||
|
sb.append(method.getName()).append("(");
|
||||||
|
Class<?>[] paramTypes = method.getParameterTypes();
|
||||||
|
for (int i = 0; i < paramTypes.length; i++) {
|
||||||
|
sb.append(paramTypes[i].getName());
|
||||||
|
if (i < paramTypes.length - 1) sb.append(",");
|
||||||
|
}
|
||||||
|
sb.append(")").append(")");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package work.slhaf.demo.core;
|
package work.slhaf.demo.core;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.demo.capability.interfaces.CapabilityCore;
|
import work.slhaf.demo.capability.annotation.CapabilityCore;
|
||||||
import work.slhaf.demo.capability.interfaces.CapabilityMethod;
|
import work.slhaf.demo.capability.annotation.CapabilityMethod;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -14,14 +14,11 @@ public class CacheCore {
|
|||||||
|
|
||||||
public static volatile CacheCore cacheCore;
|
public static volatile CacheCore cacheCore;
|
||||||
|
|
||||||
|
private CacheCore() {
|
||||||
|
cacheCore = this;
|
||||||
|
}
|
||||||
|
|
||||||
public static CacheCore getInstance() {
|
public static CacheCore getInstance() {
|
||||||
if (cacheCore == null) {
|
|
||||||
synchronized (CacheCore.class) {
|
|
||||||
if (cacheCore == null) {
|
|
||||||
cacheCore = new CacheCore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cacheCore;
|
return cacheCore;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,9 +46,9 @@ public class CacheCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@CapabilityMethod
|
@CapabilityMethod
|
||||||
public String getUserDialogMapStr(String userId) {
|
public String getUserDialogMapStr(String userId,int id) {
|
||||||
log.info("cache: getUserDialogMapStr");
|
log.info("cache: getUserDialogMapStr");
|
||||||
return "";
|
return userId+id;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package work.slhaf.demo.core;
|
package work.slhaf.demo.core;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.demo.capability.interfaces.CapabilityCore;
|
import work.slhaf.demo.capability.annotation.CapabilityCore;
|
||||||
import work.slhaf.demo.capability.interfaces.CapabilityMethod;
|
import work.slhaf.demo.capability.annotation.CapabilityMethod;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -13,14 +13,11 @@ public class MemoryCore {
|
|||||||
|
|
||||||
public static volatile MemoryCore memoryCore;
|
public static volatile MemoryCore memoryCore;
|
||||||
|
|
||||||
|
private MemoryCore() {
|
||||||
|
memoryCore = this;
|
||||||
|
}
|
||||||
|
|
||||||
public static MemoryCore getInstance() {
|
public static MemoryCore getInstance() {
|
||||||
if (memoryCore == null){
|
|
||||||
synchronized (MemoryCore.class){
|
|
||||||
if (memoryCore == null){
|
|
||||||
memoryCore = new MemoryCore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return memoryCore;
|
return memoryCore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package work.slhaf.demo.core;
|
package work.slhaf.demo.core;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import work.slhaf.demo.capability.interfaces.CapabilityCore;
|
import work.slhaf.demo.capability.annotation.CapabilityCore;
|
||||||
import work.slhaf.demo.capability.interfaces.CapabilityMethod;
|
import work.slhaf.demo.capability.annotation.CapabilityMethod;
|
||||||
|
|
||||||
@CapabilityCore(value = "perceive")
|
@CapabilityCore(value = "perceive")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -10,14 +10,11 @@ public class PerceiveCore {
|
|||||||
|
|
||||||
public static volatile PerceiveCore perceiveCore;
|
public static volatile PerceiveCore perceiveCore;
|
||||||
|
|
||||||
|
private PerceiveCore() {
|
||||||
|
perceiveCore = this;
|
||||||
|
}
|
||||||
|
|
||||||
public static PerceiveCore getInstance() {
|
public static PerceiveCore getInstance() {
|
||||||
if (perceiveCore == null){
|
|
||||||
synchronized (PerceiveCore.class){
|
|
||||||
if (perceiveCore == null){
|
|
||||||
perceiveCore = new PerceiveCore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return perceiveCore;
|
return perceiveCore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import work.slhaf.demo.ability.CacheCapability;
|
||||||
|
import work.slhaf.demo.capability.annotation.InjectCapability;
|
||||||
|
|
||||||
|
public class FunctionRouterTest {
|
||||||
|
|
||||||
|
@InjectCapability
|
||||||
|
private CacheCapability cache;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
cache.getUserDialogMapStr("123",111);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user