Openfire源码分析

1. Openfire

Openfire是开源的实时协作服务器(RTC),它是基于公开协议XMPP(也成为Jabber)消息的。Openfire的核心功能可以概括为:连接管理、消息解析、消息路由、消息发送。

1.1 框架

Openfire核心功能由Module组成,上图中的各个模块绝大多数也是基于Module实现的。为了动态的管理这些Module,Openfire使用了自己的类加载器JiveClassLoader,这样就可以在不重新启动系统的前提下进行Module的安装、卸载和重新加载。

1.2 Module

Openfire在启动时就加载了所有的Module,这些Module提供了Openfire的核心功能。

所有Module都需要实现Module接口,该接口中定义了模块生命周期中需要调用的方法,如下图所示:

1.3 数据流

2. Openfire与MIMA

MINA是Apache组织下的一个项目,它可以帮助用户轻松的开发高性能和高扩展性的网络应用。MINA提供了一个抽象的、支持各种传输协议的(如TCP/IP、UDP/IP)、基于Java NIO的异步事件驱动的API。

Openfire的ConnectionHandler类继承了MINA的IoHandlerAdaper,他主要负责连接的创建、销毁,以及接收到XML数据包的投递。ConnectionHandler有三个子类,其中ClientConnectionHandler负责客户端与服务器端的连接,ComponentConnectionHandler负责组件与服务器端的连接。

3. Multi-User-Chat

多人聊天的消息是绑定在子域conference上的,对应的Component是MultiUserChatServiceImpl。在多人聊天中,涉及到的实体与关系有:用户、聊天室、用户与聊天室的关系,MultiUserChatServiceImpl就负责这些实体与关系的维护以及消息的投递。

3.1 MUCUser

MUCUser是参与群聊用户的一个抽象,用于与聊天服务器进行交互,比如发送和接收聊天信息。

3.2 MUCRole

定义了MUCRoom与MUCUser的关系。同一个MUCUser在不同的MUCRoom中可以有不同的角色。

3.3 MUCRoom

包含聊天室的基本信息及参与人、聊天记录等信息。

3.4 MultiUserChatServiceImpl

管理所有的聊天室,并负责消息的分发。

3.5 时序图

4. Pub-Sub

发布订阅消息是绑定在子域pubsub上的,对应的模块是PubSubModule。

4.1 Node

发布订阅系统中的虚拟节点,该节点可以接收或者发布各类信息或通知。在其他发布订阅系统中常被称为TOPIC。

CollectionNode可以包含子节点,子节点的类型可以是CollectionNode或者LeafeNode,该类型的节点不能作为发布订阅的目标节点;LeafeNode不能包含子节点,它可以作为发布订阅的目标节点。

4.2 NodeAffiliate

定义了用户与节点之间的关系,包括:ower、publisher、none、outcast。

4.3 NodeSubscription

一个用户可以订阅某个节点多次,每次订阅都使用不同的配置(比如过滤关键字不同),NodeSubscription就是描述这些订阅和配置的。如果一个消息满足用户的多个订阅,则只有一份消息被分发给该用户,而不是多个。

4.4 PubSubModule

PubSubModule负责节点的管理、发布订阅关系的维护、消息的分发。

4.5 PubSubEngine

负责处理发布订阅数据包。

4.6 时序图

5 插件开发

在Openfire中,插件可以直接使用所有的Openfire API,这为插件的功能提供了极大的灵活性。

5.1 注册为组件

注册一个插件作为一个组件,组件可以接收发送给特定子域的所有数据包。例如上述的组件MultiUserChatServiceImpl就可以接收到所有发送给conference子域的数据包。所有的组件由InternalComponentManager Module进行管理。

在TestPlugin的initializePlugin方法中初始化TestComponent实例,并调用InternalComponentManager将初始化的实例注册到目标子域名中,这样TestComponent组件就能接收到并处理发送到目标子域的消息。

5.2 注册为拦截器

