如何用java编写自己的插件加载器?

发布于 2024-12-07 14:17:07 字数 258 浏览 0 评论 0原文

如何在我的 java 程序中实现插件功能?

我正在使用 Java。我当前的项目与通用电子硬件相关,它具有自定义命令集。

现在有一个通用的 GUI,通过它可以访问硬件。硬件在不同的环境中(即针对不同的客户端)以不同的方式运行。现在的问题是 GUI 必须能够添加插件。插件意味着,它必须能够向拥有特权的客户提供特定的便利。从客户端来看,插件的添加很简单,只需点击一个按钮即可添加特定设施。

我之所以考虑插件是因为只有在交付核心产品之后才会引入越来越多的设施。

How can I implement a plugin facility in my java program?

I am working with Java. My current project is something related to a general purpose electronics hardware, which have a custom command set.

Now there is a general GUI through which one can access the hardware. The hardware behaves in different ways in different environment, i.e. for different clients. Now the problem is that the GUI must be capable of adding plugins. Plugin means, it must be capable of giving a particular facility to a customer who has the privilege. From the customer side, the addition of plugin is to be simple, like just click on a button to add a particular facility.

The reason why I think about plugins is that more and more facility will be introduced only after delivering the core product.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我偏爱纯白色 2024-12-14 14:17:07

您需要提供以下内容:

  • 创建一个 API,您的插件可以使用它来更改/扩展程序的行为(恕我直言,这是最棘手的部分)
  • 定义插件的公共条目,例如插件- 定义插件的入口点或扩展点的特定属性文件(实现接口的插件类的类名)
  • 从您选择的位置动态加载插件,例如,来自的所有 *.jar 文件一个特定的目录(看看URLClassLoader)

