7.3. Organization listeners

In eXo Platform, whenever an action occurs (for example, login/logout, content creation/modification), a corresponding event is sent to Listener Service that dispatches the notification to its listeners. Listeners then can perform whatever action they want when receiving an event. See Listener Service events for more details.

In this section, you will have opportunity to learn about Organization listeners, how to write and pack them in a .jar file, then deploy them into eXo Platform based on the extension mechanism of eXo Platform.

To write a new organization listener, you first need to know about Organization Service that provides a mechanism to receive notifications when:

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 or to create drives and personal areas.

In term of working mechanism of Organization listeners, it is quite similar to that of Listener Service. See Understanding the Listener Service for how it works and how to configure a listener in general.

Writing Organization listeners

  1. Create a JAR project, named my-event-listeners for example, with the following structure:

  2. Declare the dependencies needed for your own listeners in the pom.xml file (you can go to eXo Platform repository to check the artifact versions). The pom.xml file now looks like:

    
    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>exo.userevent.listener</groupId>
      <artifactId>my-event-listeners</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>maven</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>org.exoplatform.core</groupId>
          <artifactId>exo.core.component.organization.api</artifactId>
          <version>2.6.0-GA</version>
        </dependency>
        <dependency>
          <groupId>javax.jcr</groupId>
          <artifactId>jcr</artifactId>
          <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>
      </dependencies>
    </project>
  3. Implement your own listeners by extending some existing listener classes in src/main/java/org/exoplatform/listener. These classes define hooks that are invoked before or after operations are performed on the organization model.

    • UserEventListener:

      To listen to user changes, you need to extend org.exoplatform.services.organization.UserEventListener in MyUserListener.java.

      package org.exoplatform.listener;
      
      
          import javax.jcr.Node;
          import javax.jcr.Session;
          import org.exoplatform.services.organization.User;
          import org.exoplatform.services.organization.UserEventListener;
          
          public class MyUserListener extends UserEventListener {
              public void preSave(User user, boolean isNew) throws Exception {
                  //do something - customer code is here
                  System.out.println("Before user is added into database");
              }
              public void preDelete(User user) throws Exception {
                  //do something - customer code is here
                  System.out.println("Before user is deleted from database");
              }
              public void postSave(User user, boolean isNew) throws Exception {
                  //do something - customer code is here      
                  System.out.println("After user is added into database");
              }
              public void postDelete(User user) throws Exception {
                  //do something - customer code is here
                  System.out.println("After user is deleted from database");
              }
          }

      See sample here.

    • GroupEventListener:

      To listen to group changes, you need to extend org.exoplatform.services.organization.GroupEventListener in MyGroupListener.java.

      package org.exoplatform.listener;
      
      
          import javax.jcr.Session;
          import org.exoplatform.services.organization.Group;
          import org.exoplatform.services.organization.GroupEventListener;
          
          public class MyGroupListener extends GroupEventListener {
              public void preSave(Group group, boolean isNew) throws Exception {
                  //do something - customer code is here
                  System.out.println("Before group is added into database");
              }
              public void preDelete(Group group) throws Exception {
                  //do something - customer code is here
                  System.out.println("Before group is removed from database");
              }
              public void postSave(Group group, boolean isNew) throws Exception {
                  //do something - customer code is here
                  System.out.println("After group is added into database");
              }
              public void postDelete(Group group) throws Exception {
                  //do something - customer code is here
                  System.out.println("After group is removed from database");         
              }
          }

      See sample here.

    • MembershipEventListener:

      To listen to membership changes, you need to extend org.exoplatform.services.organization.MembershipEventListener in MyMembershipListener.java.

      package org.exoplatform.listener;
      
      
          import javax.jcr.Session;
          import org.exoplatform.services.organization.Membership;
          import org.exoplatform.services.organization.MembershipEventListener;
          
          public class MyMembershipListener extends MembershipEventListener {
              public void preSave(Membership m, boolean isNew) throws Exception{
                  //do something - customer code is here
                  System.out.println("Before membership is added into database");
              }
              public void postSave(Membership m, boolean isNew) throws Exception{
                  //do something - customer code is here
                  System.out.println("After membership is added into database");
              }
              public void preDelete(Membership m) throws Exception{
                  //do something - customer code is here
                  System.out.println("Before membership is removed from database");
              }
              public void postDelete(Membership m) throws Exception{
                  //do something - customer code is here
                  System.out.println("After membership is removed from database");
              }
          }

      See sample here.

  4. Register your own listeners in configuration.xml. Registering the listeners is then achieved by using the ExoContainer plugin mechanism. See Service configuration for beginners for more information.

    To effectively register Organization listeners, you simply need to use the addListenerPlugin set-method.

    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <configuration
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd http://www.exoplatform.org/xml/ns/kernel_1_2.xsd"
       xmlns="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd">
        <external-component-plugins>
         <target-component>org.exoplatform.services.organization.OrganizationService</target-component>
          <component-plugin>
            <name>my.new.user.event.listener</name>
            <set-method>addListenerPlugin</set-method>
            <type>org.exoplatform.listener.MyUserListener</type>
            <description>description</description>
          </component-plugin>
          <component-plugin>
            <name>my.new.group.event.listener</name>
            <set-method>addListenerPlugin</set-method>
            <type>org.exoplatform.listener.MyGroupListener</type>
            <description>description</description>
          </component-plugin>
          <component-plugin>
            <name>my.membership.group.event.listener</name>
            <set-method>addListenerPlugin</set-method>
            <type>org.exoplatform.listener.MyMembershipListener</type>
            <description>description</description>
          </component-plugin>
        </external-component-plugins>
    </configuration>

    See sample here.

  5. Build your JAR project, then deploy the .jar file (in target/) under eXo Platform.

    • $PLATFORM_TOMCAT_HOME/lib (in Tomcat).

    • $PLATFORM_JBOSS_HOME/standalone/deployments/platform.ear!/lib (in JBoss).

  6. Start eXo Platform. Your own listeners are now ready for testing.

    To check if either of your own listeners takes effect, try the case when creating a new account by selecting UsersAdd Users. Once you have clicked Save, two strings will be printed to the console: "Before user is added into database" and "After user is added into database", as defined in MyUserListener.java.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus