Skip to Content
Author's profile photo Former Member

Multiple directories dynamically using Custom Adapter Module

Hi All,

Below is the JAVA code for Adapter Module which I wrote for setting target directory based on source directory information.  The requirement is to read the Folder name where the files are placed and as per that Folder name we need to set the target directo

There are many nice blogs to tell about how to use NWDS for creating modules so not putting that information. Only the code for the Adapter Module I feel will be helpful for PI consultants.

package com.pi.adaptermodule;

import javax.ejb.Stateless;

import java.rmi.RemoteException;

import java.util.Timer;

import javax.ejb.EJBException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import com.sap.aii.af.lib.mp.module.Module;

import com.sap.aii.af.lib.mp.module.ModuleContext;

import com.sap.aii.af.lib.mp.module.ModuleData;

import com.sap.aii.af.lib.mp.module.ModuleException;

import com.sap.aii.af.service.auditlog.Audit;

import com.sap.engine.interfaces.messaging.api.Message;

import com.sap.engine.interfaces.messaging.api.MessageKey;

import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;

import com.sap.engine.interfaces.messaging.api.XMLPayload;

import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

/**

* Session Bean implementation class DynamicDirectoryBean

*/

@Stateless

public class DynamicDirectoryBean implements DynamicDirectoryBeanRemote, DynamicDirectoryBeanLocal,SessionBean, Module {

    /**

     * Default constructor.

     */

    public DynamicDirectoryBean() {

        // TODO Auto-generated constructor stub

    }

    public ModuleData process(ModuleContext moduleContext,ModuleData inputModuleData)throws ModuleException {

     Object obj = null;

     Message msg = null;

     MessageKey amk = null;

     String inputDir = null;

     String outputDir = null;

     String dirpath1 = moduleContext.getContextData(“dirpath1”);

     String dirpath2 = moduleContext.getContextData(“dirpath2”);

     String dirpath3 = moduleContext.getContextData(“dirpath3”);

     String dirpath4 = moduleContext.getContextData(“dirpath4”);

     String dirpath5 = moduleContext.getContextData(“dirpath5”);

     String dirpath6 = moduleContext.getContextData(“dirpath6”);

     String folderName1 = moduleContext.getContextData(“folderName1”);

     String folderName2 = moduleContext.getContextData(“folderName2”);

     String folderName3 = moduleContext.getContextData(“folderName3”);

     String folderName4 = moduleContext.getContextData(“folderName4”);

     String folderName5 = moduleContext.getContextData(“folderName5”);

     String folderName6 = moduleContext.getContextData(“folderName6”);

     try{

     // Retrieves the current principle data, usually the message , Return type is Object

     obj = inputModuleData.getPrincipalData();

     // A Message is what an application sends or receives when interacting with the Messaging System.

     msg = (Message) obj;

     // MessageKey consists of a message Id string and the MessageDirection

     amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());

     // Audit log message will appear in MDT of Channel Monitoring

     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,”Dynamic Directory Module called”);

     // Returns the main document as XMLPayload

     XMLPayload xpld = msg.getDocument();

     //Reading the directory from message header

     MessagePropertyKey mpk = new MessagePropertyKey(“Directory”,”http://sap.com/xi/XI/System/File“);

     inputDir = msg.getMessageProperty(mpk);

     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “directory is” + inputDir );

     if(inputDir.contains(folderName1))

     {

           outputDir = dirpath1;  

     }

     else if (inputDir.contains(folderName2))

     {

           outputDir = dirpath2;

     }

     else if (inputDir.contains(folderName3))

     {

           outputDir = dirpath3;

     }

     else if (inputDir.contains(folderName4))

     {

           outputDir = dirpath4;

     }

     else if (inputDir.contains(folderName5))

     {

           outputDir = dirpath5;

     }

     else if (inputDir.contains(folderName6))

     {

           outputDir = dirpath6;

     }

     //setting directory in the message header

     MessagePropertyKey dir = new MessagePropertyKey(“Directory”,”http://sap.com/xi/XI/System/File“); 

     msg.setMessageProperty(dir,outputDir);

     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “target directory is ” + outputDir );

     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “Directory is Successfully set”);

     // Sets the principle data that represents usually the message to be processed

     Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “Message Successfully updated”);

     inputModuleData.setPrincipalData(msg);

     return inputModuleData;

     }

     catch (Exception e) {

              ModuleException me = new ModuleException(e);

              throw me;

     }

    }

    /* (non-Javadoc)

     * @see javax.ejb.SessionBean#ejbActivate()

     */

     public void ejbActivate() throws EJBException, RemoteException {

     // TODO Auto-generated method stub

     }

     /* (non-Javadoc)

     * @see javax.ejb.SessionBean#ejbPassivate()

     */

     public void ejbPassivate() throws EJBException, RemoteException {

     // TODO Auto-generated method stub

     }

     /* (non-Javadoc)

     * @see javax.ejb.SessionBean#ejbRemove()

     */

     public void ejbRemove() throws EJBException, RemoteException {

     // TODO Auto-generated method stub

     }

     /* (non-Javadoc)

     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)

     */

     public void setSessionContext(SessionContext context) throws EJBException,RemoteException {

     }

     /* (non-Javadoc)

     * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)

     */

     public void ejbTimeout(Timer arg0) {

     // TODO Auto-generated method stub

     }

     public void ejbCreate() throws javax.ejb.CreateException {

     }

}

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Iñaki Vila
      Iñaki Vila

      Hi Ashutosh Upadhyay,

      First of all thanks for sharing. However, your idea is similar that the Amit Srivastava's blog Send File to Two Different Locations using Adapter Module. I know there are differences because your module let to send to six locations and Amit's one two locations.

      Unfortunately, may be is not possible i miss one option to send to N different locations. I have one question for you, Could be possible to do moduleContext.getContextData("dirpath*"); in order to can generate infinite possible path parameters?

      Regards.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Inaki,

      It was specific requirement which I achieved with this java code. However we can modify the code ( I still need to code it and test it) by passing a parameter in adapter module to decide how many directories we need to choose and for dirPath we can take an array. Let me check if that logic works well with Adapter Module and then I will confirm it.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi I have not tried it but made this logic I hope this helps. We can take count or number of folders we want and then using the loops we can set the parameters equals to the n.

      So with below code you can take the approach like suppose 3 directories you want to handle then pass count as 3 and then you need to pass parameters as input0, input2, input3.

            int i=0;

          String count = moduleContext.getContextData("count");
          int iCount =Integer.parseInt(count);

              String[] input = new String[iCount];

              String[] inputParameters = new String[iCount];

              String str = "";

          for (i=0;i<iCount;i++){
      str = String.valueOf(i);
      input[i]="input"+str;
          }
          for (i=0;i<iCount;i++){
      inputParameters[i]=moduleContext.getContextData(input[i]);
          }