API 建议:

  • 更喜欢接口而不是(抽象)类,
  • 它可能有助于帮助用户快速查看她可以实现哪些接口(例如 IAction,注意前面的 < code>I),并由您的应用程序提供供插件使用(例如 WindowManager

You need to provide following things:

  • create an API which your plug-ins can use to change/extend the behavior of your program (IMHO this is the trickiest part)
  • define a common entry to the plug-ins, e.g., a plug-in-specific properties file defining the entry point or extension points of your plug-in (class name of a plug-in class implementing an interface)
  • dynamically load the plug-ins from a location of your choice, e.g., all *.jar files from a specific directory (take a look at URLClassLoader)

API suggestions:

  • prefer interfaces over (abstract) classes
  • it might be useful to help the user to quickly see which interfaces she could implement (e.g. IAction, notice the leading I) and which are provided by your application for plug-in usage (e.g. WindowManager)
无语# 2024-12-14 14:17:07

在任何面向对象的语言中实现插件背后的主要思想是定义插件和相关类必须实现的一组通用接口,然后通过反射加载和实例化它们......

您可以使用抽象工厂模式,以便任何对象插件需要的可以被实例化...

假设你的插件架构只有3个接口,每个插件必须提供实现这些接口的类,那么你的插件架构可以是这样的:

public interface PluginInterfaceA {
//Define API here
};

public interface PluginInterfaceB {
// Define API here
};

public interface PluginInterfaceC {
// Define API here
};

public interface PluginFactory {
/**
 * Creates plugin A object.
 */
PluginInterfaceA createPluginA();
/**
 * Creates plugin B object.
 */
PluginInterfaceB createPluginB();
/**
 * Creates plugin C object.
 */
PluginInterfaceC createPluginC();
};

然后让插件在XML中定义文件或属性文件插件工厂的类名插件:

例如,假设您的插件定义:

package com.my.plugin;

public class PluginAImpl implements PluginInterfaceA {
// Code for the class
};

public class PluginBImpl implements PluginInterfaceB {
// Code for the class
};

public class PluginCImpl implements PluginInterfaceC {
// Code for the class
};

public class PluginFactoryImpl implements PluginFactory {
public PluginInterfaceA createPluginA() {
  return new PluginAImpl();
}
public PluginInterfaceB createPluginB() {
  return new PluginAImpl();
}
public PluginInterfaceC createPluginC() {
  return new PluginAImpl();
}
};

然后在属性文件中定义
// 插件的plugin.jar中提供的文件plugin.properties
plugin.factory.class = com.my.plugin.PluginFactoryImpl;

在您的应用程序中可以执行

Properties properties = new Properties();
properties.load(this.getClass().getClassLoader().getResourceAsStream("plugin.properties"));

String factoryClass = properties.get("plugin.factory.class");

PluginFactory factory = Class.forName(factoryClass);

PluginInterfaceA interfaceA = factory.createPluginA();
PluginInterfaceB interfaceB = factory.createPluginB();
PluginInterfaceC interfaceC = factory.createPluginC();

// 这里根据您的喜好调用创建的类。

谢谢
巴勃罗

The main idea behind implementing plugins in any object oriented language, is to define a set of common interfaces that the plugin and related classes must implement, and then load and instantiate them via reflection...

You can use abstract factory pattern so that any objects needed by the plugin can be instantiated...

Let's say that your plugin architecture has only 3 interfaces and each plugin must provide classes that implement those interfaces, then your plugin architecture could be like this:

public interface PluginInterfaceA {
//Define API here
};

public interface PluginInterfaceB {
// Define API here
};

public interface PluginInterfaceC {
// Define API here
};

public interface PluginFactory {
/**
 * Creates plugin A object.
 */
PluginInterfaceA createPluginA();
/**
 * Creates plugin B object.
 */
PluginInterfaceB createPluginB();
/**
 * Creates plugin C object.
 */
PluginInterfaceC createPluginC();
};

And then let the plugins to define in an XML file or properties file the class name of the plugin factory for the plugin:

For instance let's say your plugin defines:

package com.my.plugin;

public class PluginAImpl implements PluginInterfaceA {
// Code for the class
};

public class PluginBImpl implements PluginInterfaceB {
// Code for the class
};

public class PluginCImpl implements PluginInterfaceC {
// Code for the class
};

public class PluginFactoryImpl implements PluginFactory {
public PluginInterfaceA createPluginA() {
  return new PluginAImpl();
}
public PluginInterfaceB createPluginB() {
  return new PluginAImpl();
}
public PluginInterfaceC createPluginC() {
  return new PluginAImpl();
}
};

And then define in the properties file
// File plugin.properties provided in the plugin.jar of the plugin
plugin.factory.class = com.my.plugin.PluginFactoryImpl;

In your application can do

Properties properties = new Properties();
properties.load(this.getClass().getClassLoader().getResourceAsStream("plugin.properties"));

String factoryClass = properties.get("plugin.factory.class");

PluginFactory factory = Class.forName(factoryClass);

PluginInterfaceA interfaceA = factory.createPluginA();
PluginInterfaceB interfaceB = factory.createPluginB();
PluginInterfaceC interfaceC = factory.createPluginC();

// Here invoke created classes as you like.

Thanks
Pablo

网白 2024-12-14 14:17:07

您可以随时向应用程序添加 jar 或插件。您无需执行任何特殊操作即可实现此目的。

如果您使用 OGSi,您可以更轻松地管理此操作,支持同一 jar 的多个版本并在应用程序运行时删除它们。我建议看看 Apache Karaf + iPOJO

You can add a jar or plugin to an application at anytime. You don't have to do anything special to achieve this.

If you use OGSi you can manage this easier, support multiple version of the same jar and remove them while the application is running. I suggest looking at Apache Karaf + iPOJO

冰雪之触 2024-12-14 14:17:07

JAR 文件格式有自己的小系统用于管理插件,它用于 Java SE 的多个部分,包括 JDBC 驱动程序管理。只需定义一个服务接口,将带有实现的 JAR 文件放在类路径上,然后使用 ServiceLoader.load

The JAR file format has its own little system for managing plugins, which is used for several parts of Java SE, including JDBC driver management. Just define a service interface, put JAR files with implementations on the classpath and load the implementations with ServiceLoader.load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文