OpenFire學(xué)習(xí)(二)內(nèi)部組件

說明

內(nèi)部組件就是openfire的插件澈魄,因為它必須和openfire所在的進程一起運行筒狠,被openfire動態(tài)加載溜徙,所以叫做openfire插件葛超。
一般來說包晰,都是通過admin管理界面進行導(dǎo)入

添加插件

添加操作說明

刪除插件

刪除操作說明

項目結(jié)構(gòu)

  • src
  • plugin.xml
  • changelog.html:發(fā)布日志筒愚,記錄插件各版本的變化情況贡歧,就是純HTML怨绣,方便閱讀即可
  • readme.html:項目描述
  • logo_large.gif:插件大logo
  • logo_small.gif :插件小logo
  • pom.xml:打包的項目

src

不用多說整胃,自然是源碼路徑颗圣,內(nèi)部就是一個web項目,內(nèi)部包含java:資源代碼屁使,resources:項目配置資源等在岂,webapp:web服務(wù)資源、服務(wù)配置web.xml等蛮寂。
最為重要的是assembly蔽午,其重要配置項目的xml信息,是否見插件jar的關(guān)鍵


源碼結(jié)構(gòu)

assembly.xml

<!--
  ~ Copyright (C) 2018 Ignite Realtime Foundation. All rights reserved.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>openfire-plugin-ms-filter</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <!--
        The OFMeet plugin is an aggregation of various Jitsi projects. These projects have some conflicting dependencies.
        The OFMeet plugin handles these by using a class loading mechanism that loads dependencies for each Jitsi project
        from a different 'lib' folder (as opposed to a plugin that contains all dependencies in one 'lib' folder). This
        assembly descriptor creates these different 'lib' folders. Note that moduleSets are used to capture each of the
        Jitsi projects, and explicitly not dependencySets. Using dependencySets instead of moduleSets introduces a problem,
        as dependencySets use only the dependencies that have been de-duplicated and filtered for conflict by the Maven
        dependency management mechanism. This causes conflicting dependencies to not be included (which typically is a
        good thing, but is the exact opposite of what we're trying to do with this very specific plugin). -->
    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>org.igniterealtime.openfire.ofmeet:videobridge</include>
            </includes>
            <binaries>
                <includeDependencies>false</includeDependencies>
                <unpack>true</unpack>
                <outputDirectory>lib-videobridge</outputDirectory>
            </binaries>
        </moduleSet>

        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>org.igniterealtime.openfire.ofmeet:offocus</include>
            </includes>
            <binaries>
                <includeDependencies>false</includeDependencies>
                <unpack>true</unpack>
                <outputDirectory>lib-jicofo</outputDirectory>
            </binaries>
        </moduleSet>

        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>org.igniterealtime.openfire.ofmeet:ofgasi</include>
            </includes>
            <binaries>
                <includeDependencies>false</includeDependencies>
                <unpack>true</unpack>
                <outputDirectory>lib-jigasi</outputDirectory>
            </binaries>
        </moduleSet>
    </moduleSets>

    <fileSets>

        <!-- metadata files (readme, icons, etc). -->
        <fileSet>
            <outputDirectory/>
            <directory>${project.build.sourceDirectory}/../..</directory>
            <includes>
                <include>*.gif</include>
                <include>*.png</include>
                <include>lib/**</include>
            </includes>
        </fileSet>

        <!-- filtered metadata files (html, plugin.xml) -->
        <fileSet>
            <outputDirectory/>
            <directory>${project.build.sourceDirectory}/../..</directory>
            <filtered>true</filtered>
            <includes>
                <include>*.html</include>
                <include>plugin.xml</include>
            </includes>
        </fileSet>

        <!-- database, i18n and web directory -->
        <fileSet>
            <outputDirectory/>
            <directory>${project.build.sourceDirectory}/..</directory>
            <includes>
                <include>database/**</include>
                <include>i18n/**</include>
                <include>web/**</include>
            </includes>
            <!-- Exclude jsp pages (they will be compiled) and filterable resources -->
            <excludes>
                <exclude>**/*.jsp</exclude>
                <exclude>i18n/**/*.properties</exclude>
                <exclude>web/**/*.xml</exclude>
                <exclude>web/**/*.html</exclude>
                <exclude>web/**/*.properties</exclude>
            </excludes>
        </fileSet>

        <!-- database, i18n and web directory (filtered) -->
        <fileSet>
            <outputDirectory/>
            <directory>${project.build.sourceDirectory}/..</directory>
            <filtered>true</filtered>
            <includes>
                <include>i18n/**/*.properties</include>
                <include>web/**/*.xml</include>
                <include>web/**/*.html</include>
                <include>web/**/*.properties</include>
            </includes>
            <excludes>
                <!-- Exclude the web.xml, it will be generated by JspC -->
                <exclude>web/WEB-INF/web.xml</exclude>
            </excludes>
        </fileSet>

        <!-- web.xml (it will be modified by JspC) -->
        <fileSet>
            <outputDirectory>web/WEB-INF</outputDirectory>
            <directory>${project.build.directory}</directory>
            <includes>
                <include>web.xml</include>
            </includes>
        </fileSet>

        <!-- Include the classes folder (filtered) -->
        <fileSet>
            <outputDirectory>classes</outputDirectory>
            <directory>classes</directory>
            <filtered>true</filtered>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </fileSet>

        <!-- Include the classes folder -->
        <fileSet>
            <outputDirectory>classes</outputDirectory>
            <directory>classes</directory>
            <filtered>false</filtered>
            <excludes>
                <exclude>**/*.xml</exclude>
                <exclude>**/*.properties</exclude>
            </excludes>
        </fileSet>
    </fileSets>

    <!-- Bundle the dependencies of this plugin -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <!-- Exclude the modules (and their transitive dependencies) that go in separate 'lib' folders. -->
            <excludes>
                <exclude>org.igniterealtime.openfire.ofmeet:videobridge</exclude>
                <exclude>org.igniterealtime.openfire.ofmeet:offocus</exclude>
                <exclude>org.igniterealtime.openfire.ofmeet:ofgasi</exclude>
            </excludes>
            <useTransitiveDependencies>true</useTransitiveDependencies>
            <useTransitiveFiltering>true</useTransitiveFiltering>
        </dependencySet>
    </dependencySets>
</assembly>

plugin.xml

是最重要的插件的配置文件酬蹋。這個文件非常重要及老,openfire需要讀取插件信息,例如插件的啟動類是哪一個范抓,需要在plugin.xml中定義

標簽 說明
class 插件的啟動類骄恶,也可以理解為插件的main函數(shù)
name 插件的名字
description 插件功能描述字段
author 作者名稱
version 插件版本
date 插件書寫的日期,這個自己知道就可以了
url 插件的官網(wǎng)
minServerVersion openfire的最小版本
minJavaVersion JDK最小版本
<plugin>
    <class>com.demo.plugin.MessageFilterPlugin</class>
    <name>文本消息過濾插件</name>
    <description>過濾一些敏感詞匕垫、非法詞匯</description>
    <author>機制的我</author>
    <version>${project.version}</version>
    <date>06/06/2019</date>
    <minServerVersion>4.3.2</minServerVersion>
    <minJavaVersion>1.8</minJavaVersion>
</plugin>

實戰(zhàn)

1僧鲁、在父項目中的plugin模塊下,創(chuàng)建子項目象泵,然后按照上面的結(jié)構(gòu)寞秃,創(chuàng)建項目按照上面結(jié)構(gòu)
2、復(fù)制assembly.xml
3偶惠、然后編寫主要業(yè)務(wù)類
4春寿、補上pom的配置
一個小插件項目就完成了,然后就是打包命令忽孽,在父項目目錄下

mvn verify -f plugins/openfire-plugin-ms-filter/pom.xml

最后打包出來的項目openfire-plugin-ms-filter.jar绑改,通過上面添加插件的步驟,通過admin界面進行添加扒腕。

插件

下面粘貼部分需要用到的項目的代碼和xml绢淀。

業(yè)務(wù)類

package com.demo.plugin;

import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;

import java.io.File;

public class MessageFilterPlugin implements Plugin, PacketInterceptor {
    private static final Logger Log = LoggerFactory.getLogger(MessageFilterPlugin.class);
    // 消息攔截器
    private InterceptorManager interceptorManager;

    // 插件初始化函數(shù)
    @Override
    public void initializePlugin(PluginManager pluginManager, File file) {
        // 將當(dāng)前插件加入到消息攔截管理器(interceptorManager )中,當(dāng)消息到來或者發(fā)送出去的時候瘾腰,會觸發(fā)本插件的interceptPacket方法皆的。
        interceptorManager = InterceptorManager.getInstance();
        interceptorManager.addInterceptor(this);
    }

    // 插件銷毀函數(shù)
    @Override
    public void destroyPlugin() {
        // 當(dāng)插件被卸載的時候,主要通過openfire管理控制臺卸載插件時蹋盆,被調(diào)用费薄。注意interceptorManager的addInterceptor和removeInterceptor需要成對調(diào)用硝全。
        interceptorManager.removeInterceptor(this);
    }

    // 插件攔截處理函數(shù)
    @Override
    public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
        // incoming表示本條消息剛進入openfire。processed為false楞抡,表示本條消息沒有被openfire處理過伟众。這說明這是一條處女消息,也就是沒有被處理過的消息召廷。
        if (incoming && processed == false) {
            // packet可能是IQ凳厢、Presence、Message竞慢,這里當(dāng)packet是message的時候先紫,進行處理。
            if (packet instanceof Message) {
                // 將packet強制轉(zhuǎn)換為Message
                Message msg = (Message) packet;
                // 取得message中的body內(nèi)容筹煮,就是消息正文
                String body = msg.getBody();
                // 如果內(nèi)容中包含fuck遮精,則拒絕處理消息
                if (body != null && body.contains("fuck")) {
                    // 這里通過拋出異常的方式,來阻止程序流程繼續(xù)執(zhí)行下去败潦。
                    PacketRejectedException rejectedException = new PacketRejectedException();
                    // 設(shè)置返回信息
                    rejectedException.setRejectionMessage("信息發(fā)送失敗本冲,包含侮辱性詞語");

                    throw rejectedException;
                }

            }
        }
    }
}

