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

load handler

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