Skip to content

Commit

Permalink
plugin resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jun 6, 2024
1 parent b587c85 commit d56ceba
Show file tree
Hide file tree
Showing 93 changed files with 976 additions and 1,587 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.linyuzai.plugin.core.concept;

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;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -17,65 +21,83 @@ public abstract class AbstractPlugin implements Plugin {
@Setter
private PluginConcept concept;

@Override
public void addReaders(PluginReader... readers) {
this.readers.addAll(Arrays.asList(readers));
public void addReader(PluginReader reader) {
this.readers.add(reader);
}

@Override
public Collection<PluginReader> getReaders(Class<? extends PluginReader> type) {
public void removeReader(PluginReader reader) {
this.readers.remove(reader);
}

public Collection<PluginReader> getReaders(Class<?> readable) {
return readers.stream()
.filter(type::isInstance)
.filter(it -> it.support(readable))
.collect(Collectors.toList());
}

@SuppressWarnings("unchecked")
@Override
public <T> T read(Class<? extends PluginReader> type, String name) {
public <T> T read(Class<T> type, Object key) {
for (PluginReader reader : getReaders(type)) {
Object read = reader.read(name);
Object read = reader.read(key, createReadContent());
if (read != null) {
return (T) read;
}
}
return null;
}

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

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

@Override
public void prepare(PluginContext context) {
onPrepare(context);
PluginTree.Node node = context.get(PluginTree.Node.class);
Collection<Object> contents = collectContent(context);
for (Object content : contents) {
Plugin subPlugin = context.getConcept().create(content);
Collection<PluginEntry> entries = collectEntries(context);
for (PluginEntry entry : entries) {
Plugin subPlugin = getConcept().create(entry);
if (subPlugin == null) {
node.create(content, content, this);
node.create(entry.getName(), entry, this);
} else {
PluginTree.Node subTree = node.create(content, subPlugin, this);
PluginTree.Node subTree = node.create(entry.getName(), subPlugin, this);
PluginContext subContext = context.createSubContext();
subContext.initialize();
subContext.set(Plugin.class, subPlugin);
subContext.set(PluginTree.class, subTree);
subContext.set(PluginTree.Node.class, subTree);
subPlugin.prepare(subContext);
subContext.destroy();
}
}
onPrepare(context);
}

@Override
public void release(PluginContext context) {
onRelease(context);
PluginTree.Node node = context.get(PluginTree.Node.class);
for (PluginTree.Node child : node.getChildren()) {
PluginContext subContext = context.createSubContext();
subContext.set(PluginTree.class, child);
subContext.set(PluginTree.Node.class, child);
subContext.set(Plugin.class, child.getPlugin());
child.getPlugin().release(subContext);
subContext.destroy();
}
for (PluginReader reader : readers) {
try {
reader.close();
} catch (IOException e) {
//TODO
}
}
onRelease(context);
}

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

public void onPrepare(PluginContext context) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.linyuzai.plugin.core.filter.PluginFilter;
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;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -106,12 +107,14 @@ public Plugin load(Object o) {
PluginContext context = pluginContextFactory.create(this);
//初始化上下文
context.initialize();
context.set(Plugin.class, plugin);

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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.util.Collection;
import java.util.Properties;
import java.io.IOException;

/**
* 插件抽象
Expand All @@ -23,11 +24,9 @@ public interface Plugin {

Object getId();

void addReaders(PluginReader... readers);
<T> T read(Class<T> readable, Object key);

Collection<PluginReader> getReaders(Class<? extends PluginReader> type);

<T> T read(Class<? extends PluginReader> type, String name);
PluginMetadata getMetadata();

PluginConcept getConcept();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.linyuzai.plugin.core.concept;

import java.io.IOException;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.linyuzai.plugin.core.concept;

import com.github.linyuzai.plugin.core.read.content.PluginContent;

public interface PluginEntry {

String getName();

Plugin getPlugin();

PluginContent getContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public PluginContext createSubContext() {
@Override
public void publish(Object event) {
PluginEventPublisher publisher = get(PluginEventPublisher.class);
if (publisher != null) {
publisher.publish(event);
}
publisher.publish(event);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ default PluginConcept getConcept() {
/**
* 获得插件 {@link Plugin}
*
* @param <P> {@link Plugin} 类型
* @return {@link Plugin}
*/
default <P extends Plugin> P getPlugin() {
default Plugin getPlugin() {
return get(Plugin.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.linyuzai.plugin.core.factory;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.concept.PluginConcept;
import com.github.linyuzai.plugin.core.concept.PluginEntry;

public abstract class SubPluginFactory implements PluginFactory {

@Override
public boolean support(Object o, PluginConcept concept) {
if (o instanceof PluginEntry) {
return doSupport((PluginEntry) o, concept);
} else {
return false;
}
}

public abstract boolean doSupport(PluginEntry entry, PluginConcept concept);

@Override
public Plugin create(Object o, PluginConcept concept) {
return doCreate((PluginEntry) o, concept);
}

public abstract Plugin doCreate(PluginEntry entry, PluginConcept concept);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public PluginFilter negate() {
*
* @param context 上下文 {@link PluginContext}
*/
@SuppressWarnings("unchecked")
@Override
public void filter(PluginContext context) {
Object inboundKey = getInboundKey();
Expand All @@ -41,7 +42,7 @@ public void filter(PluginContext context) {
tree.getTransformer()
.create(this)
.inboundKey(inboundKey)
.transform(node -> node.<T>filter(value -> applyNegation(doFilter(value))))
.transform(node -> node.filter(it -> applyNegation(doFilter((T) it.getValue()))))
.outboundKey(outboundKey);
/*Object key = getKey();
T original = context.get(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.github.linyuzai.plugin.core.filter;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.concept.PluginEntry;
import com.github.linyuzai.plugin.core.handle.HandlerDependency;
import com.github.linyuzai.plugin.core.resolve.PathNameResolver;
import com.github.linyuzai.plugin.core.resolve.EntryResolver;
import com.github.linyuzai.plugin.core.util.AntPathMatcher;
import lombok.Getter;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* 名称过滤器
*/
@Getter
@HandlerDependency(PathNameResolver.class)
public class NameFilter extends AbstractPluginFilter<List<String>> {
@HandlerDependency(EntryResolver.class)
public class NameFilter extends AbstractPluginFilter<PluginEntry> {

/**
* 名称模式
Expand All @@ -39,11 +38,8 @@ public Object getKey() {
}

@Override
public boolean doFilter(List<String> plugins) {
return true;
/*return plugins.stream()
.filter(it -> applyNegation(matchName(it)))
.collect(Collectors.toList());*/
public boolean doFilter(PluginEntry entry) {
return matchName(entry.getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.github.linyuzai.plugin.core.filter;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.concept.PluginEntry;
import com.github.linyuzai.plugin.core.handle.HandlerDependency;
import com.github.linyuzai.plugin.core.resolve.PathNameResolver;
import com.github.linyuzai.plugin.core.resolve.EntryResolver;
import com.github.linyuzai.plugin.core.util.AntPathMatcher;
import lombok.Getter;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* 路径过滤器
*/
@Getter
@HandlerDependency(PathNameResolver.class)
public class PathFilter extends AbstractPluginFilter<List<String>> {
@HandlerDependency(EntryResolver.class)
public class PathFilter extends AbstractPluginFilter<PluginEntry> {

/**
* 路径模式
Expand All @@ -34,16 +32,13 @@ public PathFilter(Collection<String> paths) {
}

@Override
public boolean doFilter(List<String> plugins) {
return true;
/*return plugins.stream()
.filter(it -> applyNegation(matchPath(it)))
.collect(Collectors.toList());*/
public boolean doFilter(PluginEntry entry) {
return matchPath(entry.getName());
}

@Override
public Object getKey() {
return Plugin.PATH_NAME;
return PluginEntry.class;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.handle.HandlerDependency;
import com.github.linyuzai.plugin.core.resolve.ByteArrayResolver;
import com.github.linyuzai.plugin.core.resolve.ContentResolver;

import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;
Expand All @@ -11,7 +11,7 @@
/**
* 内容匹配器
*/
@HandlerDependency(ByteArrayResolver.class)
@HandlerDependency(ContentResolver.class)
public class ContentMatcher extends AbstractPluginMatcher<Map<Object, byte[]>> {

public ContentMatcher(Annotation[] annotations) {
Expand Down

This file was deleted.

Loading

0 comments on commit d56ceba

Please sign in to comment.