配置pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.igniterealtime.openfire</groupId>
        <artifactId>plugins</artifactId>
        <version>4.3.2</version>
    </parent>
    <groupId>com.openfire.plugin.demo</groupId>
    <artifactId>openfire-plugin-ms-filter</artifactId>
    <version>1.0</version>


    <build>
        <sourceDirectory>src/java</sourceDirectory>
        <testSourceDirectory>src/test</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <finalName>${project.artifactId}</finalName>
                            <attach>false</attach>
                            <descriptors>
                                <descriptor>src/assembly/ms-filter-plugin-assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Compile the JSP pages -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-jspc-maven-plugin</artifactId>
                <version>9.2.14.v20151106</version>
                <configuration>
                    <webAppSourceDirectory>${project.build.sourceDirectory}/../java</webAppSourceDirectory>
                    <webXml>${project.build.sourceDirectory}/../java/WEB-INF/web.xml</webXml>
                </configuration>
                <executions>
                    <execution>
                        <id>jspc</id>
                        <goals>
                            <goal>jspc</goal>
                        </goals>
                        <configuration>
                            <jspc>
                                <package>org.jivesoftware.openfire.plugin.${project.artifactId}</package>
                            </jspc>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.igniterealtime.openfire</groupId>
                        <artifactId>xmppserver</artifactId>
                        <version>${openfire.version}</version>
                    </dependency>
                </dependencies>
            </plugin>


            <!-- Fix for Mina 2.x -->
            <!-- https://issues.apache.org/jira/browse/DIRMINA-919 -->
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.0.0</version>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
    </dependencies>

