openfire3.7插件开发方法 (2011-05-23 20:06)
分类: openfire
作用:初始化时在控制台打印测试消息。
开发环境:winxp/eclipse3.6/jdk1.5&1.6/mysql5.0/openfire3.7
开发步骤:
1. 布署openfire3.7源码
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);
- info = new IQHandlerInfo("query", "jabber:iq:roster");
- //这里我们对”联系人”的IQ进入监控
- }
- /*
- * (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 (true)
- {
- System.out.println("非法的请求类型111111");
- }
- if (!IQ.Type.get.equals(packet.getType()))
- {
- System.out.println("非法的请求类型");
- reply.setChildElement(groups.createCopy());
- System.out.println("返回的最终XML-SetError前"+reply.toXML());
- reply.setError(PacketError.Condition.bad_request);
- System.out.println("返回的最终XML-SetError后"+reply.toXML());
- 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 FilesJavajdk1.5.0_09;D:Program FilesMyEclipse6.5eclipsepluginsorg.apache.ant_1.7.0.v200706080842bin (如果原来有path,在变量值的尾部添加半角分号 ; 然后再加入上面的jdk和ant路径 )
b.编译
进入cmd,进入 openfire/build所在目录:输入命令:ant plugins 等待执行完,而且没有报错,说明打包成功。在openfire3.6.3targetopenfireplugins目录下就生成了itvi.jar
也可以单独的进行写buid.xml 实现对这个jar进行打包 然后拷贝到 target plugins 下
4.测试
启动openfire ,如发现控制台输出“测试插件启动成功”,说明插件开发成功。
注:这是第一次开发插件,纠结中成功!但是控制台没有打印出“测试插件启动成功”信息!待研究。。。
问题解决:
主要是控制台不打印汉字,改为英语,一切OK!以下是运行效果: