在Openfire源碼中添加本身的插件

參考:http://redhacker.iteye.com/blog/1919329html


Openfire源碼的編譯請查看另外一篇文章:Ubuntu12.04(64bit)上部署編譯運行Openfire+Spark環境java

1、基於Openfire源碼安裝自定義插件

建立本身的插件路徑
web


編寫plugin.xml,注意路徑,是建立在插件的根目錄下。不然即便編譯出jar包,管控平臺也不識別。服務器

這裏偷懶直接在其餘插件中複製過來的。app

<?xml version="1.0" encoding="UTF-8"?>jsp

<plugin>ide


<!--插件JAVA文件路徑-->post

<class>org.jivesoftware.example.plugin.ExamplePlugin</class>ui


<!-- Plugin meta-data -->this

<name>Example Plugin</name>

<description>This is an example plugin.</description>

<author>Jive Software</author>


<version>1.0</version>

<date>07/01/2006</date>

<url>http://www.igniterealtime.org/projects/openfire/plugins.jsp</url>

<minServerVersion>3.0.0</minServerVersion>

<licenseType>gpl</licenseType>


<!-- Admin console entries -->

<adminconsole>

<!-- More on this below -->

</adminconsole>

</plugin>


編寫java插件

package org.jivesoftware.example.plugin;


import java.io.File;


import org.jivesoftware.openfire.XMPPServer;

import org.jivesoftware.openfire.container.Plugin;

import org.jivesoftware.openfire.container.PluginManager;


public class ExamplePlugin 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、帶有JSPServlet的插件

目錄結構

添加Servlet文件

package org.jivesoftware.openfire.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;


public class ExampleServlet extends HttpServlet {


private static final long serialVersionUID = -6093345417438012819L;


@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// super.doGet(req, resp);


resp.setContentType("text/plain");

PrintWriter out = resp.getWriter();

System.out.println("example servlet doget");

out.print("example servlet doget");

out.flush();

}


@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// super.doPost(req, resp);


resp.setContentType("text/plain");

PrintWriter out = resp.getWriter();

System.out.println("example servlet dopost");

out.print("example servlet dopost");

out.flush();

}


@Override

public void destroy() {

super.destroy();

}


@Override

public void init() throws ServletException {

super.init();

}

}


配置Servlet,在web/WEB-INF添加xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!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文件路徑 -->

<servlet-class>org.jivesoftware.openfire.plugin.ExampleServlet</servlet-class>

<servlet-name>ExampleServlet</servlet-name>

</servlet>

<servlet-mapping>

<servlet-name>ExampleServlet</servlet-name>

<url-pattern>/servlet</url-pattern>

</servlet-mapping>

</web-app>


添加jsp文件在web目錄下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>example plugin</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="pageID" content="example-servlet"/>

</head>

<body>

<h3>hello world jsp!! <a href="/plugins/example/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>asdf</td>

<td>asdf</td>

<td>asdf</td>

</tr>

<tr class="jive-even">

<td>asdf</td>

<td>asdf</td>

<td>asdf</td>

</tr>

<tr class="jive-odd">

<td>asdf</td>

<td>asdf</td>

<td>asdf</td>

</tr>

</tbody>

</table>

</div>

</body>

</html>


配置Plugin.xml


<adminconsole>

<tab id="tab-server">

<sidebar id="sidebar-server-settings">

<item id="example-servlet" name="Example Servlet" url="example_servlet.jsp"

description="Click is trigger example plugin" />

</sidebar>

</tab>

</adminconsole>

注意:item節點下的id屬性值對應jsp<meta name="pageID" content="example-servlet"/>content值。

從新編譯openfire,在服務器設置中能夠看到Servlet插件了。

插件編譯後生成的路徑在:Openfire/openfire_src/target/openfire/plugins

3、編譯單個插件

Ant編譯窗口中,找到plugin節點,右鍵Run AS->2 Ant Build,選擇第二個選項,顯示配置信息。

Main標籤頁中Arguments中輸入-Dplugin=插件名

插件名是從src/plugins目錄下,你定義的目錄名字決定的,這裏插件名爲example


Targets中只勾選plugin選項。


保存後,點擊Run進行編譯,編譯成功後,管控平臺從新加載插件。

相關文章
相關標籤/搜索