pf4j zip 插件的处理

pf4j 对于zip 插件会先进行解压处理的,主要核心是基于PluginRepository 处理的
目前官方已经包含了不少实现

核心处理

  • 参考代码
    pf4j/src/main/java/org/pf4j/AbstractPluginManager.java
  • 方法
 
@Override
public void loadPlugins() {
    log.debug("Lookup plugins in '{}'", pluginsRoots);
    // check for plugins roots
    if (pluginsRoots.isEmpty()) {
        log.warn("No plugins roots configured");
        return;
    }
    pluginsRoots.forEach(path -> {
        if (Files.notExists(path) || !Files.isDirectory(path)) {
            log.warn("No '{}' root", path);
        }
    });
 
    // get all plugin paths from repository, 对于处理会调用createPluginRepository的repo ,后边介绍
    List<Path> pluginPaths = pluginRepository.getPluginPaths();
 
    // check for no plugins
    if (pluginPaths.isEmpty()) {
        log.info("No plugins");
        return;
    }

具体处理

  • 默认PluginManager支持的repo
    pf4j/src/main/java/org/pf4j/DefaultPluginManager.java
 
@Override
protected PluginRepository createPluginRepository() {
    return new CompoundPluginRepository()
        .add(new DevelopmentPluginRepository(getPluginsRoots()), this::isDevelopment)
        .add(new JarPluginRepository(getPluginsRoots()), this::isNotDevelopment)
        .add(new DefaultPluginRepository(getPluginsRoots()), this::isNotDevelopment);
}
  • zip 插件处理的repo
    主要基于DefaultPluginRepository
    pf4j/src/main/java/org/pf4j/DefaultPluginRepository.java
 
public class DefaultPluginRepository extends BasePluginRepository {
 
    private static final Logger log = LoggerFactory.getLogger(DefaultPluginRepository.class);
 
    public DefaultPluginRepository(Path... pluginsRoots) {
        this(Arrays.asList(pluginsRoots));
    }
 
    public DefaultPluginRepository(List<Path> pluginsRoots) {
        super(pluginsRoots);
 
        AndFileFilter pluginsFilter = new AndFileFilter(new DirectoryFileFilter());
        pluginsFilter.addFileFilter(new NotFileFilter(createHiddenPluginFilter()));
        setFilter(pluginsFilter);
    }
 
    @Override
    public List<Path> getPluginPaths() {
        extractZipFiles();  //  核心,先解压
        return super.getPluginPaths();
    }

说明

实际上默认会有一个pluginroot
pf4j/src/main/java/org/pf4j/AbstractPluginManager.java 的initialize 方法

 
if (pluginsRoots.isEmpty()) {
            pluginsRoots.addAll(createPluginsRoot());
        }
  • createPluginsRoot处理代码
protected List<Path> createPluginsRoot() {
     // 使用默认的路径(基于pf4j.pluginsDir 配置),支持配置多个,可以基于逗号分割
    String pluginsDir = System.getProperty(PLUGINS_DIR_PROPERTY_NAME);
    if (pluginsDir != null && !pluginsDir.isEmpty()) {
        return Arrays.stream(pluginsDir.split(","))
            .map(String::trim)
            .map(Paths::get)
            .collect(Collectors.toList());
    }
 
    pluginsDir = isDevelopment() ? DEVELOPMENT_PLUGINS_DIR : DEFAULT_PLUGINS_DIR;
    return Collections.singletonList(Paths.get(pluginsDir));
}

参考资料

https://pf4j.org/doc/packaging.html

posted on 2022-02-17 13:04  荣锋亮  阅读(185)  评论(0编辑  收藏  举报

导航