fix: Modify some field names, add scoped target object filtering.

pull/1102/head
yanrongzhen 3 years ago
parent 6d0423d0f7
commit f7ceb40a42

@ -21,6 +21,7 @@ import cn.hippo4j.common.extension.IExtension;
import cn.hippo4j.common.extension.annotation.Realization;
import cn.hippo4j.common.extension.support.ExtensionRegistry;
import cn.hippo4j.common.toolkit.ClassUtil;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
@ -28,6 +29,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Extension register bootstrap
@ -45,20 +47,15 @@ public class ExtensionRegisterBootstrap implements ApplicationContextAware, Appl
@Override
public void run(ApplicationArguments args) throws Exception {
Map<String, Object> realizationBeanMap = applicationContext.getBeansWithAnnotation(Realization.class);
realizationBeanMap.values().forEach(
realization -> {
if (isInfrastructureClass(realization)) {
return;
}
if (realization instanceof IExtension) {
registry.register((IExtension) realization);
}
});
applicationContext.getBeansWithAnnotation(Realization.class)
.entrySet().stream()
.filter(entry -> !filterClass(entry.getKey(), entry.getValue()))
.forEach(entry -> registry.register((IExtension) entry.getValue()));
}
private boolean isInfrastructureClass(Object obj) {
return obj.getClass().isAssignableFrom(IExtension.class);
private boolean filterClass(String beanName, Object bean) {
return bean.getClass().isAssignableFrom(IExtension.class) ||
ScopedProxyUtils.isScopedTarget(beanName) ||
!(bean instanceof IExtension);
}
}

@ -41,11 +41,11 @@ public class AllMatch<Element> extends Reducer<Element, Boolean> {
@Override
public Boolean reduce() {
if (CollectionUtil.isEmpty(extensionImplementations)) {
if (CollectionUtil.isEmpty(realizations)) {
return false;
} else {
for (IExtension extensionImpl : extensionImplementations) {
if (!predicate.test(getCallback().apply(extensionImpl))) {
for (IExtension realization : realizations) {
if (!predicate.test(getCallback().apply(realization))) {
return false;
}
}

@ -41,10 +41,10 @@ public class AnyMatch<Element> extends Reducer<Element, Boolean> {
@Override
public Boolean reduce() {
if (CollectionUtil.isEmpty(extensionImplementations)) {
if (CollectionUtil.isEmpty(realizations)) {
return false;
}
for (IExtension extension : extensionImplementations) {
for (IExtension extension : realizations) {
if (predicate.test(getCallback().apply(extension))) {
return true;
}

@ -43,7 +43,7 @@ public class FirstOf<Element> extends Reducer<Element, Element> {
@Override
public Element reduce() {
for (IExtension extension : extensionImplementations) {
for (IExtension extension : realizations) {
Element element = getCallback().apply(extension);
if (null == predicate || predicate.test(element)) {
return element;

@ -31,6 +31,6 @@ public class None<Element> extends Reducer<Element, List<Element>> {
@Override
public List<Element> reduce() {
return extensionImplementations.stream().map(getCallback()).collect(Collectors.toList());
return realizations.stream().map(getCallback()).collect(Collectors.toList());
}
}

@ -36,7 +36,7 @@ public abstract class Reducer<Element, Result> {
protected IExtensionRequest request;
@Setter
protected List<IExtension> extensionImplementations;
protected List<IExtension> realizations;
@Setter
@Getter

@ -47,13 +47,13 @@ public class ExtensionInvoker {
Assert.isTrue(IExtension.class.isAssignableFrom(targetClz),
"can not execute extension point. please implement base extension interface(" + IExtension.class.getName() + ") first.");
List<IExtension> implementations = registry.find(targetClz);
if (CollectionUtil.isEmpty(implementations)) {
implementations = new ArrayList<>(ServiceLoaderRegistry.getSingletonServiceInstances(targetClz));
List<IExtension> realizations = registry.find(targetClz);
if (CollectionUtil.isEmpty(realizations)) {
realizations = new ArrayList<>(ServiceLoaderRegistry.getSingletonServiceInstances(targetClz));
}
Assert.notEmpty(implementations, "can not find any extension realizations with interface: " + targetClz.getName());
Assert.notEmpty(realizations, "can not find any extension realizations with interface: " + targetClz.getName());
reducer.setExtensionImplementations(implementations);
reducer.setRealizations(realizations);
reducer.setCallback((ExtensionCallback<IExtension, E>) callback);
return reducer.reduce();
}

@ -56,9 +56,9 @@ public class ExtensionRegistry implements IExtensionRegistry {
@SuppressWarnings("unchecked")
@Override
public void register(IExtension implementation) {
public void register(IExtension realization) {
Class<?> implClass = implementation.getClass();
Class<?> implClass = realization.getClass();
if (AopUtils.isAopProxy(implClass)) {
implClass = ClassUtils.getUserClass(implClass);
}
@ -68,25 +68,25 @@ public class ExtensionRegistry implements IExtensionRegistry {
for (Class<?> intf : interfaces) {
if (IExtension.class.isAssignableFrom(intf)) {
this.register((Class<IExtension>) intf, implementation);
this.register((Class<IExtension>) intf, realization);
}
}
}
private void register(Class<? extends IExtension> extension, IExtension realization) {
if (!registry.containsKey(extension) || CollectionUtil.isEmpty(registry.get(extension))) {
List<IExtension> implementations = new ArrayList<>();
implementations.add(realization);
registry.put(extension, implementations);
List<IExtension> realizations = new ArrayList<>();
realizations.add(realization);
registry.put(extension, realizations);
} else {
if (registry.get(extension).contains(realization)) {
log.warn(LogMessage.getInstance()
.kv("realizationClassName", realization.getClass().getName())
.msg("Extension realization already registered, skip."));
}
List<IExtension> implementations = registry.get(extension);
implementations.add(realization);
registry.put(extension, implementations);
List<IExtension> realizations = registry.get(extension);
realizations.add(realization);
registry.put(extension, realizations);
}
}

Loading…
Cancel
Save