Skip to content

Commit

Permalink
plugin lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jun 15, 2024
1 parent d82c928 commit 03109b3
Show file tree
Hide file tree
Showing 28 changed files with 513 additions and 282 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.github.linyuzai.plugin.core.autoload;

import com.github.linyuzai.plugin.core.concept.Plugin;
import lombok.Getter;

/**
* 插件自动加载事件
*/
@Getter
public class PluginAutoLoadEvent extends PluginAutoEvent {

public PluginAutoLoadEvent(Plugin plugin) {
private final String path;

public PluginAutoLoadEvent(Plugin plugin, String path) {
super(plugin);
this.path = path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ public interface PluginAutoLoader {
void addGroup(String group);

Boolean getGroupState(String group);

Plugin getPlugin(String group, String name);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.github.linyuzai.plugin.core.autoload;

import com.github.linyuzai.plugin.core.concept.Plugin;
import lombok.Getter;

/**
* 插件自动重新加载事件
*/
@Getter
public class PluginAutoReloadEvent extends PluginAutoEvent {

public PluginAutoReloadEvent(Plugin plugin) {
private final String path;

public PluginAutoReloadEvent(Plugin plugin, String path) {
super(plugin);
this.path = path;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.github.linyuzai.plugin.core.autoload;

import com.github.linyuzai.plugin.core.concept.Plugin;
import lombok.Getter;

/**
* 插件自动重新加载事件
*/
@Getter
public class PluginAutoUnloadEvent extends PluginAutoEvent {

public PluginAutoUnloadEvent(Plugin plugin) {
private final String path;

public PluginAutoUnloadEvent(Plugin plugin, String path) {
super(plugin);
this.path = path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public class WatchServicePluginAutoLoader implements PluginAutoLoader {
*/
private final PluginLocation location;

private final Map<String, Object> pathIdMapping = new ConcurrentHashMap<>();

private final Map<String, Boolean> watchStates = new ConcurrentHashMap<>();

private WatchService watchService;
Expand Down Expand Up @@ -87,16 +85,6 @@ public Boolean getGroupState(String group) {
return watchStates.getOrDefault(group, false);
}

@Override
public Plugin getPlugin(String group, String name) {
String path = location.getLoadedPluginPath(group, name);
Object pluginId = pathIdMapping.get(path);
if (pluginId == null) {
return null;
}
return concept.getRepository().get(pluginId);
}

private void notifyOnStart(String group) {
String[] names = location.getLoadedPlugins(group);
for (String name : names) {
Expand Down Expand Up @@ -158,81 +146,75 @@ public void onNotify(WatchEvent<?> event, String group) {
}

public void onNotify(WatchEvent.Kind<?> kind, String group, String name) {
String pluginPath = location.getLoadedPluginPath(group, name);
if (pluginPath == null) {
String path = location.getLoadedPluginPath(group, name);
if (path == null) {
return;
}
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
onFileCreated(pluginPath);
onFileCreated(path);
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
onFileModified(pluginPath);
onFileModified(path);
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
onFileDeleted(pluginPath);
onFileDeleted(path);
}
}

/**
* 文件创建
*
* @param pluginPath 监听到的事件
* @param path 监听到的事件
*/
public void onFileCreated(String pluginPath) {
Plugin plugin = load(pluginPath);
concept.getEventPublisher().publish(new PluginAutoLoadEvent(plugin));
public void onFileCreated(String path) {
Plugin plugin = load(path);
concept.getEventPublisher().publish(new PluginAutoLoadEvent(plugin, path));
}

/**
* 文件修改
*
* @param pluginPath 监听到的事件
* @param path 监听到的事件
*/
public void onFileModified(String pluginPath) {
Plugin plugin = reload(pluginPath);
concept.getEventPublisher().publish(new PluginAutoReloadEvent(plugin));
public void onFileModified(String path) {
Plugin plugin = reload(path);
concept.getEventPublisher().publish(new PluginAutoReloadEvent(plugin, path));
}

/**
* 文件删除
*
* @param pluginPath 监听到的事件
* @param path 监听到的事件
*/
public void onFileDeleted(String pluginPath) {
Plugin plugin = unload(pluginPath);
public void onFileDeleted(String path) {
Plugin plugin = unload(path);
if (plugin != null) {
concept.getEventPublisher().publish(new PluginAutoUnloadEvent(plugin));
concept.getEventPublisher().publish(new PluginAutoUnloadEvent(plugin, path));
}
}

public void onError(Throwable e) {
concept.getEventPublisher().publish(new PluginLoadErrorEvent(e));
}

public Plugin load(String pluginPath) {
Plugin plugin = concept.load(pluginPath);
pathIdMapping.put(pluginPath, plugin.getId());
return plugin;
public Plugin load(String path) {
return concept.load(path);
}

/**
* 卸载并移除映射关系
*
* @param pluginPath 文件路径
* @param path 文件路径
*/
public Plugin unload(String pluginPath) {
Object id = pathIdMapping.remove(pluginPath);
if (id == null) {
return null;
}
return concept.unload(id);
public Plugin unload(String path) {
return concept.unload(path);
}

/**
* 重新加载
*
* @param pluginPath 插件源
* @param path 插件源
*/
public Plugin reload(String pluginPath) {
unload(pluginPath);
return load(pluginPath);
public Plugin reload(String path) {
unload(path);
return load(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ public String getDeletedPluginPath(String group, String name) {
return getPluginPath(group, name, DELETED);
}

@Override
public void load(String group, String name) {
move(group, name, UNLOADED, LOADED);
}

@Override
public void unload(String group, String name) {
move(group, name, LOADED, UNLOADED);
}

@Override
public void delete(String group, String name) {
move(group, name, UNLOADED, DELETED);
}

protected boolean move(String group, String name, String from, String to) {
String fromPath = getPluginPath(group, name, from);
if (fromPath == null) {
throw new IllegalArgumentException(name + " is not a Plugin");
}
File fromFile = new File(fromPath);
if (!fromFile.exists()) {
throw new IllegalArgumentException(name + " not existed");
}
String toPath = getPluginPath(group, name, to);
File toFile = new File(toPath);
int i = 1;
while (toFile.exists()) {
String path = toFile.getAbsolutePath();
int index = path.lastIndexOf(".");
if (index == -1) {
toFile = new File(path + i);
} else {
toFile = new File(path.substring(0, index) + "(" + i + ")" + path.substring(index));
}
i++;
}
return fromFile.renameTo(toFile);
}

protected File getGroupDirectory(String group) {
return check(new File(basePath, group));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public interface PluginLocation {

String getDeletedPluginPath(String group, String name);

void load(String group, String name);

void unload(String group, String name);

void delete(String group, String name);

interface Filter {

boolean filter(String group, String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,42 @@

import java.io.IOException;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Getter
@Setter
public abstract class AbstractPlugin implements Plugin {

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

private final Collection<DestroyListener> destroyListeners = new CopyOnWriteArrayList<>();

private Metadata metadata;

private PluginConcept concept;

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

@Override
public void removeReader(PluginReader reader) {
this.readers.remove(reader);
}

@Override
public void addDestroyListener(DestroyListener listener) {
this.destroyListeners.add(listener);
}

@Override
public void removeDestroyListener(DestroyListener listener) {
this.destroyListeners.remove(listener);
}

public Collection<PluginReader> getReaders(Class<?> readable) {
return readers.stream()
.filter(it -> it.support(readable))
Expand Down Expand Up @@ -105,6 +120,9 @@ public void release(PluginContext context) {

@Override
public void destroy() {
for (DestroyListener listener : destroyListeners) {
listener.onDestroy(this);
}
for (PluginReader reader : readers) {
try {
reader.close();
Expand Down
Loading

0 comments on commit 03109b3

Please sign in to comment.