openfire :openfire 不同類型插件的開發示例


新建一個自己的Java project工程,添加的jar包如下:

將jasper-compiler.jar、jasper-runtime.jar、servlet.jar添加到新建的工程中。如果沒有jar先不要急,看下面的步驟:

下載后的openfire源碼目錄是這樣的

clip_image002

如果你有ant工具可以用dos命令行的方式,直接運行build目錄中的ant腳本,運行腳本后,你會發現有一個target的目錄。該目錄如下:

clip_image004

在lib目錄中可以找到我們需要的jar文件了,將openfire.jar也添加到你的工程中。

如果你沒有安裝ant你可以用MyEclipse,將openfire源碼中的build、documentation、resources目錄復制到一個java Project中,然后在MyEclipse中運行src中的build.xml ant腳本就會出現和上面一樣的文件目錄。

建議將你的MyEclipse中的openfire源碼工程目錄設置成這樣的

clip_image006

其中,src/plugins/tree是我自己寫的插件,現在暫時可以無視。而target就是我們需要的,里面存放了openfire的配置和需要的jar包。Work是工作目錄,是一個完整的openfire服務器。如果你還沒有下載openfire服務器的話,可以用這個服務器。

了解openfire源碼中的插件

我們找一個插件目錄看看,主要看看里面的結構,目錄結構很重要。因為我們將寫好的插件打成jar包后,打包的jar的目錄有一定規范結構,不能隨便建立其他目錄。

clip_image008

這是一個userservice的插件,在src/Java中是我們的插件源代碼web目錄中則是前端的頁面,其中web-custom.xml是配置當前插件的servlet服務器UserServiceServlet配置;changelog.html是修改日志;logo_small.gif是插件圖標;plugin.xml是我們配置插件的文件,這個很重要(在這里先提示下);

二、開發簡單插件

工程現在的目錄機構如下

clip_image010

1、 建立自己的插件類,SamplePlugin.java,里面簡單的寫點內容。

package com.hoo.server.plugin;

import java.io.File;

import org.jivesoftware.openfire.XMPPServer;

import org.jivesoftware.openfire.container.Plugin;

import org.jivesoftware.openfire.container.PluginManager;

/**

* <b>function:</b> openfire server plugin sample

* @author hoojo

* @createDate 2013-2-28 下午05:48:22

* @file SamplePlugin.java

* @package com.hoo.server.plugin

* @project OpenfirePlugin

* @blog http://blog.csdn.net/IBM_hoojo

* @email hoojo_@126.com

* @version 1.0

*/

public class SamplePlugin implements Plugin {

private XMPPServer server;

@Override

public void initializePlugin(PluginManager manager, File pluginDirectory) {

server = XMPPServer.getInstance();

System.out.println("初始化…… 安裝插件!");

System.out.println(server.getServerInfo());

}

@Override

public void destroyPlugin() {

System.out.println("服務器停止,銷毀插件!");

}

}

比較簡單,如果你將插件安裝在openfire服務器上的時候,啟動服務器一個可以看到初始化的內容,關閉服務器可以看到銷毀的內容。

2、 配置插件

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<!-- Main plugin class  這里是最重要滴,就是你的插件的全路徑-->
<class>com.hoo.server.plugin.SamplePlugin</class>
 
         
<!-- Plugin meta-data -->
<name>SimplePlugin</name>
<description>This is the my sample plugin.</description>
<author>hoojo</author>
 
         
<version>1.0</version>
<date>28/02/2013</date>
<url>http://localhost:9090/openfire/plugins.jsp</url>
<minServerVersion>3.4.1</minServerVersion>
<licenseType>gpl</licenseType>
 
         
<adminconsole>
</adminconsole>
</plugin>

 

注意上面的class的配置,那個配置是最為重要的,配置的是插件的全路徑;name是插件的名稱,安裝后的插件名稱;author是插件作者;lincenseType是協議;adminconsole是配置插件關聯的頁面的;稍后再講!

3、 可部署的插件包jar的目錄結構

這個很重要,目錄結構將決定你插件 發布的成敗。

在編寫命令之前,我們可以看看openfire服務器中已經安裝的插件的jar包的目錄結構,到時候我們也要打包成那樣的結構才行的。必須打包成這樣的目錄結構。

