5.1.2.7. Use init tasks if you need a PortalContainer to initialize an Http Filter or an HttpServlet

If your Http Filter or your HttpServlet requires a PortalContainer to initialize, you need to convert your code in order to launch the code responsible for the initialization in the method onAlreadyExists of an org.exoplatform.container.RootContainer.PortalContainerInitTask.

We need to rely on init tasks, in order to be sure that the portal container is at the right state when the task is executed, in other words the task could be delayed if you try to execute it too early. Each task is linked to a web application, so when we add a new task, we first retrieve all the portal containers that depend on this web application according to the PortalContainerDefinitions, and for each container we add the task in a sorted queue which order is in fact the order of the web applications dependencies defined in the PortalContainerDefinition. If no PortalContainerDefinition can be found we execute synchronously the task which is in fact the old behavior (i.e. without the starter).

The supported init tasks are:

An init task is defined as below:

PortalContainerPreInitTask

   /**

    * This interface is used to define a task that needs to be launched at a given state during the
    * initialization of a portal container
    */
   public static interface PortalContainerInitTask
   {
      /**
       * This method allows the implementation to define what the state "already exists"
       * means for a portal container
       *
       * @param portalContainer the value of the current portal container
       * @return <code>true</code> if the portal container exists according to the task
       * requirements, <code>false</code> otherwise
       */
      public boolean alreadyExists(PortalContainer portalContainer);
      /**
       * This method is called if the related portal container has already been registered
       *
       * @param context the servlet context of the web application
       * @param portalContainer the value of the current portal container
       */
      public void onAlreadyExists(ServletContext context, PortalContainer portalContainer);
      /**
       * Executes the task
       *
       * @param context the servlet context of the web application
       * @param container The portal container on which we would like to execute the task
       */
      public void execute(ServletContext context, PortalContainer portalContainer);
      /**
       * @return the type of the task
       */
      public String getType();
   }

To add a task, you can either call:

We will take for example the class GadgetRegister that is used to register new google gadgets on a given portal container.

The old code was:

Old GadgetRegister.java

...

public class GadgetRegister implements ServletContextListener
{
...
  public void contextInitialized(ServletContextEvent event)
  {
    try
    {
      ExoContainer pcontainer =  ExoContainerContext.getContainerByName("portal") ;
      SourceStorage sourceStorage = (SourceStorage)pcontainer.getComponentInstanceOfType(SourceStorage.class);
      ...
  }
  ...
}

The new code relies on a org.exoplatform.container.RootContainer.PortalContainerPostInitTask, as you can see below

New GadgetRegister.java

...

public class GadgetRegister implements ServletContextListener {
...
   public void contextInitialized(ServletContextEvent event)
   {
      // Create a new post init task
      final PortalContainerPostInitTask task = new PortalContainerPostInitTask()
      {
         public void execute(ServletContext context, PortalContainer portalContainer)
         {
            contextInitialized(context, portalContainer);
         }
      };
      // Add the init task to all the related portal containers
      PortalContainer.addInitTask(event.getServletContext(), task);
   }
   private void contextInitialized(ServletContext context, PortalContainer pcontainer)
   {
      try
      {
         SourceStorage sourceStorage = (SourceStorage)pcontainer.getComponentInstanceOfType(SourceStorage.class);
         ...
   }
  ...
}
Copyright ©2012. All rights reserved. eXo Platform SAS