5.1.3.21. How to add new HttpSessionListeners and/or ServletContextListeners to my portal without modifying the portal binary?

We added a GenericHttpListener that allows you to define new HttpSessionListeners and/or ServletContextListeners thanks to an external plugin. Actually, the GenericHttpListener will broadcast events thanks to the ListenerService that you can easily capture. The events that it broadcasts are:

If you want to listen to org.exoplatform.web.GenericHttpListener.sessionCreated, you will need to create a Listener that extends _Listener<PortalContainer, HttpSessionEvent>_If you want to listen to \_org.exoplatform.web.GenericHttpListener.sessionDestroyed_, you will need to create a Listener that extends _Listener<PortalContainer, HttpSessionEvent>_If you want to listen to \_org.exoplatform.web.GenericHttpListener.contextInitialized_, you will need to create a Listener that extends _Listener<PortalContainer, ServletContextEvent>_If you want to listen to \_org.exoplatform.web.GenericHttpListener.contextDestroyed_, you will need to create a Listener that extends Listener<PortalContainer, ServletContextEvent>

See an example of configuration below:


<?xml version="1.0" encoding="UTF-8"?>
<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>
    <!-- The full qualified name of the ListenerService -->
    <target-component>org.exoplatform.services.listener.ListenerService</target-component>
    <component-plugin>
      <!-- The name of the listener that is also the name of the target event -->
      <name>org.exoplatform.web.GenericHttpListener.sessionCreated</name>
      <!-- The name of the method to call on the ListenerService in order to register the Listener -->
      <set-method>addListener</set-method>
      <!-- The full qualified name of the Listener -->
      <type>org.exoplatform.sample.ext.web.SampleHttpSessionCreatedListener</type>
    </component-plugin>
    <component-plugin>
      <!-- The name of the listener that is also the name of the target event -->
      <name>org.exoplatform.web.GenericHttpListener.sessionDestroyed</name>
      <!-- The name of the method to call on the ListenerService in order to register the Listener -->
      <set-method>addListener</set-method>
      <!-- The full qualified name of the Listener -->
      <type>org.exoplatform.sample.ext.web.SampleHttpSessionDestroyedListener</type>
    </component-plugin>
    <component-plugin>
      <!-- The name of the listener that is also the name of the target event -->
      <name>org.exoplatform.web.GenericHttpListener.contextInitialized</name>
      <!-- The name of the method to call on the ListenerService in order to register the Listener -->
      <set-method>addListener</set-method>
      <!-- The full qualified name of the Listener -->
      <type>org.exoplatform.sample.ext.web.SampleContextInitializedListener</type>
    </component-plugin>
    <component-plugin>
      <!-- The name of the listener that is also the name of the target event -->
      <name>org.exoplatform.web.GenericHttpListener.contextDestroyed</name>
      <!-- The name of the method to call on the ListenerService in order to register the Listener -->
      <set-method>addListener</set-method>
      <!-- The full qualified name of the Listener -->
      <type>org.exoplatform.sample.ext.web.SampleContextDestroyedListener</type>
    </component-plugin>
  </external-component-plugins>
</configuration>

See an example of Session Listener below:

SampleHttpSessionCreatedListener.java

..

import org.exoplatform.container.PortalContainer;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import javax.servlet.http.HttpSessionEvent;
public class SampleHttpSessionCreatedListener extends Listener<PortalContainer, HttpSessionEvent>
{
   @Override
   public void onEvent(Event<PortalContainer, HttpSessionEvent> event) throws Exception
   {
      System.out.println("Creating a new session");
   }
}

See an example of Context Listener below:

SampleContextInitializedListener.java

..

import org.exoplatform.container.PortalContainer;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import javax.servlet.ServletContextEvent;
public class SampleContextInitializedListener extends Listener<PortalContainer, ServletContextEvent>
{
   @Override
   public void onEvent(Event<PortalContainer, ServletContextEvent> event) throws Exception
   {
      System.out.println("Initializing the context");
   }
}
Copyright ©2012. All rights reserved. eXo Platform SAS