在我機器中的openfire服務器中,插件目錄在C:\Program Files\openfire\plugins,里面有一個search.jar插件。提示:當你將一個可以安裝的jar安裝在openfire后,會被openfire解壓成目錄結構。就向JavaEE中的war包發布的應用服務器中的效果一樣的。

打成可部署的插件jar包(相當於發布的應用服務器的目錄結構)的search.jar目錄結構如下:

clip_image012

首先看看文件命名,

(1)search.jar:打包的插件的名稱

(2)i18n: 118n國際化文件,它主要是我們在插件中的jsp和Java程序中的國際化配置。國際化的配置文件是以插件名稱開頭_118n.properties或_118n_language.properties;

(3)lib:  lib目錄是存放插件的src目錄的java代碼編譯后打包的jar,以及jsp編譯成servlet的class打包后的jar;帶有*-jspc.jar是web目錄下的jsp編譯成servlet后的class打成的包文件,都是以插件名稱開頭;WEB-INF/web.xml配置的是*-jspc.jar中的class文件;

(4)web:web目錄存放jsp、圖片、web.xml內容

(5)plugin.xml文件名固定的,里面是配置插件的xml內容。

其他的文件都是根目錄的;

對照上面插件包的jar,我們看看實際開發中的目錄結構

clip_image014

稍提醒下,如果你的插件中包含servlet,那你需要將它配置在web目錄下的WEB-INF/web-custom.xml目錄中;這個在以后會經常用到的,比如你提供一個接口給外部程序調用的情況下。目錄結構參考:

clip_image016

UserServiceServlet配置在web-custom.xml目錄中

4、 編寫ant命令,打可部署jar包。如果你不懂ant命令也沒關系,你總知道java的基本常用的dos命令。只不過ant就是將dos轉換成一個可重復多次調用的命令行。

在工程的根目錄中新建一個build目錄,新建

build.xml

