Confluence Docs 3.0 : Writing an Event Listener Plugin Module This page last changed on Feb 15, 2006 by dhardiker@adaptavist.com. ### Overview For an introduction to event listener plugin modules, please read Event Listener Plugins. ### Writing an Event Listener as a plugin module within Confluence Writing an event listener is a four-step process: Identify the events you wish to listen for Create the EventListener Java class Implement `getHandledEventClasses()` Implement `handleEvent()` Add the listener module to your `atlassian-plugin.xml` file #### Identify the events you wish to listen for The 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:
Event Class | Published
--- | ---
LabelCreateEvent | On the creation of the first label to the target Content Entity Object.
LabelRemoveEvent | On the removal of the last label from the target Content Entity Object.
LabelAddEvent | On the addition of any label to the target Content Entity Object.
LabelDeleteEvent | On the deletion of any label from the target Content Entity Object.
#### Create the EventListener The 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. Your listener will only receive events of the types specified in getHandledEventClasses() You must specify all the event types you need - specifying a superclass will not include its subclasses Returning an empty array will cause your listener to receive every event Confluence produces 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.xml The 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 Nov 05, 2009 23:34