Java handler example

Example code explores the capabilities of the Java™ handler.

Attention: Due to the complexities of the underlying engine runtime environment, custom handlers must not reside within the com.initiatesystems.hub.handler Java package. If you are upgrading from a pre-9.2 version of the software, then care must be taken to ensure that existing handler classes do not conflict with the com.initiatesystems.hub.handler package. If you change the package of a handler class, then the corresponding metadata must be updated in the InfosSphere MDM Workbench project file.
package com.foo.initiate.mds.handler;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Map;

import madison.handler.CallbackHandlerException;
import madison.handler.HandlerExtBase;
import madison.handler.IService;
import madison.mpi.Context;
import madison.mpi.RowIterator;
import madison.mpi.RowList;

public class FileHandler extends HandlerExtBase
{
   // Class-specific members used as keys in the handlerArgs to identify the 
the pre and post-ixn filename(s)
   private static final String ARG_PREFILENAME = "preFileName";
   private static final String ARG_POSTFILENAME = "postFileName";

   // Instance-specific members initialized during the overridden init() 
method
   private String preFileName = null;
   private String postFileName = null;

   public void init(Context ctx, String handlerArgs)
   {
     Map argsMap = parseArgs(handlerArgs);
     String madHomeDir = System.getenv("MAD_HOMEDIR");
      
     // Create the pre- and post- files in the MAD_HOMEDIR directory 
     if (argsMap.containsKey(ARG_PREFILENAME))
     {
       preFileName = madHomeDir + File.separator + 
String)argsMap.get(ARG_PREFILENAME);
     }
     if (argsMap.containsKey(ARG_POSTFILENAME))
     {
       postFileName = madHomeDir + File.separator + 
(String)argsMap.get(ARG_POSTFILENAME);
     }
   }
   protected void writeFile(String fileName, RowList rowList)
   {
     PrintWriter out = null;
     try
     {
       out = new PrintWriter(new BufferedWriter(new 
FileWriter(fileName)));
        
       for (RowIterator i = rowList.rows(); i.hasMoreRows(); )
       {
         out.println(i.nextRow().toString());
       }
     }
     catch (Exception e)
     {
       e.printStackTrace();
     }
     finally
     {
       if (out != null)
       {
         try
         {
           out.close();
         }
         catch (Exception e)
         {
         }
       }
     }
   }
   public void preIxn(IService service) throws CallbackHandlerException
   {
     if (preFileName != null)
     {
       writeFile(preFileName, service.getInpMemRowList());
     }
   }
   public void postIxn(IService service) throws CallbackHandlerException
   {
     if (postFileName != null)
     {
       writeFile(postFileName, service.getOutMemRowList());
     }
   }
}