Configure plug-in

This commit is contained in:
lizhi 2021-11-05 13:57:59 +08:00
parent 3c6b30e044
commit 80e9ee5624

View File

@ -3,12 +3,15 @@ package org.bdware.server.action;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.Console;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -23,6 +26,7 @@ public class ActionExecutor<T, U> {
public long permission; public long permission;
Map<String, AtomicInteger> staticData; Map<String, AtomicInteger> staticData;
private Map<String, Pair<Method, Object>> handlers; private Map<String, Pair<Method, Object>> handlers;
private Object Constructor;
public ActionExecutor(ExecutorService executor, Object... classes) { public ActionExecutor(ExecutorService executor, Object... classes) {
this.executor = executor; this.executor = executor;
@ -57,39 +61,7 @@ public class ActionExecutor<T, U> {
handlers = new HashMap<>(); handlers = new HashMap<>();
if (objects.length > 0) { if (objects.length > 0) {
for (Object obj : objects) { for (Object obj : objects) {
if (obj == null) continue; appendHandler(obj);
Method[] methods = obj.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getAnnotation(Action.class) != null) {
if (method.getAnnotation(Action.class).alias().length == 0) {
handlers.put(method.getName(), new Pair<>(method, obj));
if (!method.getReturnType().equals(Void.TYPE)
|| method.getParameterCount() != 2) {
LOGGER.error(
"action ret is not void:"
+ obj.getClass().getCanonicalName()
+ "-->"
+ method.getName());
System.exit(0);
}
} else {
for (String a : method.getAnnotation(Action.class).alias()) {
handlers.put(a, new Pair<>(method, obj));
if (!method.getReturnType().equals(Void.TYPE)
|| method.getParameterCount() != 2) {
LOGGER.error(
"action ret is not void:"
+ obj.getClass().getCanonicalName()
+ "-->"
+ method.getName());
System.exit(0);
}
}
}
}
}
} }
} }
} }
@ -102,6 +74,46 @@ public class ActionExecutor<T, U> {
return handlers; return handlers;
} }
public void appendHandler(Object obj) {
try {
if (obj == null) return;
Method[] methods = obj.getClass().getDeclaredMethods();
System.out.println("zzz2"+obj.getClass());
for (Method method : methods) {
if (method.getAnnotation(Action.class) != null) {
if (method.getAnnotation(Action.class).alias().length == 0) {
handlers.put(method.getName(), new Pair<>(method, obj));
if (!method.getReturnType().equals(Void.TYPE)
|| method.getParameterCount() != 2) {
LOGGER.error(
"action ret is not void:"
+ obj.getClass().getCanonicalName()
+ "-->"
+ method.getName());
System.exit(0);
}
} else {
for (String a : method.getAnnotation(Action.class).alias()) {
handlers.put(a, new Pair<>(method, obj));
if (!method.getReturnType().equals(Void.TYPE)
|| method.getParameterCount() != 2) {
LOGGER.error(
"action ret is not void:"
+ obj.getClass().getCanonicalName()
+ "-->"
+ method.getName());
System.exit(0);
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public void handle(String action, final U args, final T callback) public void handle(String action, final U args, final T callback)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// logger.info("[ActionExecutor] handle : "+action); // logger.info("[ActionExecutor] handle : "+action);