插件的編寫能夠參考:http://www.cnblogs.com/hoojo/archive/2013/03/07/2947502.html html
其實openfire的插件很好理解:只要在openfire/openfire/plugins這個目錄下面,添加插件的文件夾,同時在文件夾裏面有plugin.xml和lib包便可,日常要是修改能夠直接到lib包中替換對應的jar包,重啓便可。 java
本身寫了一個實例: 服務器
import java.io.File; import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.container.Plugin; import org.jivesoftware.openfire.container.PluginManager; import org.jivesoftware.openfire.interceptor.InterceptorManager; import org.jivesoftware.openfire.muc.MUCEventDispatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ecc.uc.plugin.listener.MucRoomListener; import ecc.uc.plugin.listener.SaveMessageListener; /** * @author wengwh * @Date 2014-8-4 */ public class UcPlugin implements Plugin{ private static final Logger log = LoggerFactory.getLogger(UcPlugin.class); private XMPPServer server; private InterceptorManager interceptorManager; private SaveMessageListener saveMessageListener; private RosterListener rosterListener; private MucRoomListener mucRoomListener; public UcPlugin(){ server = XMPPServer.getInstance(); interceptorManager = InterceptorManager.getInstance(); saveMessageListener = new SaveMessageListener(); rosterListener = new RosterListener(); mucRoomListener = new MucRoomListener(); } public void initializePlugin(PluginManager manager, File pluginDirectory) { interceptorManager.addInterceptor(saveMessageListener); interceptorManager.addInterceptor(rosterListener); MUCEventDispatcher.addListener(mucRoomListener); System.out.println("初始化…… 安裝插件!"); System.out.println(server.getServerInfo()); } public void destroyPlugin() { interceptorManager.removeInterceptor(saveMessageListener); interceptorManager.removeInterceptor(rosterListener); MUCEventDispatcher.removeListener(mucRoomListener); System.out.println("服務器中止,銷燬插件!"); } }
二、IQHandler:相應包中特定的元素名或命名空間。下面的代碼展現瞭如何註冊一個IQHandler. dom
IQHandler myHandler = new MyIQHander(); spa
IQRouter iqRouter = XMPPServer.getInstance().getIQRouter(); 插件
iqRouter.addHandler(myHandler); code
三、PacketInterceptor:這種方式能夠接收系統傳輸的全部包,並能夠隨意的丟棄它們。例如,一個interceptor 能夠攔截並丟棄全部含有不健康信息的消息,或者將它們報告給系統管理員。 server
四、使用JiveGlobals.getProperty(String) 和 JiveGlobals.setProperty(String, String) 方法將咱們的插件設置爲openfire的一個全局屬性。經過實現org.jivesoftware.util.PropertyEventListener方法能夠將咱們的插件作成一個屬性監聽器監放任何屬性的變化。經過 PropertyEventDispatcher.addListener(PropertyEventListener)方法能夠註冊監聽。要注意的一點是,必定要在destroyPlugin()方法中將註冊的監聽註銷。 xml
(分發器:給各個監聽觸發,openfire的一種代碼編寫機制) htm
如:MUCEventDispatcher,SessionEventDispatcher
這幾個裏面我感受分發器這種是比較好用的,這種能夠定位到專門的事件,並提供了比較完整的對應的類,以及各類事件對應的方法,在開發的過程會更加清晰。
在插件編寫的過程當中多利用XMPPServer.getInstance()這個獲取到各類對象的管理類,使用這個管理類能夠來操做openfire中那些自帶的對象,好比Roster,muc等
舉個例子:
MultiUserChatManager multiUserChatManager = XMPPServer.getInstance().getMultiUserChatManager(); Affiliation affiliation = multiUserChatManager.getMultiUserChatService(roomJID).getChatRoom(roomJID.getNode()).getOccupantByFullJID(user).getAffiliation();插件能夠實現不少功能,在開發過程當中能夠多查看openfire的源碼,在插件中去使用來實現本身的功能。