Skip to content

Instantly share code, notes, and snippets.

@massenz
Last active December 27, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save massenz/7402453 to your computer and use it in GitHub Desktop.
Save massenz/7402453 to your computer and use it in GitHub Desktop.
Dynamic plugins loading using JCL (https://github.com/kamranzafar/JCL/) This is an example of a plugin that can be used with the babysitter server (https://github.com/massenz/babysitter)
package com.rivermeadow.plugins.simple;
import com.rivermeadow.babysitter.alerts.AlertPlugin;
import com.rivermeadow.babysitter.alerts.Context;
import com.rivermeadow.babysitter.alerts.Pager;
import com.rivermeadow.babysitter.model.Server;
/**
* Simplest example of a ``babysitter`` plugin, does nothing but demonstrate how to create a pluggable component.
*
* @author marco
*/
public class SamplePlugin implements Pager, AlertPlugin {
private static final String TEST = "plugins.simple.test";
Context ctx;
@Override
public void startup(Context ctx) {
this.ctx = ctx;
ctx.setNamedProperty(TEST, "value-99");
}
@Override
public Pager activate() {
return this;
}
@Override
public void deactivate() {
}
@Override
public void shutdown() {
}
@Override
public String getName() {
return getClass().getCanonicalName();
}
@Override
public String getDescription() {
return "This is a Sample plugin, to test JCL";
}
@Override
public String getPagerClassName() {
return getClass().getCanonicalName();
}
@Override
public void page(Server server) {
String value = (String) ctx.getNamedProperty(TEST);
System.out.println("The value is: " + value);
System.out.println("This is an alert for the Sample plugin: " + server.getDescription());
}
}
// Obviously this would be POSTed as a file upload
jarClassLoader.add("/tmp/jcl-test.jar");
//Create object of loaded class
Object obj = factory.create(jarClassLoader, fqn);
AlertPlugin plugin = JclUtils.cast(obj, AlertPlugin.class);
logger.info("Loaded valid AlertPlugin: " + plugin.getName() + " :: " + plugin.getDescription());
// ctx is an instance of the Context class and provides a generic plugin context
plugin.startup(ctx);
Pager pager = JclUtils.cast(plugin.activate(), Pager.class);
logger.info("Plugin activated, obtained Pager: " + pager.getClass().getName());
// Once we register the Pager with the AlertManager, it can start receiving actual pages
Server fakeServer = new Server(new ServerAddress("test", "10.10.0.100"), 80, 30);
fakeServer.setDescription("This is a fake server");
pager.page(fakeServer);
// the output from the SamplePlugin will be:
The value is: value-99
This is an alert for the Sample plugin: This is a fake server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment