Introduction
This blog used to introduce a practice of plugin system implementation of zstack.
I will finish base of plugin load, metadate definition, capability negotiation and usage part of Java. And following section describe abstractions of this implementation.
Abstractions
I make some abstractions to satisfy our aim.
- Unique identifer to find plugin and execute it
- Capability negotiation and version information
- Observer pattern to keep all modules use same way access plugin
PluginInterface
Plugin capability currently defines SUPPORTED and UNSUPPORTED for plugin definition
1 | public enum PluginCapabilityState { |
Define a plugin interface including three methods:
1 | public interface PluginInterface { |
note: capabilities offer a map about custom plugin and expected use enum value
use reflection to collect interfaces extend this interface as the metadata of all kinds of plugins:
1 | public interface PluginEndpointSender extends PluginInterface { |
so we need a manage class as the factory of plugin
PluginManager
1 | public interface PluginManager { |
this class defines two methods, first one used to report plugin capability and another one return singleton plugin.
And in order to reduce complexity, only scan interfaces under abstraction module as meta interfaces
1 | Platform.getReflections().getSubTypesOf(PluginInterface.class).forEach(clz -> { |
then load plugin instances from meta class:
1 | interfaceMetadata.forEach(clz -> Platform.getReflections().getSubTypesOf(clz) |
class will be used as the key and instance singleton will be stored and ready to use.
Use getPlugin could get the singleton. But currently version and uniqueName do not have specific usage but capabilties could be used to check if the plugin support the feature.
Version is used to check pluginInterface’s compatibility if pluginInterface has any uncompatible change, old version of plugin can run under compatible mode or just rejects load the plugin.
And now all plugins implemented pluginInterface will be loaded and not security check which should be done in pluginInterface and I will design it in next blogs.
Conclusion
Now I implemented part of loading the plugin, for usage is quite easy because developer only need to store the plugin class name as a variable to access the plugin but current safety issue is still should cared by all modules use plugin manager. So in next blog, I will do more works to resolve security requirements.