openfire 插件开发学习笔记

来源:互联网 发布:霸王别姬 段小楼 知乎 编辑:程序博客网 时间:2024/05/22 10:27

为了帮单位开发基于XMPP协议的的消息推送系统,近期研究了openfire的源代码,在此基础上做插件开发。以前没有做过JAVA程序,所以研究起来有点吃力。学习了几天后,参考了几篇别人的文章,终于把最基础的插件开发部分完成了,为了留作备忘,记录如下。

openfire的版本 3.8.2

在eclipse上开发。

开发插件最重要的部分是插件的目录结构和名称。包括plugin.xml文件和build.xml文件

学习的文章都是单独建立插件项目,编译完成后再拷贝到openfire源码相应目录,我这里不想这么做,因为觉得调试会比较麻烦,不知道怎么远程调试什么的,就直接在openfire的源码项目上开发插件。

研究了openfire原有的插件的目录,插件都在src/plugins/目录下。插件的源文件都在src/plugins/myplugin/src/java文件夹下

所以在项目上右键-》new>Source Folder,输入路径src/plugins/myplugin/src/java,其中myplugin是我要建的插件。

然后在生成的source folder 上右键-》new>package,输入要建立的包的名称,比如com.hnds.openfire.plugin

eclipse会自动生成相应的文件夹。

然后在建成的package上右键-》new >class,输入类名testplugin

然后在新建的testplugin.java文件中输入

package com.hnds.openfire.plugin;import java.io.File;import org.jivesoftware.openfire.XMPPServer;import org.jivesoftware.openfire.container.Plugin;import org.jivesoftware.openfire.container.PluginManager;public class testplugin implements Plugin{    private XMPPServer server; @Override public void initializePlugin(PluginManager manager, File pluginDirectory) {  // TODO Auto-generated method stub  server=XMPPServer.getInstance();  System.out.println("开始启动test plugin");  System.out.println(server.getServerInfo()); } @Override public void destroyPlugin() {  // TODO Auto-generated method stub  System.out.println("停止 test plugin"); }}


 

这个部分和其他的人写的没什么区别

然后在eclipse的package explorer中找到项目的src文件夹,找到对应的src/plugins/myplugin文件夹,在myplugin 文件夹下建立plugin.xml文件,不是在myplugin/src文件夹下建立哦。

<?xml version="1.0" encoding="UTF-8"?><plugin>    <!-- Main plugin class  这里是最重要滴,就是你的插件的全路径-->    <class>com.hnds.openfire.plugin.testplugin</class>     <!-- Plugin meta-data -->    <name>myplugin</name>    <description>This is the my test plugin.</description>    <author>hnds</author>    <version>1.0</version>    <date>12/02/2014</date>    <url>http://localhost:9090/openfire/plugins.jsp</url>    <minServerVersion>3.4.1</minServerVersion>    <licenseType>gpl</licenseType>     <adminconsole>                </adminconsole>        </plugin>


其中class里面就是包的路径com.hnds.openfire.plugin加上类名testplugin,

然后是修改build.xml

openfire的3.8.2的源代码的build.xml文件都已经写好了编译插件的ant代码,需要增加的部分就是增加一个要编译的插件的名称

<property name="plugin" value="myplugin"/>    <property name="plugin.src.dir" value="${src.dir}/plugins"/>    <property name="plugin.dev.dir" value=""/>

找到下面两行的property的位置,在上面加上最上面那一句,位置应该不是很重要,写在一起好看点。plugin的值就是插件名称myplugin

 <target name="plugin" description="build one plugin">        <mkdir dir="${plugin.dest.dir}"/>        <delete dir="${plugin.dev.dest.dir}/${plugin}"/>        <delete file="${plugin.dev.dest.dir}/${plugin}.jar"/>        <delete file="${plugin.dest.dir}/${plugin}.jar"/>        <buildplugin plugin="${plugin}" pluginsrc="${plugin.src.dir}"/>        <!-- Update/create target/openfire directory -->        <antcall target="openfireHome"/>    </target>


这是因为build.xml文件中有这么一段,用来单独编译一个插件的target

然后我们在build.xml文件上点击右键-》run as>ant build

然后在targets中选择plugin,描述是build one plugin,打上勾,然后run.插件编译完

然后选择项目,运行,在console中可以看见如下

Openfire 3.8.2 [2014-2-13 17:24:39]管理平台开始监听:  http://bh-20121122vapx:9090  https://bh-20121122vapx:9091开始启动test pluginorg.jivesoftware.openfire.spi.XMPPServerInfoImpl@12b1e53


这就说明编译成功了。

通过127.0.0.1:9090访问openfire后台,可以在插件分页中看到我们新建的插件已经在插件列表中运行了。

 

因为本人对java不是很熟,所以是简单的记录我的操作全过程,相比网上其他的文章我的主要是演示了

怎样直接在openfire源码项目中建立并编译插件,之后我还要继续在这个插件的基础上编写

openfire中记录聊天记录的插件,将会继续把心得记录下来。

 

 

0 0