Skip to content

Commit

Permalink
plugin bean
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jun 10, 2024
1 parent d2b6472 commit 3a278ba
Show file tree
Hide file tree
Showing 46 changed files with 832 additions and 627 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public abstract class PluginNotifier implements BiConsumer<String, WatchEvent.Ki
public void accept(String path, WatchEvent.Kind<?> kind) {
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
Plugin plugin = load(path);
concept.publish(new PluginAutoLoadEvent(plugin));
concept.getEventPublisher().publish(new PluginAutoLoadEvent(plugin));
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
Plugin plugin = reload(path);
concept.publish(new PluginAutoReloadEvent(plugin));
concept.getEventPublisher().publish(new PluginAutoReloadEvent(plugin));
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
Plugin plugin = unload(path);
if (plugin != null) {
concept.publish(new PluginAutoUnloadEvent(plugin));
concept.getEventPublisher().publish(new PluginAutoUnloadEvent(plugin));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,55 +53,74 @@ protected PluginContext createReadContent() {
}

@Override
public void prepare(PluginContext context) {
public void initialize() {
onInitialize();
}

@Override
public void open(PluginContext context) {
PluginTree.NodeFactory node = context.get(PluginTree.Node.class);
collectEntries(context, entry -> {
Plugin subPlugin = getConcept().create(entry, context);
if (subPlugin == null) {
node.create(entry.getId(), entry.getName(), entry);
} else {
subPlugin.setConcept(getConcept());
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);
subPlugin.prepare(subContext);
subPlugin.open(subContext);
subContext.destroy();
}
});
onPrepare(context);
onOpen(context);
}

@Override
public void release(PluginContext context) {
public void close(PluginContext context) {
PluginTree.Node node = context.get(PluginTree.Node.class);
for (PluginTree.Node child : node.getChildren()) {
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);
subPlugin.close(subContext);
subContext.destroy();
}
}
onClose(context);
}

@Override
public void destroy() {
for (PluginReader reader : readers) {
try {
reader.close();
} catch (IOException e) {
//TODO
}
}
onRelease(context);
onDestroy();
}

public abstract void collectEntries(PluginContext context, Consumer<Entry> consumer);

public void onPrepare(PluginContext context) {
public void onInitialize() {

}

public void onDestroy() {

}

public void onOpen(PluginContext context) {

}

public void onRelease(PluginContext context) {
public void onClose(PluginContext context) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.linyuzai.plugin.core.handle.PluginHandler;
import com.github.linyuzai.plugin.core.handle.PluginHandlerChain;
import com.github.linyuzai.plugin.core.handle.PluginHandlerChainFactory;
import com.github.linyuzai.plugin.core.repository.PluginRepository;
import com.github.linyuzai.plugin.core.resolve.PluginResolver;
import com.github.linyuzai.plugin.core.tree.PluginTree;
import com.github.linyuzai.plugin.core.tree.PluginTreeFactory;
Expand All @@ -18,7 +19,6 @@
import lombok.SneakyThrows;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* {@link PluginConcept} 抽象类
Expand All @@ -27,8 +27,6 @@
@Setter
public abstract class AbstractPluginConcept implements PluginConcept {

public static final String DEFAULT_GROUP = "default";

/**
* 上下文工厂
*/
Expand All @@ -43,12 +41,14 @@ public abstract class AbstractPluginConcept implements PluginConcept {

protected PluginHandlerChainFactory handlerChainFactory;

protected PluginRepository repository;

protected Collection<PluginHandler> handlers;

/**
* 插件工厂
*/
protected Collection<PluginFactory> pluginFactories;
protected Collection<PluginFactory> factories;

/**
* 插件提取器
Expand All @@ -57,19 +57,14 @@ public abstract class AbstractPluginConcept implements PluginConcept {

protected volatile PluginHandlerChain handlerChain;

/**
* 插件缓存
*/
protected final Map<Object, Plugin> plugins = new ConcurrentHashMap<>();

@Override
public void initialize() {

}

@Override
public void destroy() {
//TODO release plugin
repository.stream().forEach(Plugin::destroy);
}

@Override
Expand Down Expand Up @@ -122,7 +117,7 @@ public Plugin create(Object o, PluginContext context) {
if (o instanceof Plugin) {
return (Plugin) o;
}
for (PluginFactory factory : pluginFactories) {
for (PluginFactory factory : factories) {
Plugin plugin = factory.create(o, context);
if (plugin != null) {
return plugin;
Expand All @@ -131,15 +126,10 @@ public Plugin create(Object o, PluginContext context) {
return null;
}

@Override
public Plugin load(Object o) {
return load(o, DEFAULT_GROUP);
}

/**
* 加载插件。
* 通过 {@link PluginFactory} 创建插件,
* 准备插件 {@link Plugin#prepare(PluginContext)},
* 准备插件 {@link Plugin#open(PluginContext)},
* 通过 {@link PluginContextFactory} 创建上下文 {@link PluginContext} 并初始化,
* 执行插件解析链 {@link PluginResolver},
* 通过 {@link PluginExtractor} 提取插件,
Expand All @@ -148,7 +138,7 @@ public Plugin load(Object o) {
* @param o 插件源
*/
@Override
public Plugin load(Object o, String group) {
public Plugin load(Object o) {
//创建上下文
PluginContext context = contextFactory.create(this);
context.set(PluginConcept.class, this);
Expand All @@ -164,16 +154,16 @@ public Plugin load(Object o, String group) {

eventPublisher.publish(new PluginCreatedEvent(plugin));

plugin.initialize();

context.set(Plugin.class, plugin);

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, eventPublisher);
plugin.open(context);

eventPublisher.publish(new PluginPreparedEvent(plugin));

Expand All @@ -185,77 +175,32 @@ public Plugin load(Object o, String group) {
extractor.extract(context);
}

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


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

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

@Override
public Plugin unload(Object o) {
return unload(o, DEFAULT_GROUP);
}

/**
* 卸载插件。
* 通过插件的 id 或插件本身移除对应的插件
*
* @param o 插件源
*/
@Override
public Plugin unload(Object o, String group) {
Plugin plugin = plugins.remove(o);
if (plugin == null) {
if (o instanceof Plugin) {
if (plugins.values().remove(o)) {
eventPublisher.publish(new PluginUnloadedEvent((Plugin) o));
return (Plugin) o;
}
}
} else {
eventPublisher.publish(new PluginUnloadedEvent(plugin));
return plugin;
public Plugin unload(Object o) {
Plugin removed = repository.remove(o);
if (removed != null) {
eventPublisher.publish(new PluginUnloadedEvent(removed));
}
return null;
return removed;
}

/**
* 插件是否加载
*
* @param o 插件 id 或插件对象
* @return 如果加载返回 true 否则返回 false
*/
@Override
public boolean isLoaded(Object o) {
return plugins.containsKey(o) || (o instanceof Plugin && plugins.containsValue(o));
}

/**
* 发布事件
*
* @param event 事件
*/
@Override
public void publish(Object event) {
eventPublisher.publish(event);
}

/**
* 获得插件
*
* @param id 插件 id
* @return 插件或 null
*/
@Override
public Plugin getPlugin(Object id) {
return plugins.get(id);
}

@SuppressWarnings("unchecked")
public static abstract class AbstractBuilder<B extends AbstractBuilder<B, T>, T extends AbstractPluginConcept> {
Expand All @@ -266,11 +211,13 @@ public static abstract class AbstractBuilder<B extends AbstractBuilder<B, T>, T

protected PluginTreeFactory treeFactory;

protected PluginRepository repository;

protected PluginEventPublisher eventPublisher;

protected List<PluginEventListener> eventListeners = new ArrayList<>();

protected List<PluginFactory> pluginFactories = new ArrayList<>();
protected List<PluginFactory> factories = new ArrayList<>();

protected List<PluginHandler> handlers = new ArrayList<>();

Expand All @@ -297,6 +244,11 @@ public B treeFactory(PluginTreeFactory treeFactory) {
return (B) this;
}

public B repository(PluginRepository repository) {
this.repository = repository;
return (B) this;
}

/**
* 设置事件发布者
*
Expand Down Expand Up @@ -346,7 +298,7 @@ public B addFactories(PluginFactory... factories) {
* @return {@link B}
*/
public B addFactories(Collection<? extends PluginFactory> factories) {
this.pluginFactories.addAll(factories);
this.factories.addAll(factories);
return (B) this;
}

Expand Down Expand Up @@ -419,7 +371,9 @@ public T build() {
concept.setContextFactory(contextFactory);
concept.setHandlerChainFactory(handlerChainFactory);
concept.setTreeFactory(treeFactory);
concept.setRepository(repository);
concept.setEventPublisher(eventPublisher);
concept.setFactories(factories);
concept.setHandlers(handlers);
concept.setExtractors(extractors);
return concept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ public interface Plugin {

void setConcept(PluginConcept concept);

void initialize();

void destroy();

/**
* 准备
*/
void prepare(PluginContext context);
void open(PluginContext context);

/**
* 释放
*/
void release(PluginContext context);
void close(PluginContext context);

interface Metadata {

Expand Down
Loading

0 comments on commit 3a278ba

Please sign in to comment.