Last active
December 27, 2015 22:59
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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