</project>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市劫扒,隨后出現(xiàn)的幾起案子檬洞,更是在濱河造成了極大的恐慌,老刑警劉巖粟关,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疮胖,死亡現(xiàn)場離奇詭異,居然都是意外死亡闷板,警方通過查閱死者的電腦和手機澎灸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來遮晚,“玉大人性昭,你說我怎么就攤上這事∠厍玻” “怎么了糜颠?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長萧求。 經(jīng)常有香客問我其兴,道長,這世上最難降的妖魔是什么夸政? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任元旬,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘匀归。我一直安慰自己坑资,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布穆端。 她就那樣靜靜地躺著袱贮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪体啰。 梳的紋絲不亂的頭發(fā)上攒巍,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天,我揣著相機與錄音狡赐,去河邊找鬼窑业。 笑死钦幔,一個胖子當(dāng)著我的面吹牛枕屉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鲤氢,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼搀擂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了卷玉?” 一聲冷哼從身側(cè)響起哨颂,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎相种,沒想到半個月后威恼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡寝并,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年箫措,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衬潦。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡斤蔓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出镀岛,到底是詐尸還是另有隱情弦牡,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布漂羊,位于F島的核電站驾锰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏走越。R本人自食惡果不足惜椭豫,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捻悯,春花似錦匆赃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至姓言,卻和暖如春瞬项,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背何荚。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工囱淋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人餐塘。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓妥衣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親戒傻。 傳聞我的和親對象是個殘疾皇子税手,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 使用指導(dǎo) 如何添加外部依賴jar包 在Maven工程中添加依賴jar包,很簡單需纳,只要在POM文件中引入對應(yīng)的<de...
    靜默虛空閱讀 2,802評論 0 13
  • openfire服務(wù)器下載:https://www.igniterealtime.org/downloads/in...
    huangxiongbiao閱讀 580評論 0 0
  • 我們都知道Maven本質(zhì)上是一個插件框架芦倒,它的核心并不執(zhí)行任何具體的構(gòu)建任務(wù),所有這些任務(wù)都交給插件來完成不翩,例如編...
    付鵬丶閱讀 1,622評論 0 15
  • 官方文檔:http://maven.apache.org/guides/introduction/introduc...
    icecrea閱讀 2,103評論 0 2
  • 一【職業(yè)目標】: 2019年成為公司一名優(yōu)秀培訓(xùn)管理者兵扬, 01-為公司打造3個培訓(xùn)項目, 02-培養(yǎng)15名優(yōu)秀內(nèi)訓(xùn)...
    小鑫子閱讀 433評論 0 4