怎么开发 openFire 插件我就不说了,官方文档说得听明白,还可以参考 phoenix's blog
我要实现一个功能,从 openFire 的配置文件读取一些拦截器的类名,然后让它们生效
InterceptorRegisterPlugin.java
import java.io.File;import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;public class InterceptorRegisterPlugin implements Plugin {public void destroyPlugin() {}public void initializePlugin(PluginManager manager, File pluginDirectory) {InterceptorManager im = InterceptorManager.getInstance();String[] names = JiveGlobals.getXMLProperty("interceptors", "").split("[,;:\\s]");for (String name : names) {name = name.trim();if (!"".equals(name)) {try {PacketInterceptor interceptor = (PacketInterceptor) (Class.forName(name).newInstance());im.addInterceptor(interceptor);Log.info(name + " registed");} catch (ClassNotFoundException e) {Log.error("<" + name + "> not found", e);} catch (ClassCastException e) {Log.error("<" + name + "> is not a PacketInterceptor", e);} catch (InstantiationException e) {Log.error("", e);} catch (IllegalAccessException e) {Log.error("", e);}}}}}
<?xml version="1.0" encoding="UTF-8"?>
<jive>...<interceptors><!-- 这里列出 拦截器的全限定类名,用,;:空白符分割它们 --></interceptors>
</jive>
再写一个拦截器试试
SimpleInterceptor.java
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.util.Log;
import org.xmpp.packet.Packet;public class SimpleInterceptor implements PacketInterceptor {public SimpleInterceptor() {super();}public void interceptPacket(Packet packet, Session session,boolean incoming, boolean processed) throws PacketRejectedException {Log.info(packet.toXML());}}
... <interceptors>SimpleInterceptor</interceptors> ...
没什么好说的,- 3- 看不懂代码就去翻 openFire 的 API手册和文档