Skip to content

Commit

Permalink
plugin 注释
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jul 13, 2024
1 parent a284235 commit f33dae2
Show file tree
Hide file tree
Showing 120 changed files with 1,453 additions and 893 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import lombok.RequiredArgsConstructor;

/**
* 插件自动加载事件
* 插件自动加载失败事件
*/
@Getter
@RequiredArgsConstructor
public class PluginAutoLoadErrorEvent implements PluginAutoEvent, PluginErrorEvent {

/**
* 插件路径
*/
private final String path;

private final Throwable error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
@Getter
public class PluginAutoLoadEvent extends AbstractPluginEvent implements PluginAutoEvent {

/**
* 插件路径
*/
private final String path;

public PluginAutoLoadEvent(Plugin plugin, String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.linyuzai.plugin.core.autoload;

/**
* 插件自动加载,可在某些场景下触发自动加载
* 插件自动加载
*/
public interface PluginAutoLoader {

Expand All @@ -10,14 +10,25 @@ public interface PluginAutoLoader {
*/
void start();

/**
* 开始监听
*
* @param load 加载已经存在的插件
*/
void start(boolean load);

/**
* 停止监听
*/
void stop();

/**
* 添加分组监听
*/
void addGroup(String group);

/**
* 获取分组监听状态
*/
Boolean getGroupState(String group);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import lombok.RequiredArgsConstructor;

/**
* 插件自动加载事件
* 插件自动卸载失败事件
*/
@Getter
@RequiredArgsConstructor
public class PluginAutoUnloadErrorEvent implements PluginAutoEvent, PluginErrorEvent {

/**
* 插件路径
*/
private final String path;

private final Throwable error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
@Getter
public class PluginAutoUnloadEvent extends AbstractPluginEvent implements PluginAutoEvent {

/**
* 插件路径
*/
private final String path;

public PluginAutoUnloadEvent(Plugin plugin, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.util.concurrent.ExecutorService;

/**
* 基于 {@link WatchService} 的插件文件自动加载器
* 基于目录监听的插件文件自动加载
*
* @see WatchService
*/
@Getter
@RequiredArgsConstructor
Expand All @@ -35,10 +37,16 @@ public class WatchServicePluginAutoLoader implements PluginAutoLoader {
*/
private final PluginLocation location;

/**
* 目录状态
*/
private final Map<String, Boolean> watchStates = new ConcurrentHashMap<>();

private WatchService watchService;

/**
* 是否运行
*/
private boolean running = false;

@Override
Expand All @@ -59,11 +67,13 @@ public synchronized void start(boolean load) {

executor.execute(this::listen);

//添加目录监听
String[] groups = location.getGroups();
for (String group : groups) {
addGroup(group);
}

//加载已经存在的插件
if (load) {
List<String> paths = new ArrayList<>();
for (String group : groups) {
Expand All @@ -90,15 +100,15 @@ public synchronized void start(boolean load) {
@Override
public void addGroup(String group) {
try {
String path = location.getLoadedPath(group);
registerPath(path);
String path = location.getLoadedBasePath(group);
registerPath(path, watchService);
watchStates.put(group, true);
} catch (Throwable e) {
watchStates.put(group, true);
}
}

protected void registerPath(String path) throws IOException {
protected void registerPath(String path, WatchService watchService) throws IOException {
Paths.get(path).register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
Expand Down Expand Up @@ -136,6 +146,7 @@ public void listen() {
try {
final WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
//获得分组
String groupPath = key.watchable().toString();
String group = location.getGroup(groupPath);
if (group == null) {
Expand All @@ -159,7 +170,7 @@ public void onNotify(WatchEvent<?> event, String group) {
if (kind == StandardWatchEventKinds.OVERFLOW) {
return;
}
String name = event.context().toString();
String name = event.context().toString();//文件名
onNotify(kind, group, name);
}

Expand All @@ -179,26 +190,20 @@ public void onNotify(WatchEvent.Kind<?> kind, String group, String name) {

/**
* 文件创建
*
* @param path 监听到的事件
*/
public void onFileCreated(String path) {
load(path);
}

/**
* 文件修改
*
* @param path 监听到的事件
*/
public void onFileModified(String path) {
reload(path);
}

/**
* 文件删除
*
* @param path 监听到的事件
*/
public void onFileDeleted(String path) {
unload(path);
Expand All @@ -208,6 +213,9 @@ public void onListenError(Throwable e) {
concept.getLogger().error("Plugin listen error", e);
}

/**
* 加载插件
*/
public void load(String path) {
try {
Plugin plugin = concept.load(path);
Expand All @@ -217,6 +225,9 @@ public void load(String path) {
}
}

/**
* 重新加载插件
*/
public void reload(String path) {
unload(path);
load(path);
Expand All @@ -229,9 +240,7 @@ public void reload(String path) {
}

/**
* 卸载并移除映射关系
*
* @param path 文件路径
* 卸载插件
*/
public void unload(String path) {
try {
Expand Down
Loading

0 comments on commit f33dae2

Please sign in to comment.