注册一个组件为拦截器,那该组件可以接收所有的数据包,并且可以随意的抛弃他们。

在TestPlugin中的initializePlugin方法中初始化该组件,并调用InterceptionManager将该拦截器注册到全局拦截器中。这样所有的消息都会经由方法interceptPacket进行处理。

5.3 注册为IQHandler

将插件注册为IQHandler,则该插件关联命名空间的数据包将交予该插件进行处理。

在TestIQHandlerPlugin中的initializePlugin方法中,初始化TestIQHandler,并注册到IQRouter中。IQHandlerInfo中声明了该IQHandler处理的IQ消息的命名空间。

 

Openfire 插件开发指南

英文版本链接:http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/plugin-dev-guide.html

介绍

插件加强了Openfire的功能,这篇文档指导开发者如何创建一个插件。

插件的结构

插件位于openfireHome目录下的plugins目录中,当一个插件以一个JAR或者WAR文件部署时,他会被自动的解压。一个插件的目录结构如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[text]  view plain copy
  1. myplugin/  
  2.  |- plugin.xml      <- 插件的声明文件  
  3.  |- readme.html     <- 可选 插件的自述文件,可以展示给使用用户  
  4.  |- changelog.html  <- 可选 插件的更新日志,可以展示给使用用户  
  5.  |- logo_small.gif  <- 可选 插件的小图标(16x16) (也可以是png文件)  
  6.  |- logo_large.gif  <- 可选 插件的大图标(32x32) (也可以是png文件)  
  7.  |- classes/        <- 插件的资源文件(即class文件或properties文件)  
  8.  |- database/       <- 可选 插件需要的数据库脚本  
  9.  |- i18n/           <- 可选 插件国际化所需要的i18n文件  
  10.  |- lib/            <- 插件所需要的lib文件 (JAR files)  
  11.  |- web             <- Admin集成所需要的资源  
  12.      |- WEB-INF/  
  13.          |- web.xml           <- 生成的web.xml文件,包括编译后的JSP实例  
  14.          |- web-custom.xml    <- 可选 用户定义的web.xml,包含定制的servlets  
  15.      |- images/  

如果插件需要在Openfire的控制台添加页面,则需要创建web目录。下面将详细进行描述。

plugin.xml指定了插件的主类,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[xml]  view plain copy
  1. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><?xml</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">version</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"1.0"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">encoding</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"UTF-8"</span><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">?></span></span>  
  2. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><plugin<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  3.     <span style="font-family:inherit; font-size:12px; color:#808080; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px"><!-- Main plugin class --></span>  
  4.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><class<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>org.example.ExamplePlugin<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></class<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  5.    
  6.     <span style="font-family:inherit; font-size:12px; color:#808080; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px"><!-- Plugin meta-data --></span>  
  7.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><name<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>Example Plugin<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></name<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  8.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><description<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>This is an example plugin.<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></description<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  9.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><author<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>Jive Software<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></author<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  10.    
  11.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><version<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>1.0<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></version<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  12.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><date<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>07/01/2006<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></date<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  13.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><url<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>http://www.igniterealtime.org/projects/openfire/plugins.jsp<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></url<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  14.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><minServerVersion<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>3.0.0<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></minServerVersion<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  15.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><licenseType<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>gpl<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></licenseType<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  16.    
  17.     <span style="font-family:inherit; font-size:12px; color:#808080; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px"><!-- Admin console entries --></span>  
  18.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><adminconsole<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  19.         <span style="font-family:inherit; font-size:12px; color:#808080; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px"><!-- More on this below --></span>  
  20.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></adminconsole<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  21. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></plugin<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  

plugin.xml文件中的元数据包括:

  • name — 插件的名称
  • description — 插件的描述
  • author — 插件的作者
  • version — 插件的版本
  • date — 插件发布的日期,日期的格式必须是MM/dd/yyyy,比如07/01/2006
  • url — 关于该插件更多信息的地址
  • minServerVersion — 允许该插件最小的Openfire版本号(从Openfile 2.1.2版本开始支持),如果Openfire的版本号比需要的小,则插件不会被启动
  • databaseKey — 如果插件需要自己的数据库表,databaseKey将被用于数据库脚本的名称(通常与插件名称相同)。可被支持的数据库脚本文件需要放置在database目录下。例如,给定的databaseKey为foo,脚本文件可以为“foo_mysql.sql”、“foo_oracle.sql”等。我们建议表名增加前缀“of”,避免与其他应用使用的表名产生冲突。databaseKey可以作为ofVersion表的一条记录的key,这样脚本的版本信息可以被跟踪,例如:
    1
    
    [sql]  view plain copy
    1. <span style="font-family:inherit; font-size:12px; color:#993333; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">INSERT</span> <span style="font-family:inherit; font-size:12px; color:#993333; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">INTO</span> ofVersion <span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span>name<span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">,</span> version<span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span> <span style="font-family:inherit; font-size:12px; color:#993333; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">VALUES</span> <span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span><span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">'foo'</span><span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">,</span> <span style="font-family:inherit; font-size:12px; color:#cc66cc; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">0</span><span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span>;  
  • databaseVersion — 数据库脚本的版本(如果使用数据库脚本的话)。一个新插件的数据库脚本文本从0开始。如果将来该插件的数据库脚本需要更新,则这些更新可以在database/upgrade子目录下定义。例如,目录database/upgrade/1和database/upgrade/2都可以包含脚本foo_myslq.sql和foo_oracle.sql,这些脚本中包含每个版本相关联的数据库修改。每个脚本都会更新ofVersion表中的版本信息。
    1
    
    [sql]  view plain copy
    1. <span style="font-family:inherit; font-size:12px; color:#993333; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">UPDATE</span> ofVersion <span style="font-family:inherit; font-size:12px; color:#993333; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">set</span> version<span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">=</span><span style="font-family:inherit; font-size:12px; color:#cc66cc; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">1</span> <span style="font-family:inherit; font-size:12px; color:#993333; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">where</span> name<span style="font-family:inherit; font-size:12px; color:#66cc66; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">=</span><span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">'foo'</span>;  
  • parentPlugin — 父插件的名称(对于foo.jar插件可以使用名称foo)。当一个插件有一个父插件,父插件的类加载器将被直接使用,而不需要创建一个新的类加载器。这将让插件能更紧密的合作。没有父插件的配合,子插件将不能正常使用。
  • licenseType — 指明该插件使用的许可协议,有效的值有:
    • commercial:该插件是在商业许可协议下发布的
    • gpl:该插件是在GNU公共许可协议下(GPL)发布的
    • apache:该插件是在Apache协议下发布的
    • internal:该插件只能用于组织内部,且不允许扩散
    • other:该插件的发布许可协议不包含在上述列表中,详情可见插件中的自述文件

    如果许可类型没有指定,则会被设置为other。

插件中的几个附加文件可为最终的使用用户提供额外的信息(这些文件都位于插件的根目录):

  • readme.html — 可选 插件的自述文件,将展现给最终用户
  • changelog.html — 可选 插件的更新李日,将展现给最终用户
  • logo_small.png — 可选 插件的小图标(16×16),也可以是.gif文件
  • logo_large.png — 可选 插件的大图标(32×32),也可以是.gif文件

插件类必须实现Plugin接口,他有一个默认的构造方法(无参数)。Plugin接口有方法用于初始化和销毁插件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[java]  view plain copy
  1. <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">package</span> <span style="font-family:inherit; font-size:12px; color:#06699; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">org.example</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
  2.    
  3. <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">import</span> <span style="font-family:inherit; font-size:12px; color:#06699; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">org.jivesoftware.openfire.container.Plugin</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
  4. <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">import</span> <span style="font-family:inherit; font-size:12px; color:#06699; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">org.jivesoftware.openfire.container.PluginManager</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
  5.    
  6. <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">import</span> <span style="font-family:inherit; font-size:12px; color:#06699; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">java.io.File</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
  7.    
  8. <span style="font-family:inherit; font-size:12px; color:#0800; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:bold; vertical-align:baseline; outline-width:0px">/** 
  9.  * A sample plugin for Openfire. 
  10.  */</span>  
  11. <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">public</span> <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">class</span> ExamplePlugin <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">implements</span> Plugin <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">{</span>  
  12.    
  13.     <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">public</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">void</span> initializePlugin<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span>PluginManager manager, <a href="http://www.google.com/search?hl=en&q=allinurl%3Afile+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="border-width:0px; margin:0px; padding:0px; color:rgb(51,51,51); font-family:inherit; font-size:12px; font-style:inherit; font-weight:inherit; text-decoration:underline; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#03399; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">File</span></a> pluginDirectory<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span> <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">{</span>  
  14.         <span style="font-family:inherit; font-size:12px; color:#666666; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px">// Your code goes here</span>  
  15.    
  16.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">}</span>  
  17.    
  18.     <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">public</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">void</span> destroyPlugin<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span> <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">{</span>  
  19.         <span style="font-family:inherit; font-size:12px; color:#666666; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px">// Your code goes here</span>  
  20.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">}</span>  
  21. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">}</span>  

插件开发的最优方法

当选择一个包名作为你的插件名称时,我们推荐你选择对你或者你的组织来说有特色的名称,这将尽可能的避免可能的冲突。例如,所有人都是用org.example.PluginName,即使PluginName不同,你可能会碰到类名冲突的问题,特别是在集群运行时。

配置管理后台

插件可以再管理后台添加Tab标签、片段和页面,通过下面的几个步骤可以完成配置:

  • 在plugin.xml文件中添加<adminconsole/>
  • JSP文件必须已被编译,且添加到插件的classpath中。web.xml文件中包含被编译的JSP servlet实体,且被放置在插件的web目录下。注意:Openfile的build脚本可以完成JSP文件的编译和web.xml文件的创建,下面将会详细描述。
  • JSP页面中所需要的图片都必须放置在web/images/目录下,且只允许为GIF何PNG文件。

plugin.xml文件中的<adminconsole/>片段用于在管理后台框架中添加一个Tab标签、片段和页面。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[xml]  view plain copy
  1. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><?xml</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">version</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"1.0"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">encoding</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"UTF-8"</span><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">?></span></span>  
  2. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><plugin<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  3.     <span style="font-family:inherit; font-size:12px; color:#808080; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px"><!-- Main plugin class --></span>  
  4.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><class<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>org.example.ExamplePlugin<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></class<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  5.    
  6.     <span style="font-family:inherit; font-size:12px; color:#808080; border-width:0px; margin:0px; padding:0px; font-style:italic; font-weight:inherit; vertical-align:baseline; outline-width:0px"><!-- Admin console entries --></span>  
  7.    
  8.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><adminconsole<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  9.         <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><tab</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">id</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"mytab"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">name</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"Example"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">url</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"my-plugin-admin.jsp"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">description</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"Click to manage..."</span><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span>  
  10.             <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><sidebar</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">id</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"mysidebar"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">name</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"My Plugin"</span><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span>  
  11.                <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><item</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">id</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"my-plugin"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">name</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"My Plugin Admin"</span></span>  
  12. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">                   <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">url</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"my-plugin-admin.jsp"</span></span>  
  13. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">                   <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">description</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"Click to administer settings for my plugin"</span> <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">/></span></span>  
  14.             <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></sidebar<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  15.         <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></tab<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  16.    
  17.     <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></adminconsole<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  
  18. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></plugin<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  

在示例中,我们定义了一个新Tab标签“Example”,一个工具条片段“My Plugin”和一个页面“My Plugin Admin”。我们同样注册了一个页面my-plugin-admin.jsp。你可以重写一个已经存在的Tab标签,片段和页面,通过在中使用已经存在的id。

管理后台最优方法

当通过插件对Openfile的管理后台有所调整时,这里有几条最优方法可供参考。通用主题可以使插件无缝的集成:

  • 尽可能的集成到已经存在的Tab标签或者左侧导航,而不是创建自己的。只有在提供重大新功能时才创建新的Tab。
  • 不要在Tab标签、左侧导航中中使用“plugin”字样,例如使用“Gateway Settings”,而不是“Gateway Plugin”。
  • 在你定制的插件页面中,尽可能的匹配已经存在的管理后台的UI。
  • 没有必要创建一个入口来展示插件的元数据信息,而是让用户直接使用Openfire提供的功能,了解哪些插件已被安装并对这些插件进行管理。

创建管理后台页面

Openfire使用Sitemesh框架来管理后台的页面。一个全局定义的状态被应用到各个页面中,用于渲染最终的输出,如下图:

使用Sitemesh创建创建一个页面很简单。先创建一个HTML页面,然后使用meta标签将指令传递给Sitemesh框架。在渲染输出时,Sitemesh将使用你提供的指令来渲染HTML页面中body标签中的所有内容。下面的meta标签可被使用:

  • pageID — 页面的ID,他必须符合上述的管理后台的XML条目,pageID和subPageID必须被指定。
  • subPageID — 子页面的ID,他必须符合上述的管理后台的XML条目。子页面用于具体的功能操作,他与父页面ID相关联。例如,编辑或删除一个组。pageID或者subPageID必须被指定。
  • extraParams(可选)– 需要传递给页面的额外参数。例如,在一个页面中删除一个分组,那么分组的ID需要传递给该页面。所有的参数必须是做了URL encoded编码的。
  • decorator(可选)– 为该页面重写Sitemesh装饰者。一个名为为none的装饰者将被用于渲染页面。

下面的HTML片段展示了一个有效的页面:

1
2
3
4
5
6
7
8
9
10
[html]  view plain copy
  1. <html>  
  2. <head>  
  3.    <title>My Plugin Page</title>  
  4.    
  5.    <meta name="pageID" content="myPluginPage"/>  
  6. </head>  
  7. <body>  
  8.     Body here!  
  9. </body>  
  10. </html>  

在插件中使用i18n

如果想把你的插件翻译成多种语言,可以使用下面的步骤:

  • 在插件的根目录下创建“i18n”目录
  • 按照 %[plugin_name]%_i18n_language.properties 的命名规范来添加资源文件,[plugin_name]使用插件的目录名。
  • 在JSP文件中使用国际化的key来代替确切的文本,例如:
    1
    2
    3
    4
    
    [html]  view plain copy
    1. <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>  
    2. <%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>  
    3. ...  
    4. <fmt:message key="some.key.name" />  
  • 在Java类中需要国际化的文本可以使用LocalUtils类:
    1
    
    [java]  view plain copy
    1. org.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">jivesoftware</span>.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">util</span>.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">LocaleUtils</span>.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">getLocalizedString</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span><span style="font-family:inherit; font-size:12px; color:#00ff; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"some.key.name"</span>, <span style="font-family:inherit; font-size:12px; color:#00ff; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"[plugin_name]"</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
  • 在plugin.xml中需要国际化的文本使用${var}格式:
    1
    2
    
    [xml]  view plain copy
    1. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><sidebar</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">id</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"gateways"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">name</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"${plugin.sidebar.name}"</span> <span style="font-family:inherit; font-size:12px; color:#0066; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">description</span>=<span style="font-family:inherit; font-size:12px; color:#ff00; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">"${plugin.sidebar.description}"</span><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span>  
    2. <span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"><description<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>${plugin.description}<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px"><span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px"></description<span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">></span></span></span>  

使用Openfire的Build脚本

Openfire提供的build脚本可以帮助你编译和开发插件。脚本按照下面的目录结构寻找资源:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[text]  view plain copy
  1. <code>  
  2. myplugin/  
  3.  |- plugin.xml      <- 插件描述文件  
  4.  |- readme.html     <- 插件自述文件  
  5.  |- changelog.html  <- 插件更新日志  
  6.  |- logo_small.gif  <- 插件小图标  
  7.  |- logo_large.gif  <- 插件大图标  
  8.  |- classes/        <- 插件的类和properties文件  
  9.  |- lib/            <- 插件需要的lib库  
  10.  |- src/  
  11.      |- database    <- 插件的数据库脚本  
  12.      |- java        <- 插件的Java源码  
  13.      |   |- com  
  14.      |       |- mycompany  
  15.      |           |- *.java  
  16.      |- web  
  17.          |- *.jsp      <- 插件用于管理后台的JSP页面  
  18.          |- images/    <- JSP页面中需要的图片文件  
  19.          |- WEB-INF  
  20.              |- web.xml    <- 当JSP被编译成servlet后,将在该文件中注册  

build脚本将编译源码和JSP文件,并且按照插件的结构创建一个JAR文件。将你的插件目录拷贝到src/plugins目录下,然后使用命令 ant plugins 就可以编译你的插件了。

你的插件所需要的所有JAR文件需要放到lib目录下。在编译过程中,这些JAR文件同样会被复制到插件的lib目录下。

如果你创建了一个src/web/WEB-INF/web.xml文件,当插件启动时,注册其中的servlet将被初始化。只有web.xml中注册和映射的servlet将被认可。注意:此功能是通过JSP编译过程中所生成的web.xml文件合并到自定义的web.xml文件实现的。

实现你自己的插件

插件可以使用所有的Openfire API,这为插件的功能提供了极大的灵活性。下面有几种最常用的集成方式:

  • 注册一个插件作为一个组件。组件可以接收发送给特定子域的所有数据包。例如,test_component.example.com,发送给joe@test_component.example.com的数据包将被分发到该组件。注意:组件中定义的子域与DNS的子域是无关的。所有的XMPP路由在socket级别已经通过使用主服务器域名完成路由(如example.com),子域名只用于XMPP服务器内部路由。
  • 注册一个插件作为IQHandler,IQ处理器在特定的命名空间内相应IQ数据包,下面的代码片段展示如何注册一个IQHandler:
    1
    2
    3
    
    [java]  view plain copy
    1. IQHandler myHandler <span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">=</span> <span style="font-family:inherit; font-size:12px; color:#000000; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:bold; vertical-align:baseline; outline-width:0px">new</span> MyIQHander<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
    2. IQRouter iqRouter <span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">=</span> XMPPServer.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">getInstance</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span>.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">getIQRouter</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
    3. iqRouter.<span style="font-family:inherit; font-size:12px; color:#06633; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">addHandler</span><span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">(</span>myHandler<span style="font-family:inherit; font-size:12px; color:#0990; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">)</span><span style="font-family:inherit; font-size:12px; color:#339933; border-width:0px; margin:0px; padding:0px; font-style:inherit; font-weight:inherit; vertical-align:baseline; outline-width:0px">;</span>  
  • 注册一个插件作为PacketInterceptor,用于接收所有的数据包,并且可以随意的抛弃他们。例如,一个拦截器可以抛弃所有包含禁词的消息,或者标记他们让管理员进行审核。
  • 你可以持久化存储插件的设置,通过使用JiveGlobals.getProperty(String)和JiveGlobals.setProperty(String,String)方法,作为Openfire的属性。通过实现org.jivesoftware.util.PropertyEventListener的方法,使你的插件可以监听他的属性值的变化。你可以使用PropertyEventDispatcher.addListener(PropertyEventListener)方法注册你的插件作为一个坚挺着。务必在插件的destroyPlugin方法中注销你的插件。

常见问题

我可以使用一个目录而不是JAR文件来部署插件吗?

不可以,所有的插件必须使用JAR或者WAR文件来部署。当plugin目录下的JAR或者WAR文件不存在时,Openfire会认为该文件已经被删除用户期望卸载该插件,然后会同步删除对应的目录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值