Confluence : Writing an Event Listener Plugin Module
This page last changed on Feb 15, 2006 by dhardiker@adaptavist.com.
OverviewFor an introduction to event listener plugin modules, please read Event Listener Plugins. Writing an Event Listener as a plugin module within ConfluenceWriting an event listener is a four-step process:
Identify the events you wish to listen forThe easiest thing here is to consult the latest API, in the package com.atlassian.confluence.event.events . When you implement an EventListener you will provide an array of Class objects which represent the events you wish to handle. The naming of most events are self explanitory (GlobalSettingsChangedEvent or ReindexStartedEvent for example), however there are some which need further clarification:
Create the EventListenerThe EventListener interface defines two methods which must be implemented: getHandledEventClasses() and handleEvent(). Implement getHandledEventClasses() The getHandledEventClasses() method holds an array of class objects representing the events you wish to listen for.
So, if you want your listener to receive only SpaceCreatedEvent and SpaceRemovedEvent private static final Class[] HANDLED_EVENTS = new Class[] { SpaceCreateEvent.class, SpaceRemovedEvent.class }; public Class[] getHandledEventClasses() { return HANDLED_EVENTS; } Alternatively, to receive all possible events: /** * Returns an empty array, thereby handling every ConfluenceEvent * @return */ public Class[] getHandledEventClasses() { return new Class[0]; } Implement handleEvent() The implementation below simply relies upon the toString() implementation of the event and logs it to a log4j appender. public void handleEvent(ConfluenceEvent event) { if (!initialized) initializeLogger(); log.info(event); } Most often, a handleEvent(..) method will type check each event sent through it and execute some conditional logic. public void handleEvent(ConfluenceEvent event) { if (event instanceof LoginEvent) { LoginEvent loginEvent = (LoginEvent) event; // ... logic associated with the LoginEvent } else if (event instanceof LogoutEvent) { LogoutEvent logoutEvent = (LogoutEvent) event; // ... logic associated with the LogoutEvent } } A full example of an EventListener class that listens for login and logout events can be found in EventListener Example. Add the EventListener as a module to your plugin by creating an atlassian-plugin.xmlThe atlassian-plugin.xml file has been described elsewhere in detail. This is an example of a listener plugin module included in an atlassian-plugin.xml file. <atlassian-plugin name='Optional Listeners' key='confluence.extra.auditor'> <plugin-info> <description>Audit Logging</description> <vendor name="Atlassian Software Systems" url="http://www.atlassian.com"/> <version>1.0</version> </plugin-info> <listener name='Audit Log Listener' class='com.atlassian.confluence.extra.auditer.AuditListener' key='auditListener'> <description>Provides an audit log for each event within Confluence.</description> </listener> </atlassian-plugin> |
![]() |
Document generated by Confluence on May 01, 2007 19:27 |