插件的编写可以参考:http://www.cnblogs.com/hoojo/archive/2013/03/07/2947502.html
其实openfire的插件很好理解:只要在openfire/openfire/plugins这个目录下面,添加插件的文件夹,同时在文件夹里面有plugin.xml和lib包即可,平常要是修改可以直接到lib包中替换对应的jar包,重启即可。
自己写了一个实例:
import java.io.File;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.muc.MUCEventDispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ecc.uc.plugin.listener.MucRoomListener;
import ecc.uc.plugin.listener.SaveMessageListener;
/**
* @author wengwh
* @Date 2014-8-4
*/
public class UcPlugin implements Plugin{
private static final Logger log = LoggerFactory.getLogger(UcPlugin.class);
private XMPPServer server;
private InterceptorManager interceptorManager;
private SaveMessageListener saveMessageListener;
private RosterListener rosterListener;
private MucRoomListener mucRoomListener;
public UcPlugin(){
server = XMPPServer.getInstance();
interceptorManager = InterceptorManager.getInstance();
saveMessageListener = new SaveMessageListener();
rosterListener = new RosterListener();
mucRoomListener = new MucRoomListener();
}
public void initializePlugin(PluginManager manager, File pluginDirectory) {
interceptorManager.addInterceptor(saveMessageListener);
interceptorManager.addInterceptor(rosterListener);
MUCEventDispatcher.addListener(mucRoomListener);
System.out.println("初始化…… 安装插件!");
System.out.println(server.getServerInfo());
}
public void destroyPlugin() {
interceptorManager.removeInterceptor(saveMessageListener);
interceptorManager.removeInterceptor(rosterListener);
MUCEventDispatcher.removeListener(mucRoomListener);
System.out.println("服务器停止,销毁插件!");
}
}
插件的好处就是:可以直接使用openfire的源码,来进行一些开发过程中的需求,比如聊天记录保存,聊天室的管理等
2、IQHandler:相应包中特定的元素名或命名空间。下面的代码展示了如何注册一个IQHandler.
IQHandler myHandler = new MyIQHander();
IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
iqRouter.addHandler(myHandler);
3、PacketInterceptor:这种方式可以接收系统传输的所有包,并可以随意的丢弃它们。例如,一个interceptor 可以拦截并丢弃所有含有不健康信息的消息,或者将它们报告给系统管理员。
4、使用JiveGlobals.getProperty(String) 和 JiveGlobals.setProperty(String, String) 方法将我们的插件设置为openfire的一个全局属性。通过实现org.jivesoftware.util.PropertyEventListener方法可以将我们的插件做成一个属性监听器监听任何属性的变化。通过 PropertyEventDispatcher.addListener(PropertyEventListener)方法可以注册监听。要注意的一点是,一定要在destroyPlugin()方法中将注册的监听注销。
(分发器:给各个监听触发,openfire的一种代码编写机制)
如:MUCEventDispatcher,SessionEventDispatcher
这几个里面我感觉分发器这种是比较好用的,这种可以定位到专门的事件,并提供了比较完整的对应的类,以及各种事件对应的方法,在开发的过程会更加清晰。
在插件编写的过程中多利用XMPPServer.getInstance()这个获取到各种对象的管理类,使用这个管理类可以来操作openfire中那些自带的对象,比如Roster,muc等
举个例子:
MultiUserChatManager multiUserChatManager = XMPPServer.getInstance().getMultiUserChatManager();
Affiliation affiliation = multiUserChatManager.getMultiUserChatService(roomJID).getChatRoom(roomJID.getNode()).getOccupantByFullJID(user).getAffiliation();