<project name="Webapp Precompilation" default="openfire-plugins" basedir=".">
<property file="build.properties" />
 
         
<!-- java servlet相關文件編譯jar存放位置 -->
<property name="java.jar.dir" value="${webapp.path}/java-dist"/>
<!-- jsp servlet編譯后jar存放位置 -->
<property name="jsp.jar.dir" value="${webapp.path}/jsp-dist/lib"/>
 
         
<!-- 定義java servlet和jsp servlet的jar包名稱 -->
<property name="java.jar" value="${java.jar.dir}/plugin-${plugin.name}.jar"/>
<property name="jsp.jar" value="${jsp.jar.dir}/plugin-${plugin.name}-jsp.jar"/>
 
         
<!-- jsp servlet配置到web.xml中 -->
<property name="plugin.web.xml" value="${webapp.path}/jsp-dist/web.xml"/>
 
         
<!-- 編譯jsp 並生成相關jar、xml文件 -->
<target name="jspc">
 
         
<taskdef classname="org.apache.jasper.JspC" name="jasper2">
<classpath id="jspc.classpath">
<pathelement location="${java.home}/../lib/tools.jar" />
<fileset dir="${tomcat.home}/bin">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/server/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar" />
</fileset>
<!--
<fileset dir="D:/Workspace/openfire/build/lib">
<include name="**/*.jar" />
</fileset-->
</classpath>
</taskdef>
 
         
<!-- 編譯jsp->servlet class -->
<jasper2 javaEncoding="UTF-8" validateXml="false"
uriroot="${plugin.path}/web"
outputDir="${webapp.path}/jsp-dist/src"
package="com.hoo.openfire.plugin.${plugin.name}" />
 
         
<!-- 編譯后的servlet class 配置到web.xml文件中 -->
<jasper2
validateXml="false"
uriroot="${plugin.path}/web"
outputDir="${webapp.path}/jsp-dist/src"
package="com.hoo.openfire.plugin.${plugin.name}"
webXml="${plugin.web.xml}"/>
</target>
 
         
<!-- 編譯jsp 並將其打jar包 -->
<target name="compile">
 
         
<mkdir dir="${webapp.path}/jsp-dist/classes" />
<mkdir dir="${webapp.path}/jsp-dist/lib" />
<mkdir dir="${webapp.path}/jsp-dist/src" />
 
         
<javac destdir="${webapp.path}/jsp-dist/classes" optimize="off"
encoding="UTF-8" debug="on" failonerror="false"
srcdir="${webapp.path}/jsp-dist/src" excludes="**/*.smap">
<classpath>
<pathelement location="${webapp.path}/jsp-dist/classes" />
<fileset dir="${webapp.path}/jsp-dist/lib">
<include name="*.jar" />
</fileset>
<pathelement location="${tomcat.home}/common/classes" />
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar" />
</fileset>
<pathelement location="${tomcat.home}/shared/classes" />
<fileset dir="${tomcat.home}/shared/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/bin">
<include name="*.jar" />
</fileset>
</classpath>
<include name="**" />
<exclude name="tags/**" />
</javac>
 
         
<jar jarfile="${jsp.jar}" basedir="${webapp.path}/jsp-dist/classes" />
</target>
 
         
<!-- 將java servlet打包成jar -->
<target name="java-jar">
<mkdir dir="${java.jar.dir}"/>
<jar jarfile="${java.jar}">
<fileset dir="${webapp.path}/bin" includes="**/*.class"/>
</jar>
</target>
 
         
<!-- 生成可部署的插件包 -->
<target name="plug-jar">
<!-- 插件插件包相關lib、 web目錄 -->
<mkdir dir="${webapp.path}/${plugin.name}/lib"/>
<mkdir dir="${webapp.path}/${plugin.name}/web/WEB-INF"/>
 
         
<!-- 復制jsp servlet的jar和java servlet的相關jar包到插件包的lib目錄下 -->
<copy file="${java.jar}" todir="${webapp.path}/${plugin.name}/lib"/>
<copy file="${jsp.jar}" todir="${webapp.path}/${plugin.name}/lib"/>
 
         
<!-- 將相關的圖片、幫助文檔、修改日志等文件復制到插件目錄下 -->
<copy todir="${webapp.path}/${plugin.name}">
<fileset dir="${plugin.path}" includes="*.*"/>
</copy>
<copy todir="${webapp.path}/${plugin.name}/web">
<fileset dir="${plugin.path}/web">
<include name="*"/>
<include name="**/*.*"/>
<exclude name="**/*.xml"/>
<exclude name="**/*.jsp"/>
</fileset>
</copy>
<!-- jsp servlet的web復制到插件目錄下 -->
<copy file="${plugin.web.xml}" todir="${webapp.path}/${plugin.name}/web/WEB-INF"/>
<copy todir="${webapp.path}/${plugin.name}/web">
<fileset dir="${plugin.path}/web" includes="**/*.xml"/>
</copy>
<!-- 將國際化相關資源文件復制到插件目錄下
<copy file="${webapp.path}/bin/i18n" todir="${webapp.path}/${plugin.name}"/>
-->
<!-- 產生可部署插件包 -->
<jar jarfile="${webapp.path}/${plugin.name}.jar">
<fileset dir="${webapp.path}/${plugin.name}" includes="**/**"/>
</jar>
</target>
 
         
<!-- 生成沒有Web資源的可部署插件包 -->
<target name="java-plug-jar">
<!-- 插件插件包相關lib、 web目錄 -->
<mkdir dir="${webapp.path}/${plugin.name}/lib"/>
 
         
<!-- 復制java servlet的相關jar包到插件包的lib目錄下 -->
<copy file="${java.jar}" todir="${webapp.path}/${plugin.name}/lib"/>
 
         
<!-- 將相關的圖片、幫助文檔、修改日志等文件復制到插件目錄下 -->
<copy todir="${webapp.path}/${plugin.name}">
<fileset dir="${plugin.path}" includes="*.*"/>
</copy>
 
         
<!-- 產生可部署插件包 -->
<jar jarfile="${webapp.path}/${plugin.name}.jar">
<fileset dir="${webapp.path}/${plugin.name}" includes="**/**"/>
</jar>
</target>
 
         
<!-- 清理生成的文件 -->
<target name="clean">
<delete file="${webapp.path}/${plugin.name}.jar"/>
<delete dir="${webapp.path}/${plugin.name}"/>
<delete dir="${webapp.path}/jsp-dist"/>
<delete dir="${webapp.path}/java-dist"/>
</target>
 
         
<target name="all" depends="clean,jspc,compile"/>
 
         
<target name="openfire-plugin" depends="jspc,java-jar"/>
 
         
<target name="openfire-plugins" depends="all,java-jar,plug-jar"/>
 
         
<target name="openfire-plugin-java" depends="clean,java-jar,java-plug-jar"/>
</project>

