Skip to content

Create a Plugin

Frieder Reinhold edited this page Jul 4, 2020 · 2 revisions

The easiest way to get started is to checkout and modify the example plugin.

Plugin classes are instantiated only once for the whole application.
A new extension will be instantiated for each extension point.
If a plugin needs to receive lifecycle-hooks it must implement a class extending Plugin. This plugin class could implement extension points directly, but there will be two different objects created, one for the plugin and another one for the extension.

Maven Dependencies

    <dependencies>
        <dependency>
            <groupId>com.exxeta</groupId>
            <artifactId>correomqtt</artifactId>
            <version>0.12.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

Access Plugin from Extension

To access the plugin object for an extension use this constructor

@Extension
public class ExampleExtension implements ExampleHook {

    public ExampleExtension(Plugin plugin) {
        this.plugin = (ExamplePlugin) plugin;
    }
    ...
}

Permissions

If a plugin or extension needs more permissions it must provide a class extending PermissionPlugin which is a subclass of Plugin. To add new permissions override the Permissions getPermissions() {} method.

Extensions

To provide a simple extension leave the <plugin.class /> tag empty and create a new class which implements your desired hook from the package com.exxeta.correomqtt.plugin.spi.

Annotate the class with the @Extension annotation from PF4J and that's it.

Multiple Extensions for the same extension point in one Plugin

That's possible. Just add the @ExtensionId("my-id") annotation to each class.
Users then need to declare the plugin inside the plugins/protocol.xml like this:

<plugin name="example-id" extensionId="my-id" />

Configure Extensions

Users can configure each extension in the plugins/protocol.xml.

<plugin name="example-id">
    <config>
        <example color="green" />
    </config>
</plugin>

Your extension receives this configuration each time it is instantiated. Just implement this method:

void onConfigReceived(Element config) {}

Each extension can make up it's own configuration structure.

PF4J

CorreoMQTT is using PF4J to provide it's plugin architecture.
Learn more about PF4J on it's official homepage.