eXo Platform 3.5

## Chapter 75. Organization Listener

The Organization Service provides a mechanism to receive notifications when:

- A User is created, deleted or modified.

- A Group is created, deleted or modified.

- A Membership is created or removed.

This mechanism is very useful to cascade some actions when the organization model is modified. For example, it is currently used to :

- Initialize the personal portal pages.

- Initialize the personal calendars, address books and mail accounts in CS.

- Create drives and personal areas in ECM.

## Writing your own listeners

To implement your own listener, you just need to write extend some existing listener classes. These classes define hooks that are invoked before or after operations are performed on organization model.

### UserEventListener

To listen to user changes, you need to extend <>org.exoplatform.services.organization.UserEventListener</> :

    public class MyUserListener extends UserEventListener {

      public void preSave(User user, boolean isNew) throws Exception {

        System.out.println("Before " + (isNew?"creating":"updating") + " user " + user.getUserName());

      }

      public void postSave(User user, boolean isNew) throws Exception {

        System.out.println("After user " + user.getUserName() + (isNew?" created":" updated"));

      }

      public void preDelete(User user) throws Exception {

        System.out.println("Before deleting user " + user.getUserName());

      }

      public void preDelete(User user) throws Exception {

        System.out.println("After deleting user " + user.getUserName());

      }

    }

### GroupEventListener

To listen group changes, you need to extend org.exoplatform.services.organization.GroupEventListener :

    public class MyGroupListener extends GroupEventListener {

      public void preSave(Group group, boolean isNew) throws Exception {

        System.out.println("Before " + (isNew?"creating":"updating") + " group " + group.getName());

      }

      public void postSave(Group group, boolean isNew) throws Exception {

        System.out.println("After group " + group.getName() + (isNew?" created":" updated"));

      }

      public void preDelete(Group group) throws Exception {

        System.out.println("Before deleting group " + group.getName());

      }

      public void preDelete(Group group) throws Exception {

        System.out.println("After deleting group " + group.getName());

      }

    }

### MembershipEventListener

To listen to membership changes, you need to extend org.exoplatform.services.organization.MembershipEventListener :

    public class MyMembershipListener extends MembershipEventListener {

      public void preSave(Membership membership, boolean isNew) throws Exception {

        System.out.println("Before " + (isNew?"creating":"updating") + " membership.");

      }

      public void postSave(Membership membership, boolean isNew) throws Exception {

        System.out.println("After membership " + (isNew?" created":" updated"));

      }

      public void preDelete(Membership membership) throws Exception {

        System.out.println("Before deleting membership");

      }

      public void preDelete(Membership membership) throws Exception {

        System.out.println("After deleting membership");

      }

    }

## Registering your listeners

Registering the listeners is then achieved by using the ExoContainer plugin mechanism. Learn more about it on the Service Configuration for Beginners article.

To effectively register organization service's listeners you simply need to use the addListenerPlugin seer injector.

So, the easiest way to register your listeners is to pack them into a .jar and create a configuration file into it under mylisteners.jar!/conf/portal/configuration.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <configuration>

     <external-component-plugins>

      <target-component>org.exoplatform.services.organization.OrganizationService</target-component>

       <component-plugin>

        <name>myuserplugin</name>

        <set-method>addListenerPlugin</set-method>

        <type>org.example.MyUserListener</type>

        <description></description>

       </component-plugin>

       <component-plugin>

        <name>mygroupplugin</name>

        <set-method>addListenerPlugin</set-method>

        <type>org.example.MyGroupListener</type>

        <description></description>

       </component-plugin>

       <component-plugin>

        <name>mymembershipplugin</name>

        <set-method>addListenerPlugin</set-method>

        <type>org.example.MyMembershipListener</type>

        <description></description>

       </component-plugin>

      </external-component-plugins>

    <configuration>

Now, simply deploy the jar under $TOMCAT_HOME/lib and your listeners are ready!

### Note

Be aware that you need to set proper RuntimePermission to be able to add or remove Listeners. To do that you need to grant the following permission for your code

    permission java.lang.RuntimePermission "manageListeners"

Copyright © 2009-2012. All rights reserved. eXo Platform SAS