在Java中实现动态插件 [英] Implementing dynamic plugins in Java

查看:343
本文介绍了在Java中实现动态插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java应用程序中实现动态插件功能.理想情况下:

I'd like to implement a dynamic plugin feature in a Java application. Ideally:

  • 应用程序将使用getCapabilities()之类的方法定义接口Plugin.
  • 一个插件将是一个JAR pluginX.jar,其中包含实现Plugin的类PluginXImpl(可能还有其他一些).
  • 用户可以将pluginX.jar放在特殊目录中或设置指向它的配置参数.用户不必一定要在类路径中包含pluginX.jar.
  • 应用程序将找到PluginXImpl(也许通过JAR清单,也许是通过反射)并将其添加到注册表中.
  • 客户端可以通过调用诸如getPluginWithCapabilities("X")之类的方法来获取PluginXImpl的实例.用户不必一定要知道插件的名称.
  • The application would define an interface Plugin with a method like getCapabilities().
  • A plugin would be a JAR pluginX.jar containing a class PluginXImpl implementing Plugin (and maybe some others).
  • The user would put pluginX.jar in a special directory or set a configuration parameter pointing to it. The user should not necessarily have to include pluginX.jar in their classpath.
  • The application would find PluginXImpl (maybe via the JAR manifest, maybe by reflection) and add it to a registry.
  • The client could get an instance of PluginXImpl, e.g., by invoking a method like getPluginWithCapabilities("X"). The user should not necessarily have to know the name of the plugin.

我认为我应该可以使用豌豆来做到这一点,但是我对文档没有任何意义.我已经花了一些时间学习Guice,所以我的首选答案不是使用 Spring动态模块. "

I've got a sense I should be able to do this with peaberry, but I can't make any sense of the documentation. I've invested some time in learning Guice, so my preferred answer would not be "use Spring Dynamic Modules."

有人可以给我一个简单的想法,如何使用Guice/peaberry,OSGi或仅使用普通Java来做到这一点吗?

Can anybody give me a simple idea of how to go about doing this using Guice/peaberry, OSGi, or just plain Java?

推荐答案

使用纯Java手段实际上很容易:

This is actually quite easy using plain Java means:

由于您不希望用户在启动应用程序之前配置类路径,因此我将首先创建一个URLClassLoader,其中包含指向插件目录中文件的URL数组.使用File.listFiles查找所有插件jar,然后使用File.toURI().toURL()获取每个文件的URL.您应该将系统类加载器(ClassLoader.getSystemClassLoader())作为父类传递给URLClassLoader.

Since you don't want the user to configure the classpath before starting the application, I would first create a URLClassLoader with an array of URLs to the files in your plugin directory. Use File.listFiles to find all plugin jars and then File.toURI().toURL() to get a URL to each file. You should pass the system classloader (ClassLoader.getSystemClassLoader()) as a parent to your URLClassLoader.

如果插件罐包含java.util.ServiceLoader的API文档中所述的META-INF/services中的配置文件,则现在可以使用ServiceLoader.load(Plugin.class,myUrlClassLoader)来为您的Plugin接口,并在其上调用iterator()以获得所有已配置的Plugin实现的实例.

If the plugin jars contain a configuration file in META-INF/services as described in the API documentation for java.util.ServiceLoader, you can now use ServiceLoader.load(Plugin.class, myUrlClassLoader) to obatin a service loader for your Plugin interface and call iterator() on it to get instances of all configured Plugin implementations.

您仍然必须为此提供自己的包装,以过滤插件功能,但是我想这应该不会太麻烦.

You still have to provide your own wrapper around this to filter plugin capabilites, but that shouldn't be too much trouble, I suppose.

这篇关于在Java中实现动态插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