build.properties文件內容

#tomcat home
tomcat.home=D:/tomcat-5.0.28/tomcat-5.0.28
webapp.path=D:/Workspace/OpenfirePlugin
 
         
plugin.name=sample
plugin.path=D\:/Workspace/OpenfirePlugin/src/plugins/sample

注意:這里我沒有編寫編譯java代碼到class的步驟,我是直接使用MyEclipse自動編譯的bin/class的。如果你沒有用MyEclipse或Eclipse,那么你需要將src中的Java代碼編譯class。

這里需要配置tomcat的目錄,我這里是5.0.28的版本。我用tomcat6有些問題,這里主要是用tomcat中的lib庫,幫助我們編譯jsp。還需要配置你當前工程的所在目錄,也就是工程在Eclipse中的目錄位置。最后你需要配置插件的名稱和插件在工程中的所在目錄,這個是在打包的時候,需要將其他的html、image、xml等資源導入的jar內。

因為這里的插件是不帶jsp的,所以我們執行clean、java-jar、java-plugin-jar。也就是openfire-plugin-java這個命令即可。執行命令后,你可以看到工作空間的工程目錄下多了目錄和文件。見圖:

clip_image018

java-dist目錄里面的就是src/plugin/sample目錄中的java代碼打成的jar包。具體你可以用zip打開看看。

sample就是我們的插件目錄,和sample.jar中的內容是一模一樣的。

sample.jar就是將sample目錄打成jar包。

5、 發布插件

發布插件有2種方式

第一種:直接將插件放置在openfire服務器的plugins目錄下。我的是在:C:\Program Files\openfire\plugins目錄。重起openfire后你可以看到控制台輸出我們插件中輸出的內容,並且在C:\Program Files\openfire\plugins目錄中可以看到該目錄下多了一個sample的目錄(openfire可以自動解壓jar包)。

clip_image020

當你在關閉服務器的瞬間,也會打印銷毀插件的消息。

第二種:在openfire啟動的情況下,訪問http://localhost:9090/plugin-admin.jsp頁面,點擊頁面下方的upload plugin完成插件上傳操作。

插件按照成功后,訪問http://localhost:9090/plugin-admin.jsp頁面你就可以看到安裝好的插件了。

clip_image022

至此,不帶jsp頁面的簡單插件就編寫部署成功了。

三、開發帶jsp、PluginServlet的插件

有些插件是單純的繼承Plugin或Handler什么的,但有些是需要jsp頁面和Servlet的。下面我們就來開發帶jsp和servlet的插件。

在之前的目錄下添加文件,目錄結構如下:

clip_image024

1、 首先建立一個SampleServlet的文件,內容如下

package com.hoo.server.plugin;
 
         
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
         
