Use Plugin Classpath with Openfire Plugin

Published:
Reading time:
About 1 min

When creating an Openfire plugin, some classes cannot be found by the default classpath. This is a result of the way that Openfire loads plugins and their jars into the classpath. For me, specifically, I got a ClassNotFoundException when trying to create an IntialContext with WLIntialContextFactory, even though the weblogic.jar was in my plugin lib folder. One solution is to move everything from the plugin/lib folder into openfire/lib. But this is messy at best.

Luckily, I found a solution from someone trying to use
GWT in an Openfire Plugin.

First, set a PluginClassLoader on your Plugin class. Here is my trimmed example:


private PluginClassLoader pluginClassLoader = null;

public void initializePlugin(PluginManager pManager, File pluginDirectory) {
pluginClassLoader = pManager.getPluginClassloader(this);
}

public PluginClassLoader getPluginClassLoader() {
return pluginClassLoader;
}


Then, in my class that attempts to load a Context, before loading it I do this:

MyPlugin myPlugin = (MyPlugin) XMPPServer.getInstance()
.getPluginManager().getPlugin(“myplugin”);
if (myPlugin != null && myPlugin.getPluginClassLoader() != null) {
Thread.currentThread().setContextClassLoader(
myPlugin.getPluginClassLoader().getClassLoader());
}