昨天說了怎麼配置openfire的開發環境。今天寫一點openfire的插件開發。我這裏做了一個例子主要是針對於XMPP的通信。後邊會說一點smack和openfire通信的實現。
注:這裏的例子中的註釋純屬個人理解。
在openfire的源碼裏有很多插件。我這裏實際就是拷貝了其中的一個插件。重名了一下名字。目錄結構如下:
貌似圖片傳不上來了。如果看不到圖,就看看源碼中的其他插件的例子。跟其他插件的目錄結構是一樣一樣的。
在這些文件裏最重要的就是plugin.xml文件。因爲有這個文件openfire才認識這個插件。在這個文件裏會配置插件的入口類。我這裏簡單寫了一個plugin.xml.示例如下。
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.yangzc.testplugin.TestPlugin</class>
<name>test Plugin</name>
<description>test Plugin descript</description>
<author>yangzc</author>
<version>1.0.0</version>
<date>20/6/2011</date>
<minServerVersion>3.7.0</minServerVersion>
<databaseKey>testPlugin</databaseKey>
<databaseVersion>0</databaseVersion>
<adminconsole>
</adminconsole>
</plugin>
class部分就是插件的實現類。氣的名字什麼的就不用說了,根據英文基本可以猜個差不多。
具體TestPlugin的實現。這裏也有一個例子:
package org.yangzc.testplugin;
import java.io.File;
import org.dom4j.Element;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.handler.IQHandler;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;
public class TestPlugin implements Plugin{
@Override
public void destroyPlugin() {
}
@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
XMPPServer service = XMPPServer.getInstance();
service.getIQRouter().addHandler(new MyIQHandler());
}
class MyIQHandler extends IQHandler{
public static final String moduleName = "testPlugin";
private IQHandlerInfo info;
public MyIQHandler(){
super(moduleName);
info = new IQHandlerInfo("group", "com:im:group");//設置監聽的命名空間
}
@Override
public IQHandlerInfo getInfo() {
return info;//取得指定監聽命名空間的IQHandeler
}
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
IQ reply = IQ.createResultIQ(packet);
Element groups = packet.getChildElement();//取得客戶端發送過來的xml
if (!IQ.Type.get.equals(packet.getType())){
System.out.println("非法的請求類型");
reply.setChildElement(groups.createCopy());
reply.setError(PacketError.Condition.bad_request);
return reply;
}
//String userName = StringUtils.substringBefore(packet.getFrom().toString(),"@");
//GroupManager.getInstance().initElement(groups,userName);
//reply.setChildElement(groups.createCopy());//2
//System.out.println("返回的最終XML" reply.toXML());
return reply;
}
}
}
在這個實現類中需要實現接口plugin。這個接口包含兩個方法。一個是初始化插件,一個是銷燬插件動作。
這個例子裏在初始化插件的時候通過service添加了一個監聽。這裏我理解爲監聽。這個監聽可以監聽指定命名空間的消息。這個例子裏代碼很簡單。
明天寫點smack和openfire通信的例子。敬請期待...
<!--EndFragment-->