Skip to content

Commit

Permalink
plugin filter/matcher/convertor
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jun 8, 2024
1 parent d56ceba commit 374bf0a
Show file tree
Hide file tree
Showing 89 changed files with 1,120 additions and 1,073 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.github.linyuzai.plugin.core.context.DefaultPluginContext;
import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.read.PluginReadable;
import com.github.linyuzai.plugin.core.read.PluginReader;
import com.github.linyuzai.plugin.core.read.metadata.PluginMetadata;
import com.github.linyuzai.plugin.core.tree.PluginTree;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -14,11 +12,13 @@
import java.util.stream.Collectors;

@Getter
@Setter
public abstract class AbstractPlugin implements Plugin {

private final Collection<PluginReader> readers = new ArrayList<>();

@Setter
private Metadata metadata;

private PluginConcept concept;

public void addReader(PluginReader reader) {
Expand Down Expand Up @@ -47,26 +47,21 @@ public <T> T read(Class<T> type, Object key) {
return null;
}

@Override
public PluginMetadata getMetadata() {
return read(PluginMetadata.class, null);
}

protected PluginContext createReadContent() {
return new DefaultPluginContext(null);
}

@Override
public void prepare(PluginContext context) {
PluginTree.Node node = context.get(PluginTree.Node.class);
Collection<PluginEntry> entries = collectEntries(context);
for (PluginEntry entry : entries) {
Plugin subPlugin = getConcept().create(entry);
PluginTree.NodeFactory node = context.get(PluginTree.Node.class);
Collection<Entry> entries = collectEntries(context);
for (Entry entry : entries) {
Plugin subPlugin = getConcept().create(entry, context);
if (subPlugin == null) {
node.create(entry.getName(), entry, this);
node.create(entry.getId(), entry.getName(), entry);
} else {
PluginTree.Node subTree = node.create(entry.getName(), subPlugin, this);
PluginContext subContext = context.createSubContext();
PluginTree.Node subTree = node.create(subPlugin.getId(), entry.getName(), subPlugin);
PluginContext subContext = context.createSubContext(false);
subContext.initialize();
subContext.set(Plugin.class, subPlugin);
subContext.set(PluginTree.Node.class, subTree);
Expand All @@ -81,11 +76,14 @@ public void prepare(PluginContext context) {
public void release(PluginContext context) {
PluginTree.Node node = context.get(PluginTree.Node.class);
for (PluginTree.Node child : node.getChildren()) {
PluginContext subContext = context.createSubContext();
subContext.set(PluginTree.Node.class, child);
subContext.set(Plugin.class, child.getPlugin());
child.getPlugin().release(subContext);
subContext.destroy();
if (child.getValue() instanceof Plugin) {
Plugin subPlugin = (Plugin) child.getValue();
PluginContext subContext = context.createSubContext(false);
subContext.set(PluginTree.Node.class, child);
subContext.set(Plugin.class, subPlugin);
subPlugin.release(subContext);
subContext.destroy();
}
}
for (PluginReader reader : readers) {
try {
Expand All @@ -97,7 +95,7 @@ public void release(PluginContext context) {
onRelease(context);
}

public abstract Collection<PluginEntry> collectEntries(PluginContext context);
public abstract Collection<Entry> collectEntries(PluginContext context);

public void onPrepare(PluginContext context) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import com.github.linyuzai.plugin.core.extract.PluginExtractor;
import com.github.linyuzai.plugin.core.factory.PluginFactory;
import com.github.linyuzai.plugin.core.filter.PluginFilter;
import com.github.linyuzai.plugin.core.handle.PluginHandler;
import com.github.linyuzai.plugin.core.handle.PluginHandlerChain;
import com.github.linyuzai.plugin.core.resolve.PluginResolver;
import com.github.linyuzai.plugin.core.resolve.PluginResolverChainImpl;
import com.github.linyuzai.plugin.core.tree.PluginTree;
import com.github.linyuzai.plugin.core.tree.PluginTreeFactory;
import lombok.Getter;
Expand All @@ -28,14 +29,16 @@ public abstract class AbstractPluginConcept implements PluginConcept {
/**
* 上下文工厂
*/
protected PluginContextFactory pluginContextFactory;
protected PluginContextFactory contextFactory;

/**
* 事件发布者
*/
protected PluginEventPublisher pluginEventPublisher;
protected PluginEventPublisher eventPublisher;

protected PluginTreeFactory pluginTreeFactory;
protected PluginTreeFactory treeFactory;

protected PluginHandlerChain handlerChain;

/**
* 插件工厂
Expand All @@ -45,17 +48,17 @@ public abstract class AbstractPluginConcept implements PluginConcept {
/**
* 插件解析器
*/
protected Collection<PluginResolver> pluginResolvers;
protected Collection<PluginResolver> resolvers;

/**
* 插件过滤器
*/
protected Collection<PluginFilter> pluginFilters;
protected Collection<PluginFilter> filters;

/**
* 插件提取器
*/
protected Collection<PluginExtractor> pluginExtractors;
protected Collection<PluginExtractor> extractors;

/**
* 插件缓存
Expand All @@ -69,13 +72,14 @@ public abstract class AbstractPluginConcept implements PluginConcept {
* @return 插件 {@link Plugin}
*/
@Override
public Plugin create(Object o) {
public Plugin create(Object o, PluginContext context) {
if (o instanceof Plugin) {
return (Plugin) o;
}
for (PluginFactory factory : pluginFactories) {
if (factory.support(o, this)) {
return factory.create(o, this);
Plugin plugin = factory.create(o, context);
if (plugin != null) {
return plugin;
}
}
return null;
Expand All @@ -94,49 +98,52 @@ public Plugin create(Object o) {
*/
@Override
public Plugin load(Object o) {
Plugin plugin = create(o);
//创建上下文
PluginContext context = contextFactory.create(this);
context.set(PluginConcept.class, this);
//初始化上下文
context.initialize();

Plugin plugin = create(o, context);
if (plugin == null) {
throw new PluginException("Plugin can not create: " + o);
}

plugin.setConcept(this);

pluginEventPublisher.publish(new PluginCreatedEvent(plugin));
eventPublisher.publish(new PluginCreatedEvent(plugin));

//创建上下文
PluginContext context = pluginContextFactory.create(this);
//初始化上下文
context.initialize();
context.set(Plugin.class, plugin);

PluginTree tree = pluginTreeFactory.create(plugin, this);
PluginTree tree = treeFactory.create(plugin, this);
context.set(PluginTree.class, tree);
context.set(PluginTree.Node.class, tree.getRoot());

//准备插件
plugin.prepare(context);
//在上下文中添加事件发布者
context.set(PluginEventPublisher.class, pluginEventPublisher);
context.set(PluginEventPublisher.class, eventPublisher);

pluginEventPublisher.publish(new PluginPreparedEvent(plugin));
eventPublisher.publish(new PluginPreparedEvent(plugin));

//解析插件
new PluginResolverChainImpl(new ArrayList<>(pluginResolvers), new ArrayList<>(pluginFilters))
.next(context);
/*new PluginResolverChainImpl(new ArrayList<>(resolvers), new ArrayList<>(filters))
.next(context);*/
handlerChain.next(context);

//提取插件
for (PluginExtractor extractor : pluginExtractors) {
for (PluginExtractor extractor : extractors) {
extractor.extract(context);
}

plugin.release(context);
//销毁上下文
context.destroy();
pluginEventPublisher.publish(new PluginReleasedEvent(plugin));
eventPublisher.publish(new PluginReleasedEvent(plugin));

plugins.put(plugin.getId(), plugin);

pluginEventPublisher.publish(new PluginLoadedEvent(plugin));
eventPublisher.publish(new PluginLoadedEvent(plugin));
return plugin;
}

Expand All @@ -152,12 +159,12 @@ public Plugin unload(Object o) {
if (plugin == null) {
if (o instanceof Plugin) {
if (plugins.values().remove(o)) {
pluginEventPublisher.publish(new PluginUnloadedEvent((Plugin) o));
eventPublisher.publish(new PluginUnloadedEvent((Plugin) o));
return (Plugin) o;
}
}
} else {
pluginEventPublisher.publish(new PluginUnloadedEvent(plugin));
eventPublisher.publish(new PluginUnloadedEvent(plugin));
return plugin;
}
return null;
Expand All @@ -181,7 +188,7 @@ public boolean isLoad(Object o) {
*/
@Override
public void publish(Object event) {
pluginEventPublisher.publish(event);
eventPublisher.publish(event);
}

/**
Expand Down Expand Up @@ -442,17 +449,17 @@ protected void preBuild() {
private void addResolversDependOnExtractors(Collection<? extends PluginExtractor> extractors) {
for (PluginExtractor extractor : extractors) {
//插件提取器依赖的插件解析器
Collection<Class<? extends PluginResolver>> dependencies = extractor.getDependencies();
for (Class<? extends PluginResolver> dependency : dependencies) {
Class<? extends PluginHandler>[] dependencies = extractor.getDependencies();
for (Class<? extends PluginHandler> dependency : dependencies) {
//已经存在
if (containsResolver(dependency)) {
continue;
}
//获得对应的实现类
Class<? extends PluginResolver> implOrDefault =
resolverDefaultImpl.getOrDefault(dependency, dependency);
/*Class<? extends PluginResolver> implOrDefault =
resolverDefaultImpl.getOrDefault(dependency, dependency);*/
//实例化
PluginResolver resolver = implOrDefault.newInstance();
PluginResolver resolver = (PluginResolver) dependency.newInstance();
//添加该插件解析器依赖的解析器
addResolversWithDependencies(Collections.singletonList(resolver));
}
Expand All @@ -473,11 +480,11 @@ private void addResolversWithDependencies(Collection<? extends PluginResolver> r
pluginResolvers.add(0, resolver);
}

Set<Class<? extends PluginResolver>> unfounded = new HashSet<>();
Set<Class<? extends PluginHandler>> unfounded = new HashSet<>();
for (PluginResolver resolver : resolvers) {
//插件解析器依赖的插件解析器
Collection<Class<? extends PluginResolver>> dependencies = resolver.getDependencies();
for (Class<? extends PluginResolver> dependency : dependencies) {
Class<? extends PluginHandler>[] dependencies = resolver.getDependencies();
for (Class<? extends PluginHandler> dependency : dependencies) {
//已经存在
if (containsResolver(dependency)) {
continue;
Expand All @@ -488,12 +495,10 @@ private void addResolversWithDependencies(Collection<? extends PluginResolver> r
List<PluginResolver> unfoundedPluginResolvers = new ArrayList<>();
if (!unfounded.isEmpty()) {
//遍历需要但是还没有的插件解析器类
for (Class<? extends PluginResolver> dependency : unfounded) {
for (Class<? extends PluginHandler> dependency : unfounded) {
//获得对应的实现类
Class<? extends PluginResolver> implOrDefault =
resolverDefaultImpl.getOrDefault(dependency, dependency);
//实例化
PluginResolver instance = implOrDefault.newInstance();
PluginResolver instance = (PluginResolver) dependency.newInstance();
unfoundedPluginResolvers.add(instance);
}
//添加这些新实例化的插件解析器依赖的插件解析器
Expand All @@ -507,7 +512,7 @@ private void addResolversWithDependencies(Collection<? extends PluginResolver> r
* @param target 目标插件解析器类
* @return 如果已经存在返回 true 否则返回 false
*/
private boolean containsResolver(Class<? extends PluginResolver> target) {
private boolean containsResolver(Class<? extends PluginHandler> target) {
for (PluginResolver resolver : pluginResolvers) {
if (target.isInstance(resolver)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
package com.github.linyuzai.plugin.core.concept;

import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.read.PluginReadable;
import com.github.linyuzai.plugin.core.read.PluginReader;
import com.github.linyuzai.plugin.core.read.metadata.PluginMetadata;

import java.io.IOException;
import java.io.InputStream;

/**
* 插件抽象
*/
public interface Plugin {

String PREFIX = "CONCEPT_PLUGIN@";

String PATH_NAME = PREFIX + "PATH_NAME";

String BYTE_ARRAY = PREFIX + "BYTE_ARRAY";

String PROPERTIES_NAME = PREFIX + "PROPERTIES_NAME";

String PROPERTIES = PREFIX + "PROPERTIES";

Object getId();

<T> T read(Class<T> readable, Object key);

PluginMetadata getMetadata();
Metadata getMetadata();

void setMetadata(Metadata metadata);

PluginConcept getConcept();

Expand All @@ -41,4 +31,29 @@ public interface Plugin {
* 释放
*/
void release(PluginContext context);

interface Metadata {

String get(String key);

String get(String key, String defaultValue);

boolean isEmpty();
}

interface Entry {

Object getId();

String getName();

Plugin getPlugin();

Content getContent();
}

interface Content {

InputStream getInputStream() throws IOException;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.plugin.core.concept;

import java.io.IOException;
import com.github.linyuzai.plugin.core.context.PluginContext;

import java.util.Map;

/**
Expand All @@ -14,7 +15,7 @@ public interface PluginConcept {
* @param o 插件源
* @return 插件 {@link Plugin}
*/
Plugin create(Object o);
Plugin create(Object o, PluginContext context);

/**
* 加载插件
Expand Down
Loading

0 comments on commit 374bf0a

Please sign in to comment.