Commit c3f628bb authored by 薛涛 Toby Xue's avatar 薛涛 Toby Xue 🆒

load handler

parent 657d18ca
Pipeline #4022 canceled with stages
package logwire.web.bo;
import logwire.core.bo.handler.OperationHandler;
import logwire.core.bo.eventhandler.ListOperationEventHandler;
import logwire.core.bo.eventhandler.ObjectOperationEventHandler;
import logwire.core.bo.eventhandler.TypeOperationEventHandler;
import logwire.core.bo.handler.ListOperationHandler;
import logwire.core.bo.handler.ObjectOperationHandler;
import logwire.core.bo.handler.TypeOperationHandler;
import logwire.core.bo.object.BizObject;
import logwire.core.meta.Config;
import logwire.core.resource.bean.BeanAdapter;
......@@ -31,14 +36,22 @@ public class BoRelationDefinition extends BeanAdapter {
private List<Class> listOperationProviders = new ArrayList<>();
@Config
private List<OperationHandler> typeOperationHandlers = new ArrayList<>();
private List<TypeOperationHandler> typeOperationHandlers = new ArrayList<>();
@Config
private List<Class> objectOperationHandlers = new ArrayList<>();
private List<ObjectOperationHandler> objectOperationHandlers = new ArrayList<>();
@Config
private List<Class> listOperationHandlers = new ArrayList<>();
private List<ListOperationHandler> listOperationHandlers = new ArrayList<>();
@Config
private List<TypeOperationEventHandler> typeOperationEventHandlers = new ArrayList<>();
@Config
private List<ObjectOperationEventHandler> objectOperationEventHandlers = new ArrayList<>();
@Config
private List<ListOperationEventHandler> listOperationEventHandlers = new ArrayList<>();
public List<Class> getTypeOperationProviders() {
return typeOperationProviders;
......@@ -77,27 +90,27 @@ public class BoRelationDefinition extends BeanAdapter {
this.listOperationProviders.add(listOperationProvider);
}
public List<OperationHandler> getTypeOperationHandlers() {
public List<TypeOperationHandler> getTypeOperationHandlers() {
return typeOperationHandlers;
}
public void setTypeOperationHandlers(List<OperationHandler> typeOperationHandlers) {
public void setTypeOperationHandlers(List<TypeOperationHandler> typeOperationHandlers) {
this.typeOperationHandlers = typeOperationHandlers;
}
public List<Class> getObjectOperationHandlers() {
public List<ObjectOperationHandler> getObjectOperationHandlers() {
return objectOperationHandlers;
}
public void setObjectOperationHandlers(List<Class> objectOperationHandlers) {
public void setObjectOperationHandlers(List<ObjectOperationHandler> objectOperationHandlers) {
this.objectOperationHandlers = objectOperationHandlers;
}
public List<Class> getListOperationHandlers() {
public List<ListOperationHandler> getListOperationHandlers() {
return listOperationHandlers;
}
public void setListOperationHandlers(List<Class> listOperationHandlers) {
public void setListOperationHandlers(List<ListOperationHandler> listOperationHandlers) {
this.listOperationHandlers = listOperationHandlers;
}
......@@ -126,4 +139,28 @@ public class BoRelationDefinition extends BeanAdapter {
public void setBoServiceInitialized(boolean boServiceInitialized) {
this.boServiceInitialized = boServiceInitialized;
}
public List<TypeOperationEventHandler> getTypeOperationEventHandlers() {
return typeOperationEventHandlers;
}
public void setTypeOperationEventHandlers(List<TypeOperationEventHandler> typeOperationEventHandlers) {
this.typeOperationEventHandlers = typeOperationEventHandlers;
}
public List<ObjectOperationEventHandler> getObjectOperationEventHandlers() {
return objectOperationEventHandlers;
}
public void setObjectOperationEventHandlers(List<ObjectOperationEventHandler> objectOperationEventHandlers) {
this.objectOperationEventHandlers = objectOperationEventHandlers;
}
public List<ListOperationEventHandler> getListOperationEventHandlers() {
return listOperationEventHandlers;
}
public void setListOperationEventHandlers(List<ListOperationEventHandler> listOperationEventHandlers) {
this.listOperationEventHandlers = listOperationEventHandlers;
}
}
......@@ -5,7 +5,12 @@ import logwire.core.bo.annotation.BizModel;
import logwire.core.bo.annotation.ListOperationProvider;
import logwire.core.bo.annotation.ObjectOperationProvider;
import logwire.core.bo.annotation.TypeOperationProvider;
import logwire.core.bo.eventhandler.ListOperationEventHandler;
import logwire.core.bo.eventhandler.ObjectOperationEventHandler;
import logwire.core.bo.eventhandler.OperationEventHandler;
import logwire.core.bo.eventhandler.TypeOperationEventHandler;
import logwire.core.bo.handler.ListOperationHandler;
import logwire.core.bo.handler.ObjectOperationHandler;
import logwire.core.bo.handler.OperationHandler;
import logwire.core.bo.handler.TypeOperationHandler;
import logwire.core.bo.object.BizObject;
......@@ -69,14 +74,18 @@ public class BoRelationBeanDefinitionLoader extends BeanLoader<BoRelationDefinit
field.set(null, o);
boRelationDefinition.setBoServiceInitialized(true);
} catch (NoSuchFieldException | IllegalAccessException | InstantiationException e) {
//TODO
e.printStackTrace();
}
}
}
private Map<String, BoRelationDefinition> loadBeanDefinition(TenantProject project) throws ClassNotFoundException {
/**
* @param project
* @return
* @throws Exception
*/
private Map<String, BoRelationDefinition> loadBeanDefinition(TenantProject project) throws Exception {
Map<String, BoRelationDefinition> boRelationBeanDefinitions = Maps.newHashMap();
TenantClassLoader classLoader = project.getTenantBeanContext().getTenantClassLoader();
ClassPathScanningCandidateComponentProvider provider = loaderUtil.getScanningBeanProvider();
......@@ -93,14 +102,12 @@ public class BoRelationBeanDefinitionLoader extends BeanLoader<BoRelationDefinit
//handler
if (loaderUtil.isOperationHandler(clazz)) {
if (!isConcrete((ScannedGenericBeanDefinition) boClassDefinition)) continue;
addOperationHandlerBeanDefinition(boRelationBeanDefinitions, (Class<OperationHandler>) clazz);
addOperationHandler(boRelationBeanDefinitions, (Class<OperationHandler>) clazz);
}
//event_handler
else if (OperationEventHandler.class.isAssignableFrom(clazz)) {
else if (loaderUtil.isOperationEventHandler(clazz)) {
if (!isConcrete((ScannedGenericBeanDefinition) boClassDefinition)) continue;
//TODO
addOperationEventHandler(boRelationBeanDefinitions, clazz);
addOperationEventHandler(boRelationBeanDefinitions, (Class<OperationEventHandler>) clazz);
}
//operation
else if (clazz.getAnnotation(TypeOperationProvider.class) != null) {
......@@ -114,8 +121,6 @@ public class BoRelationBeanDefinitionLoader extends BeanLoader<BoRelationDefinit
boRelationBeanDefinitions.get(type.getSimpleName()).addListOperationProvider(clazz);
}
}
}
return boRelationBeanDefinitions;
}
......@@ -140,23 +145,36 @@ public class BoRelationBeanDefinitionLoader extends BeanLoader<BoRelationDefinit
provider.addIncludeFilter(new AnnotationTypeFilter(ListOperationProvider.class));
}
private void addOperationEventHandler(Map<String, BoRelationDefinition> bizObjectBeanMap, Class<?> beanClass) {
//TODO
private void addOperationEventHandler(Map<String, BoRelationDefinition> bizObjectBeanMap, Class<OperationEventHandler> clazz) throws IllegalAccessException, InstantiationException {
//get BizObject from genericInterface
Class<? extends BizObject> bizObject = (Class<? extends BizObject>) ((ParameterizedType) clazz.getGenericInterfaces()[0]).getActualTypeArguments()[0];
BoRelationDefinition boRelationDefinition = bizObjectBeanMap.get(bizObject.getSimpleName());
OperationEventHandler opEventHandler = clazz.newInstance();
if (opEventHandler.isEnabled()) {
if (TypeOperationEventHandler.class.isAssignableFrom(clazz)) {
boRelationDefinition.getTypeOperationEventHandlers().add((TypeOperationEventHandler) opEventHandler);
} else if (TypeOperationHandler.class.isAssignableFrom(clazz)) {
boRelationDefinition.getObjectOperationEventHandlers().add((ObjectOperationEventHandler) opEventHandler);
} else {
boRelationDefinition.getListOperationEventHandlers().add((ListOperationEventHandler) opEventHandler);
}
}
}
private void addOperationHandlerBeanDefinition(Map<String, BoRelationDefinition> bizObjectBeanMap, Class<OperationHandler> clazz) {
if (TypeOperationHandler.class.isAssignableFrom(clazz)) {
private void addOperationHandler(Map<String, BoRelationDefinition> bizObjectBeanMap, Class<OperationHandler> clazz) throws IllegalAccessException, InstantiationException {
//get BizObject from genericInterface
Class<? extends BizObject> bizObject = (Class<? extends BizObject>) ((ParameterizedType) clazz.getGenericInterfaces()[0]).getActualTypeArguments()[0];
BoRelationDefinition boRelationDefinition = bizObjectBeanMap.get(bizObject.getSimpleName());
try {
boRelationDefinition.getTypeOperationHandlers().add(clazz.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
OperationHandler opHandler = clazz.newInstance();
if (opHandler.isEnabled()) {
if (TypeOperationHandler.class.isAssignableFrom(clazz)) {
boRelationDefinition.getTypeOperationHandlers().add((TypeOperationHandler) opHandler);
} else if (ObjectOperationHandler.class.isAssignableFrom(clazz)) {
boRelationDefinition.getObjectOperationHandlers().add((ObjectOperationHandler) opHandler);
} else if (ListOperationHandler.class.isAssignableFrom(clazz)) {
boRelationDefinition.getListOperationHandlers().add((ListOperationHandler) opHandler);
}
}
}
private boolean isConcrete(ScannedGenericBeanDefinition beanDefinition) {
......
package logwire.web.bo.loader;
import logwire.core.bo.eventhandler.OperationEventHandler;
import logwire.core.bo.handler.OperationHandler;
import logwire.core.bo.object.BizObject;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
......@@ -33,6 +34,10 @@ public class LoaderUtil {
return OperationHandler.class.isAssignableFrom(beanClass);
}
public boolean isOperationEventHandler(Class<?> beanClass) {
return OperationEventHandler.class.isAssignableFrom(beanClass);
}
private class AbstractTypeFilter extends AssignableTypeFilter {
public AbstractTypeFilter(Class<?> targetType) {
super(targetType);
......
package logwire.web.bo.proxy;
import logwire.core.bo.handler.OperationHandler;
import logwire.core.bo.handler.TypeOperationHandler;
import logwire.core.bo.handler.Handler;
import logwire.core.bo.object.BizObject;
import logwire.web.bo.BoRelationDefinition;
import logwire.web.security.SecurityUtil;
import logwire.web.security.TenantUser;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.SuperMethod;
import net.bytebuddy.implementation.bind.annotation.This;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
public class TypeOperationInterceptor {
private static final Logger logger = LoggerFactory.getLogger(TypeOperationInterceptor.class);
private BoRelationDefinition boRelationDefinition;
public TypeOperationInterceptor(BoRelationDefinition boRelationDefinition) {
......@@ -35,23 +38,31 @@ public class TypeOperationInterceptor {
}
/**
* 根据methodName找到对应的handler
* 根据methodName执行对应的handler
*/
private Object doHandler(Method method, Object[] args) {
List<OperationHandler> typeOperationHandlers = boRelationDefinition.getTypeOperationHandlers();
OperationHandler operationHandler = typeOperationHandlers.stream()
.filter(typeOperationHandler -> {
return method.getName().equals(typeOperationHandler.getOperation());
})
.max(Comparator.comparing(OperationHandler::getOrder)).get();
TenantUser currentUser = SecurityUtil.currentUser();
/* logger.debug("Execute operation {} start", new Object[]{operationName});
String namespace = logwire.core.resource.Util.operationName(thisOperable.getOperableName(), operationName);
actionContext.emit(namespace, BuiltinEventNames.OPERATION_BEFORE.name(), currentUser.getDomain(), new OperationEventData(thisOperable, inputArgs, null));
*/
return ((TypeOperationHandler) operationHandler).execute(boRelationDefinition.getBoClass(), args);
Class<? extends BizObject> bizClazz = boRelationDefinition.getBoClass();
String operationName = method.getName();
logger.debug("Execute operation {} start", new Object[]{operationName});
getHandler(operationName, boRelationDefinition.getTypeOperationEventHandlers(),
typeOperationEventHandler -> !typeOperationEventHandler.isAfter())
.doBefore(bizClazz, args);
Object result = getHandler(operationName, boRelationDefinition.getTypeOperationHandlers(),
typeOperationHandler -> typeOperationHandler.accept(bizClazz, args))
.execute(bizClazz, args);
getHandler(operationName, boRelationDefinition.getTypeOperationEventHandlers(),
typeOperationEventHandler -> typeOperationEventHandler.isAfter())
.doAfter(bizClazz, result, args);
return result;
}
private <T extends Handler> T getHandler(String operation, List<T> handlers, Function<T, Boolean> function) {
return handlers.stream()
.filter(handler -> operation.equals(handler.getOperation()) && function.apply(handler))
.max(Comparator.comparing(T::getOrder)).get();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment