(转)OpenFire源码学习之十五:插件开发

转:http://blog.csdn.net/huwenfeng_2011/article/details/43418493

Plugin接口规范

插件是openfire功能的增强表现,它的主要任务:

l  在XMPP协议中作为附加功能实现

l  动态修改控制管理台

l  使用openfire api作为新功能添加到服务器

Openfire里面的插件都会存放在plugins(工程目录为:src/plugins)的住目录下。使用ant工具编译后插件会打成jar包生成在target/openfire/plugins目录下。一个完整的插件应该包含以下的结构:

Yourplugin/

       | -plugin.xml                            插件定义文件
        | -readme.html                          任择自述文件的插件,它会显示给最终用户

       | -changelog.html                      任择修改文件的插件,它会显示给最终用户 

       | -icon_small.gif                       可选小( 16x16 )图标与插件(也可以是 PNG文件) 

       | -icon_large.gif                       可选大( 32x32 )图标与插件(也可以是 PNG文件) 
        |classes/                                    资源的插件需要(即属性文件) 
        |-database/                               可选数据库架构文件,你需要插件
        | -i18n/                                      插件国际化的语言配置。 
        |-lib/                                        您的插件的jar包 

       |-web                                      资源的管理控制台集成,如果有的话
            | - WEB-INF

                   |-web-custom.xml              可选用户自定义的web.xml中的自定义servlets
            |-images/                         图片文件存放的目录

            |-test-plugin.jsp         jsp文件

其中类和lib目录是可选的,类目录下的所有文件和以及在lib目录的所有JAR文件中都会被添加到classpath路径下。 plugin.xml 这个文件也必须要添加到您的插件当中,这个文件描述了改插件的的基本信息和一些需要在控制台上生成目录的信息。Plugin.xml文件中的内容应如下所示:

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <plugin>  
  3.     <!—需要的插件类 -->  
  4.     <class>org.example.ExamplePlugin</class>  
  5.    
  6.     <!-- 插件元数据 -->  
  7.     <name>Example Plugin</name>  
  8.     <description>This is an example plugin.</description>  
  9.     <author>Jive Software</author>  
  10.     <version>1.0</version>  
  11.     <date>07/01/2006</date>  
  12.     <url>http://www.igniterealtime.org/projects/openfire/plugins.jsp</url>  
  13.     <!-- 要求openfire服务器的最低版本 -->  
  14.    <minServerVersion>3.0.0</minServerVersion>  
  15.      <licenseType>gpl</licenseType>  
  16.    
  17.     <!-- 管理控制台的条目 -->  
  18. <adminconsole>  
  19.       <!-- More on this below -->  
  20.     </adminconsole>  
  21. </plugin>  

该元素的领域设置介绍:

l   Name                        该插件的名称

l   Description                    该插件说明

l   Author                       该插件作者

l   Version                      该插件的版本标识

l   Date                         该插件的发布日期

l   Url                          该插件说明网址

l   minServerVersion              该插件需要最低版本Opfenfire的支持

l   licenseType                   显示许可协议,常用的显示值如下:

                                 “commercial”:commercial “商业” :插件是下发布的商业许可协议。

                                 “gpl”: 通用公共许可证,插件发布使用GNU公共授权

                                 “apache”:Apache许可证internal

                                 “internal”:插件是提供内部组织使用

                                 “other”: 如果许可未设置就会假定为其他

l  databaseKey                   如果插件需要它自己的数据表,该databaseKey内容应设立一个架构                    主要名称(通常是相同名称的插件)。数据库架构文件为每个支持的数据库,然后放置在数据库目录下的插件。例如, “foo”,架构文件将被称为“ foo_mysql.sql ” , “ foo_oracle.sql ”等等,我们建议您,您的表前缀of ,以避免可能的冲突与其他应用程序安装在同一数据库。脚本应该进入ofVersion表使用的关键,这样的架构版本信息可跟踪,例如: 
INSERT INTO ofVersion (name, version) VALUES ('foo', 0); databaseVersion -数据库版本号(如果数据库模式的定义)。新的插件与数据库架构应该开始在版本。如果将来插件版本的需要更新,这些更新可以定义创建子目录中的升级数据库目录为每个版本。例如,目录database/upgrade/1database/upgrade/2将包含脚本,如“ foo_mysql.sql ”和“ foo_oracle.sql ”中包含相关的数据库,为每一个版本的变化。每个脚本应该更新版本中的信息ofVersion表,例如: 
UPDATE ofVersion set version=1 where name='foo';

l     parentPlugin                 父层插件(作为“foo”的“ foo.jar ”插件)。当一个插件有一个父插件,插件的类加载器将被使用来而不是建立一个新的类加载器。这可让插件更加紧密地协同工作。子插件将不会影响其父插件。

l   Adminconsole                  该插件在管理控制台的条目            

一个完整plugin.xml文件内容应该如下:

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <plugin>  
  3.     <class>org.jivesoftware.openfire.plugin.RegistrationPlugin</class>  
  4.       
  5.     <name>Registration</name>  
  6.     <description>Performs various actions whenever a new user account is created.</description>  
  7.     <author>Ryan Graham</author>  
  8.     <version>1.5.0</version>  
  9.     <date>12/2/2009</date>  
  10.     <minServerVersion>3.7.0</minServerVersion>  
  11.       
  12.     <adminconsole>  
  13.         <tab id="tab-users">  
  14.             <sidebar id="sidebar-users">  
  15.                 <item id="registration-props-form" name="Registration Properties"  
  16.                         url="registration-props-form.jsp"  
  17.                         description="User Registration" />  
  18.             </sidebar>  
  19.         </tab>  
  20.     </adminconsole>  
  21. </plugin>  

插件开发

注意:启动of的开发模式需要添加tools.jar包 添加到target/lib目录下

设置参数-DdevelopmentMode="true"

一、添加如下结构插件包

二、编写TestIQHandle类

[java] view plain copy
 
  1. public class TestIQHandle extends IQHandler {  
  2.   
  3.     private static final String MODULE_NAME = "test plugin";  
  4.     private static final String NAME_SPACE = "com:test:testplug";  
  5.     private IQHandlerInfo info;  
  6.       
  7.     public TestIQHandle(){  
  8.         super(MODULE_NAME);  
  9.         info = new IQHandlerInfo("query", NAME_SPACE);  
  10.     }  
  11.     public TestIQHandle(String moduleName) {  
  12.         super(moduleName);  
  13.         info = new IQHandlerInfo("query", NAME_SPACE);  
  14.     }  
  15.   
  16.     @Override  
  17.     public IQ handleIQ(IQ packet) throws UnauthorizedException {  
  18.         IQ reply = IQ.createResultIQ(packet);  
  19.         Element groups = packet.getChildElement();  
  20.         if(true){  
  21.             System.out.println("=======请求非法========");  
  22.         }  
  23.         if(!IQ.Type.get.equals(packet.getType())){  
  24.             reply.setChildElement(groups.createCopy());  
  25.             reply.setError(PacketError.Condition.bad_request);  
  26.             return reply;  
  27.         }  
  28.         //StringUtils.substringBefore(packet.getFrom().toString(), "@");  
  29.         return reply;  
  30.     }  
  31.   
  32.     @Override  
  33.     public IQHandlerInfo getInfo() {  
  34.         // TODO Auto-generated method stub  
  35.         return info;  
  36.     }  
  37. }  

三、编写TestPlugin类

[java] view plain copy
 
  1. public class TestPlugin implements Plugin ,PropertyEventListener{  
  2.   
  3.     private XMPPServer server;  
  4.     @Override  
  5.     public void initializePlugin(PluginManager manager, File pluginDirectory) {  
  6.         server = XMPPServer.getInstance();  
  7.         server.getIQRouter().addHandler(new TestIQHandle());  
  8.         PropertyEventDispatcher.addListener(this);  
  9.         System.out.println("==========插件初始化=============");  
  10.     }  
  11.   
  12.     @Override  
  13.     public void destroyPlugin() {  
  14.         PropertyEventDispatcher.removeListener(this);  
  15.         System.out.println("==========插件销毁动作=============");          
  16.     }  
  17.   
  18.     @Override  
  19.     public void propertySet(String property, Map<String, Object> params) {  
  20.         // TODO Auto-generated method stub  
  21.     }  
  22.   
  23.     @Override  
  24.     public void propertyDeleted(String property, Map<String, Object> params) {  
  25.         // TODO Auto-generated method stub  
  26.     }  
  27.   
  28.     @Override  
  29.     public void xmlPropertySet(String property, Map<String, Object> params) {  
  30.         // TODO Auto-generated method stub  
  31.     }  
  32.   
  33.     @Override  
  34.     public void xmlPropertyDeleted(String property, Map<String, Object> params) {  
  35.         // TODO Auto-generated method stub  
  36.     }  
  37. }  

四、编写TestServlet

[java] view plain copy
 
  1. public class TestServlet extends HttpServlet{  
  2.   
  3.     public TestServlet() {  
  4.         super();  
  5.     }  
  6.   
  7.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  8.             throws ServletException, IOException {  
  9.         System.out.println("============调用servlet=============");  
  10.     }  
  11.   
  12.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  13.             throws ServletException, IOException {  
  14.         this.doGet(request, response);  
  15.     }  
  16.   
  17.     public void init() throws ServletException {  
  18.         AuthCheckFilter.addExclude("test/testservlet");  
  19.         System.out.println("==========init()============");  
  20.     }  
  21.   
  22.     public void destroy() {   
  23.         System.out.println("==========destroy()=========");  
  24.         AuthCheckFilter.addExclude("test/testservlet");  
  25.     }  
  26. }  

五、配置web-custom.xml

[html] view plain copy
 
  1. <?xml version='1.0' encoding='ISO-8859-1'?>  
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
  3. <web-app>  
  4.     <servlet>  
  5.         <servlet-name>TestServlet</servlet-name>  
  6.         <servlet-class>com.test.plugin.test.TestServlet</servlet-class>  
  7.     </servlet>  
  8.       
  9.     <servlet-mapping>  
  10.         <servlet-name>TestServlet</servlet-name>  
  11.         <url-pattern>/servlet</url-pattern>  
  12.     </servlet-mapping>  
  13. </web-app>  

六、test-plugin.jsp

[html] view plain copy
 
  1. <%@ page import="java.util.*,  
  2.                  org.jivesoftware.openfire.XMPPServer,  
  3.                  org.jivesoftware.util.*,  
  4.                  com.test.plugin.TestPlugin"  
  5.     errorPage="error.jsp"  
  6. %>  
  7.   
  8. <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>  
  9. <%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>  
  10.   
  11. <%-- Define Administration Bean --%>  
  12. <jsp:useBean id="admin" class="org.jivesoftware.util.WebManager"  />  
  13. <c:set var="admin" value="${admin.manager}" />  
  14. <% admin.init(request, response, session, application, out ); %>  
  15. <%String path = request.getContextPath();   
  16. System.out.println("path= "+path);  
  17. %>  
  18.   
  19. <html>  
  20.     <head>  
  21.         <title>User Service Properties</title>  
  22.         <meta name="pageID" content="test_plugin"/>  
  23.     </head>  
  24.     <body>  
  25.     <p>==============================================</p>  
  26.         <form action="<%=path %>/plugins/testplug/servlet" method="post">               //testplug是插件的名称
  27.           
  28.         <fieldset>  
  29.             <legend>test plugin</legend>  
  30.             <div>  
  31.                <input type="text" size="15" /><br>  
  32.                <input type="text" size="15" /><br>  
  33.             </div>  
  34.         </fieldset>  
  35.         <br><br>  
  36.         <input type="submit" value="Save Settings">  
  37.         </form>  
  38. </body>  
  39. </html>  

七、Plugin.xml

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <plugin>  
  3.   
  4.    <class>com.test.plugin.server.TestPlugin</class>  
  5.      
  6.    <name>TestPlugin</name>  
  7.    <description>test plugin For myTestPlugin</description>  
  8.    <author>huwf</author>  
  9.    <version>1.0.0</version>  
  10.    <date>5/10/2013</date>  
  11.    <url>http://www.baidu.com</url>  
  12.    <minServerVersion>3.5.1</minServerVersion>  
  13.    <licenseType>gpl</licenseType>  
  14.   
  15.    <adminconsole>  
  16.       <tab id="tab-server">  
  17.          <sidebar id="sidebar-server-settings">  
  18.             <item id="test_plugin"   
  19.                   name="testPlugin"   
  20.                   url="test-plugin.jsp"   
  21.                   description="Edit subscription plugin properties" />  
  22.          </sidebar>  
  23.       </tab>  
  24.    </adminconsole>  
  25.      
  26. </plugin>  

八、发布/编译插件

 
原文地址:https://www.cnblogs.com/wangle1001986/p/7228444.html