Openfire插件开发

注意: Openfire生成插件的jar不是像Spark一样通过build.xml生成

 

而是在命令行 ant plugins 实现生成插件的

 

这篇生成插件的方法很好 我看到最好的

http://blog.chinaunix.net/uid-25434387-id-333577.html

 

作用:初始化时在控制台打印测试消息。

开发环境:winxp/eclipse3.6/jdk1.5&1.6/mysql5.0/openfire3.7

开发步骤:

1. 布署openfire3.7源码

2. src/plugins目录下新建文件夹itvi。并按下图的目录结构新建对应的文件。

plugin.xml文件代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <plugin>
  3. <class>org.jivesoftware.openfire.plugin.Itvi</class>
  4. <name>Itvi</name>
  5. <description>Broadcasts messages to users.</description>
  6. <author>openfireteam</author>
  7. <version>1.7.0</version>
  8. <date>2/19/2009</date>
  9. <url>http://openfireteam.blogjava.net</url>
  10. <minServerVersion>3.6.3</minServerVersion>
  11. </plugin>

Itvi.java文件代码如下:

  1. package org.jivesoftware.openfire.plugin;
  2. import org.dom4j.Element;
  3. import org.jivesoftware.openfire.IQHandlerInfo;
  4. import org.xmpp.packet.*;
  5. import org.jivesoftware.openfire.handler.IQHandler;
  6. import org.jivesoftware.openfire.auth.UnauthorizedException;
  7. /**
  8. * 处理客户端发来的IQ,并回送结果IQ
  9. *
  10. * @author Phoenix
  11. *
  12. * Mar 14, 2008 4:55:33 PM
  13. *
  14. * version 0.1
  15. */
  16. import org.dom4j.DocumentHelper;
  17. import org.dom4j.Element;
  18. import org.dom4j.QName;
  19. import org.jivesoftware.openfire.XMPPServer;
  20. import org.jivesoftware.openfire.container.Plugin;
  21. import org.jivesoftware.openfire.container.PluginManager;
  22. import org.jivesoftware.openfire.disco.IQDiscoInfoHandler;
  23. import org.jivesoftware.openfire.disco.IQDiscoItemsHandler;
  24. import org.jivesoftware.openfire.forms.DataForm;
  25. import org.jivesoftware.openfire.forms.FormField;
  26. import org.jivesoftware.openfire.forms.spi.XDataFormImpl;
  27. import org.jivesoftware.openfire.forms.spi.XFormFieldImpl;
  28. import org.jivesoftware.openfire.resultsetmanager.ResultSet;
  29. import org.jivesoftware.openfire.resultsetmanager.ResultSetImpl;
  30. import org.jivesoftware.openfire.user.User;
  31. import org.jivesoftware.openfire.user.UserManager;
  32. import org.jivesoftware.openfire.user.UserNotFoundException;
  33. import org.jivesoftware.util.*;
  34. import org.xmpp.component.Component;
  35. import org.xmpp.component.ComponentException;
  36. import org.xmpp.component.ComponentManager;
  37. import org.xmpp.component.ComponentManagerFactory;
  38. import org.xmpp.packet.IQ;
  39. import org.xmpp.packet.IQ.Type;
  40. import org.xmpp.packet.JID;
  41. import org.xmpp.packet.Packet;
  42. import org.xmpp.packet.PacketError;
  43. import org.xmpp.packet.PacketError.Condition;
  44. import java.io.File;
  45. import java.util.*;
  46. import java.util.Map.Entry;
  47. public class Itvi implements Plugin
  48. {
  49. private XMPPServer server;
  50. /*
  51. * (non-Javadoc)
  52. *
  53. * @see org.jivesoftware.openfire.container.Plugin#destroyPlugin()
  54. */
  55. public void destroyPlugin()
  56. {
  57. }
  58. /*
  59. * (non-Javadoc)
  60. *
  61. * @see org.jivesoftware.openfire.container.Plugin#initializePlugin(org.jivesoftware.openfire.container.PluginManager,
  62. * java.io.File)
  63. */
  64. public void initializePlugin(PluginManager manager, File pluginDirectory)
  65. {
  66. server = XMPPServer.getInstance();
  67. server.getIQRouter().addHandler(new ItviIQHander()); //
  68. System.out.println("测试插件启动成功");
  69. }
  70. }

最后输出语句“测试插件启动成功”在控制台显示乱码,将此改为""

ItviIQHander.java文件代码如下:

  1. package org.jivesoftware.openfire.plugin;
  2. import org.dom4j.Element;
  3. import org.jivesoftware.openfire.IQHandlerInfo;
  4. import org.xmpp.packet.*;
  5. import org.jivesoftware.openfire.handler.IQHandler;
  6. import org.jivesoftware.openfire.auth.UnauthorizedException;
  7. /**
  8. * 处理客户端发来的IQ,并回送结果IQ
  9. * @author Phoenix
  10. * Mar 14, 2008 4:55:33 PM
  11. * version 0.1
  12. */
  13. public class ItviIQHander extends IQHandler
  14. {
  15. private static final String MODULE_NAME = "group tree handler";
  16. private static final String NAME_SPACE = "com:im:group";
  17. private IQHandlerInfo info;
  18. public ItviIQHander()
  19. {
  20. super(MODULE_NAME);
  21. //info = new IQHandlerInfo("gruops", NAME_SPACE);
  22. info = new IQHandlerInfo("query", "jabber:iq:roster");
  23. //这里我们对”联系人”的IQ进入监控
  24. }
  25. /*
  26. * (non-Javadoc)
  27. *
  28. * @see org.jivesoftware.openfire.handler.IQHandler#getInfo()
  29. */
  30. @Override
  31. public IQHandlerInfo getInfo()
  32. {
  33. return info;
  34. }
  35. /*
  36. * (non-Javadoc)
  37. *
  38. * @see org.jivesoftware.openfire.handler.IQHandler#handleIQ(org.xmpp.packet.IQ)
  39. */
  40. @Override
  41. public IQ handleIQ(IQ packet) throws UnauthorizedException
  42. {
  43. IQ reply = IQ.createResultIQ(packet);
  44. Element groups = packet.getChildElement();//1
  45. if (true)
  46. {
  47. System.out.println("非法的请求类型111111");
  48. }
  49. if (!IQ.Type.get.equals(packet.getType()))
  50. {
  51. System.out.println("非法的请求类型");
  52. reply.setChildElement(groups.createCopy());
  53. System.out.println("返回的最终XML-SetError前"+reply.toXML());
  54. reply.setError(PacketError.Condition.bad_request);
  55. System.out.println("返回的最终XML-SetError后"+reply.toXML());
  56. return reply;
  57. }
  58. //String userName = StringUtils.substringBefore(packet.getFrom().toString(),"@");
  59. //GroupManager.getInstance().initElement(groups,userName);
  60. //reply.setChildElement(groups.createCopy());//2
  61. //System.out.println("返回的最终XML" reply.toXML());
  62. return reply;
  63. }
  64. }

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 ,如发现控制台输出“测试插件启动成功”,说明插件开发成功。

转自:http://www.blogjava.net/duanzhimin528/archive/2010/07/14/326146.html

注:这是第一次开发插件,纠结中成功!但是控制台没有打印出“测试插件启动成功”信息!待研究。。。

问题解决:

主要是控制台不打印汉字,改为英语,一切OK!以下是运行效果:

posted @ 2013-06-01 16:38  IamThat  阅读(495)  评论(0编辑  收藏  举报