openfire3.6.3插件开发方法说明:
作用:初始化时在控制台打印测试消息。
开发环境:winxp/myeclipse6.5/jdk1.5&1.6/mysql5.0/openfire3.6.3
QQ群:28588322 (openfire专题研究小组)
开发步骤:
1. 布署openfire3.6.3源码(请参考openfire3.6.3导入eclipse项目实现方法)
结构如下:
2. src/plugins目录下新建文件夹itvi。并按下图的目录结构新建对应的文件。
plugin.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.jivesoftware.openfire.plugin.Itvi</class>
<name>Itvi</name>
<description>Broadcasts messages to users.</description>
<author>openfireteam</author>
<version>1.7.0</version>
<date>2/19/2009</date>
<url>http://openfireteam.blogjava.net</url>
<minServerVersion>3.6.3</minServerVersion>
</plugin>
Itvi.java文件代码如下:
package org.jivesoftware.openfire.plugin;
import org.dom4j.Element;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.xmpp.packet.*;
import org.jivesoftware.openfire.handler.IQHandler;
import org.jivesoftware.openfire.auth.UnauthorizedException;
/**
* 处理客户端发来的IQ,并回送结果IQ
*
* @author Phoenix
*
* Mar 14, 2008 4:55:33 PM
*
* version 0.1
*/
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.disco.IQDiscoInfoHandler;
import org.jivesoftware.openfire.disco.IQDiscoItemsHandler;
import org.jivesoftware.openfire.forms.DataForm;
import org.jivesoftware.openfire.forms.FormField;
import org.jivesoftware.openfire.forms.spi.XDataFormImpl;
import org.jivesoftware.openfire.forms.spi.XFormFieldImpl;
import org.jivesoftware.openfire.resultsetmanager.ResultSet;
import org.jivesoftware.openfire.resultsetmanager.ResultSetImpl;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.*;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.PacketError.Condition;
import java.io.File;
import java.util.*;
import java.util.Map.Entry;
public class Itvi implements Plugin
{
private XMPPServer server;
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.container.Plugin#destroyPlugin()
*/
public void destroyPlugin()
{
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.container.Plugin#initializePlugin(org.jivesoftware.openfire.container.PluginManager,
* java.io.File)
*/
public void initializePlugin(PluginManager manager, File pluginDirectory)
{
server = XMPPServer.getInstance();
server.getIQRouter().addHandler(new ItviIQHander()); //
System.out.println("测试插件启动成功");
}
}
ItviIQHander.java文件代码如下:
package org.jivesoftware.openfire.plugin;
import org.dom4j.Element;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.xmpp.packet.*;
import org.jivesoftware.openfire.handler.IQHandler;
import org.jivesoftware.openfire.auth.UnauthorizedException;
/**
* 处理客户端发来的IQ,并回送结果IQ
*
* @author Phoenix
*
* Mar 14, 2008 4:55:33 PM
*
* version 0.1
*/
public class ItviIQHander extends IQHandler
{
private static final String MODULE_NAME = "group tree handler";
private static final String NAME_SPACE = "com:im:group";
private IQHandlerInfo info;
public ItviIQHander()
{
super(MODULE_NAME);
info = new IQHandlerInfo("gruops", NAME_SPACE);
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.handler.IQHandler#getInfo()
*/
@Override
public IQHandlerInfo getInfo()
{
return info;
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.handler.IQHandler#handleIQ(org.xmpp.packet.IQ)
*/
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException
{
IQ reply = IQ.createResultIQ(packet);
Element groups = packet.getChildElement();//1
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;
}
}
3.打包插件
a.第一次打包需配置ant工具。在环境变量中,
添加 path=C:\Program Files\Java\jdk1.5.0_09\;D:\Program Files\MyEclipse6.5\eclipse\plugins\org.apache.ant_1.7.0.v200706080842\bin (如果原来有path,在变量值的尾部添加半角分号 ; 然后再加入上面的jdk和ant路径 )
b.编译
进入cmd,进入 openfire/build所在目录:输入命令:ant plugins 等待执行完,而且没有报错,说明打包成功。在openfire3.6.3\target\openfire\plugins目录下就生成了itvi.jar
4.测试
启动openfire ,如发现控制台输出“测试插件启动成功”,说明插件开发成功。