/**
* <b>function:</b> sample servlet
* @author hoojo
* @createDate 2013-3-4 下午04:15:20
* @file SampleServlet.java
* @package com.hoo.server.plugin
* @project OpenfirePlugin
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class SampleServlet extends HttpServlet {
 
         
private static final long serialVersionUID = -5404916983906926869L;
 
         
@Override
public void init() throws ServletException {
super.init();
}
 
         
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doGet(request, response);
 
         
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
System.out.println("請求SampleServlet GET Method");
out.print("請求SampleServlet GET Method");
out.flush();
}
 
         
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doPost(request, response);
 
         
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
System.out.println("請求SampleServlet GET Method");
out.print("請求SampleServlet POST Method");
out.flush();
}
 
         
@Override
public void destroy() {
super.destroy();
}

2、 在當前插件根目錄添加web目錄,在目錄下建立WEB-INF目錄,添加web-custom.xml文件(文件名應該是固定的)。在里面配置我們的servlet。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 
         
<servlet>
<servlet-class>com.hoo.server.plugin.SampleServlet</servlet-class>
<servlet-name>SampleServlet</servlet-name>
</servlet>
 
         
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app>

當插件發布后你可以通過用:http://127.0.0.1:9090/plugins/sample/servlet 就可以訪問到這個servlet了。但我發現我們只需用http://127.0.0.1:9090/plugins/sample也是可以訪問到的。好像openfire會自動找到我們插件目錄下的servlet配置。

注意:這里的http://127.0.0.1:9090/plugins/是固定的,至少plugins是固定的。所有的插件都在plugins目錄下訪問的。如果你想知道為什么,你可以看看openfire源碼下的web.xml,具體目錄路徑在/openfire/src/web/WEB-INF/web.xml。里面有一個PluginServlet是過來plugin的配置的。

3、 在web目錄下添加jsp文件,文件名是插件名稱-自定義名稱.jsp(建議規范命名)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>hello world: 你好openfire</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="pageID" content="sample-service"/>
</head>
 
         
<body>
<h3>hello world jsp!! <a href="/plugins/sample/servlet">SampleServlet</a></h3>
<div class="jive-contentBoxHeader">jive-contentBoxHeader</div>
<div class="jive-contentBox">jive-contentBox</div>
 
         
<div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
<tr>
<th>&nbsp;sss</th>
<th nowrap>a</th>
<th nowrap>b</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">asdf</td>
<td align="center">asdf</td>
<td align="center">asdf</td>
</tr>
<tr class="jive-even">
<td align="center">asdf</td>
<td align="center">asdf</td>
<td align="center">asdf</td>
</tr>
<tr class="jive-odd">
<td align="center">asdf</td>
<td align="center">asdf</td>
<td align="center">asdf</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

其中最重要的一點就是:<meta name="pageID" content="sample-service"/>這個pageID。這里的是固定的,后面的content對應我們plugin.xml的內容(等下看看plguin.xml的配置)。然后可以適當的看下里面table的 屬性和樣式,因為很多時候會在jsp中顯示內容,且用table布局的。

4、 改下之前的plugin.xml的配置,配置組件在openfire 管理員控制台的哪個地方顯示,以及顯示的頁面。

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<!-- Main plugin class  這里是最重要滴,就是你的插件的全路徑-->
<class>com.hoo.server.plugin.SamplePlugin</class>
 
         
<!-- Plugin meta-data -->
<name>SimplePlugin</name>
<description>This is the my sample plugin.</description>
<author>hoojo</author>
 
         
<version>1.0</version>
<date>28/02/2013</date>
<url>http://localhost:9090/openfire/plugins.jsp</url>
<minServerVersion>3.4.1</minServerVersion>
<licenseType>gpl</licenseType>
 
         
<adminconsole>
<tab id="tab-server">
<sidebar id="sidebar-server-settings">
<item id="sample-service" name="Sample Service" url="sample-service.jsp"
description="Click is trigger sample plugin" />
</sidebar>
</tab>
</adminconsole>
</plugin>

這里主要就是adminconsole這里面的配置。首先tab-server應該是在管理員控制台頁面的服務器菜單中顯示;sidebar中的的id配置固定這樣寫即可;item中的id(sample-service)對應的就是上面的sample-service.jsp的<meta name="pageID" content="sample-service"/>的content內容;item的url對應的是我們寫的jsp頁面;name是插件的菜單名稱。也就是說在管理員控制台頁面中的服務器菜單下增加一個sample service的菜單,打開的頁面是sample-service.jsp頁面。

5、 運行ant腳本,打包發布插件。在上一章節有完整ant腳本的,運行build.xml中的這個openfire-plugins命令即可打包。然后將打好包的sample.jar發布到openfire的plugins目錄下即可。

打包后的jar插件目錄結構如下:

clip_image026

啟動openfire后,在openfire管理員控制台頁面的服務器->服務器設置中就可以看到Sample Service插件了。

clip_image028

點擊Sample Servlet就可以看到openfire控制台打印請求的文字信息。

 

5.openfire(2)協議插件開發

昨天說了怎么配置openfire的開發環境。今天寫一點openfire的插件開發。我這里做了一個例子主要是針對於XMPP的通信。后邊會說一點smack和openfire通信的實現。

在openfire的源碼里有很多插件。我這里實際就是拷貝了其中的一個插件。重名了一下名字。目錄結構如下:

 

貌似圖片傳不上來了。如果看不到圖,就看看源碼中的其他插件的例子。跟其他插件的目錄結構是一樣一樣的。

在這些文件里最重要的就是plugin.xml文件。因為有這個文件openfire才認識這個插件。在這個文件里會配置插件的入口類。我這里簡單寫了一個plugin.xml.示例如下。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <plugin>  
  4.     <class>org.yangzc.testplugin.TestPlugin</class>  
  5.     <name>test Plugin</name>  
  6.     <description>test Plugin descript</description>  
  7.     <author>yangzc</author>  
  8.     <version>1.0.0</version>  
  9.     <date>20/6/2011</date>  
  10.     <minServerVersion>3.7.0</minServerVersion>  
  11.     <databaseKey>testPlugin</databaseKey>  
  12.     <databaseVersion>0</databaseVersion>  
  13.       
  14.     <adminconsole>  
  15.     </adminconsole>  
  16. </plugin>  

class部分就是插件的實現類。

具體TestPlugin的實現。這里也有一個例子:

  1. package org.yangzc.testplugin;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.dom4j.Element;  
  6. import org.jivesoftware.openfire.IQHandlerInfo;  
  7. import org.jivesoftware.openfire.XMPPServer;  
  8. import org.jivesoftware.openfire.auth.UnauthorizedException;  
  9. import org.jivesoftware.openfire.container.Plugin;  
  10. import org.jivesoftware.openfire.container.PluginManager;  
  11. import org.jivesoftware.openfire.handler.IQHandler;  
  12. import org.xmpp.packet.IQ;  
  13. import org.xmpp.packet.PacketError;  
  14.   
  15. public class TestPlugin implements Plugin{  
  16.   
  17.     @Override  
  18.     public void destroyPlugin() {  
  19.     }  
  20.   
  21.     @Override  
  22.     public void initializePlugin(PluginManager manager, File pluginDirectory) {  
  23.         XMPPServer service = XMPPServer.getInstance();  
  24.         service.getIQRouter().addHandler(new MyIQHandler());  
  25.     }  
  26.   
  27.     class MyIQHandler extends IQHandler{  
  28.   
  29.         public static final String moduleName = "testPlugin";  
  30.           
  31.         private IQHandlerInfo info;  
  32.         public MyIQHandler(){  
  33.             super(moduleName);  
  34.             info = new IQHandlerInfo("group", "com:im:group");//設置監聽的命名空間  
  35.         }  
  36.           
  37.         @Override  
  38.         public IQHandlerInfo getInfo() {  
  39.             return info;//取得指定監聽命名空間的IQHandeler  
  40.         }  
  41.   
  42.         @Override  
  43.         public IQ handleIQ(IQ packet) throws UnauthorizedException {  
  44.             IQ reply = IQ.createResultIQ(packet);  
  45.             Element groups = packet.getChildElement();//取得客戶端發送過來的xml  
  46.               
  47.             if (!IQ.Type.get.equals(packet.getType())){  
  48.                 System.out.println("非法的請求類型");  
  49.                 reply.setChildElement(groups.createCopy());  
  50.                 reply.setError(PacketError.Condition.bad_request);  
  51.                 return reply;  
  52.             }  
  53.   
  54.             //String userName = StringUtils.substringBefore(packet.getFrom().toString(),"@");  
  55.             //GroupManager.getInstance().initElement(groups,userName);  
  56.             //reply.setChildElement(groups.createCopy());//2  
  57.             //System.out.println("返回的最終XML" reply.toXML());  
  58.             return reply;  
  59.         }  
  60.           
  61.     }  
  62. }  

在這個實現類中需要實現接口plugin。這個接口包含兩個方法。一個是初始化插件,一個是銷毀插件動作。

這個例子里在初始化插件的時候通過service添加了一個監聽。這里我理解為監聽。這個監聽可以監聽指定命名空間的消息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM