openfire 插件开发

插件的编写能够参考:http://www.cnblogs.com/hoojo/archive/2013/03/07/2947502.html html

其实openfire的插件很好理解:只要在openfire/openfire/plugins这个目录下面,添加插件的文件夹,同时在文件夹里面有plugin.xml和lib包便可,日常要是修改能够直接到lib包中替换对应的jar包,重启便可。 java


本身写了一个实例: 服务器

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的源码,来进行一些开发过程当中的需求,好比聊天记录保存,聊天室的管理等


在编写插件的过程当中,插件集成方式
一、Component:能够接收一个特定子域(sub-domain)的全部包。好比test_componet.hoo.com。因此一个发送给jojo@test_componet.hoo.com的包将被转发给这个componet.

二、IQHandler:相应包中特定的元素名或命名空间。下面的代码展现了如何注册一个IQHandler. dom

IQHandler myHandler = new MyIQHander(); spa

IQRouter iqRouter = XMPPServer.getInstance().getIQRouter(); 插件

iqRouter.addHandler(myHandler); code

三、PacketInterceptor:这种方式能够接收系统传输的全部包,并能够随意的丢弃它们。例如,一个interceptor 能够拦截并丢弃全部含有不健康信息的消息,或者将它们报告给系统管理员。 server

四、使用JiveGlobals.getProperty(String) 和 JiveGlobals.setProperty(String, String) 方法将咱们的插件设置为openfire的一个全局属性。经过实现org.jivesoftware.util.PropertyEventListener方法能够将咱们的插件作成一个属性监听器监放任何属性的变化。经过 PropertyEventDispatcher.addListener(PropertyEventListener)方法能够注册监听。要注意的一点是,必定要在destroyPlugin()方法中将注册的监听注销。 xml

(分发器:给各个监听触发,openfire的一种代码编写机制) htm

如:MUCEventDispatcher,SessionEventDispatcher


这几个里面我感受分发器这种是比较好用的,这种能够定位到专门的事件,并提供了比较完整的对应的类,以及各类事件对应的方法,在开发的过程会更加清晰。

在插件编写的过程当中多利用XMPPServer.getInstance()这个获取到各类对象的管理类,使用这个管理类能够来操做openfire中那些自带的对象,好比Roster,muc等

举个例子:

MultiUserChatManager multiUserChatManager = XMPPServer.getInstance().getMultiUserChatManager();
Affiliation affiliation = multiUserChatManager.getMultiUserChatService(roomJID).getChatRoom(roomJID.getNode()).getOccupantByFullJID(user).getAffiliation();
插件能够实现不少功能,在开发过程当中能够多查看openfire的源码,在插件中去使用来实现本身的功能。
相关文章
相关标签/搜索