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

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

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

@ -31,6 +31,6 @@ public class None<Element> extends Reducer<Element, List<Element>> {
@Override @Override
public List<Element> reduce() { 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; protected IExtensionRequest request;
@Setter @Setter
protected List<IExtension> extensionImplementations; protected List<IExtension> realizations;
@Setter @Setter
@Getter @Getter

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

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

Loading…
Cancel
Save