Implementing dynamic plugins in Java

后端 未结 5 1838
心在旅途
心在旅途 2021-01-31 10:45

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

  • The application would define an interface Plugin with a method like
相关标签:
5条回答
  • 2021-01-31 11:20

    Apologize if you know this, but check out the forName method of Class. It is used at least in JDBC to dynamically load the DBMS-specific driver classes runtime by class name.

    Then I guess it would not be difficult to enumerate all class/jar files in a directory, load each of them, and define an interface for a static method getCapabilities() (or any name you choose) that returns their capabilities/description in whatever terms and format that makes sense for your system.

    0 讨论(0)
  • 2021-01-31 11:23

    Any chance you can leverage the Service Provider Interface?

    0 讨论(0)
  • 2021-01-31 11:36

    The best way to implement plug-ins with Guice is with Multibindings. The linked page goes into detail on how to use multibindings to host plugins.

    0 讨论(0)
  • 2021-01-31 11:41

    This is actually quite easy using plain Java means:

    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.

    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.

    0 讨论(0)
  • 2021-01-31 11:44

    OSGI would be fine if you want to replace the plugins during runtime i.g. for bugfixes in a 24/7 environment. I played a while with OSGI but it took too much time, because it wasn't a requirement, and you need a plan b if you remove a bundle.

    My humble solution then was, providing a properties files with the class names of plugin descriptor classes and let the server call them to register (including quering their capabilities).

    This is obvious suboptimal but I can't wait to read the accepted answer.

    0 讨论(0)
提交回复
热议问题