Technical Articles
DynamicConfiguration variable in UDF, JavaMap, JavaAdpterModule
Overview:
This blog’s main objective is to consolidate different techniques to use DynamicConfiguration variables in SAP PI/PO.
DynamicConfiguration variable in Java-UDF:
Here, just to highlight the java-code-syntax, below code reference is been given. The Java-UDF is a user defined function to be used in “Graphical Mapping (Message Mapping)”.
// Use of DynamicConfigurationVariable "FileName" in Java-UDF (in Graphical-Map)
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
//1. Read "FileName" variable
String strFileName = "";
strFileName = conf.get(key); //Get the Key-Value
//2. Assign value to "FileName" variable
strFileName = "CustName.asc"; //or get dynamic value from source inputXml-fields
conf.put(key, strFileName); //Set the Key-Value
DynamicConfiguration variable in Java-Map:
Here, just to highlight the java-code-syntax, below code reference is been given:
// Use of DynamicConfigurationVariable "FileName" in Java-Map
DynamicConfiguration conf = (DynamicConfiguration)param.get("DynamicConfiguration");
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
//1. Read "FileName" variable
String strFileName = "";
strFileName = conf.get(key); //Get the Key-Value
//2. Assign value to "FileName" variable
strFileName = "CustName.asc"; //or get dynamic value from source inputXml-fields
conf.put(key, strFileName); //Set the Key-Value
For complete JavaMap code-example following blog link can be referred:
DynamicConfiguration variable in custom Java-AdapterModule:
In custom Java Adapter module, we take use of MessagePropertyKey which is similar to DynamicConfiguration variable.
For example, we have following business requirement:
- In SAP-R3, ABAP program generates 3 different plant’s zip file (having .csv files) in a common directory (of t-ode AL11 directory). These zip files is been named based on plant code for example for plant-1 file-name is 01.zip and plant-2 file-name is 02.zip vice versa. This ABAP program is been scheduled to generate files on weekly basis.
- Next these files need to be transferred to SFTP-server. For same, one SAP-PI/PO passthrough interface is been designed (File to SFTP).
- But the SFTP server has dedicated directories for each plant files. SAP-PI/PO need to push each plant file to its respective SFTP-directory. For example:
- 01.zip should go to /sftpRoot/Plant_01/
- 02.zip should go to /sftpRoot/Plant_02/
- 03.zip should go to /sftpRoot/Plant_03/
- In SAP-PI/PO interface, using sender FILE adapter, files will be read from SAP-R3 directory. Here we enable ASMA properties in channel.
- and using receiver SFTP adapter, plant wise files will be placed in to SFTP-directories. Here, we make use of custom java-adapter-module in module tab to set dynamic directory based on incoming filename.
Now to create custom java-adapter-module, for detailed steps, below blog link can be referred:
And as per above business requirement, Java code (of custom java-adapter-module) is as below:
/**
*
*/
package com.sap.adaptermodule;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
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.auditlog.AuditLogStatus;
/**
* @author
*
*/
public class copyToDynamicFolderBean implements SessionBean, TimedObject {
/* (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 arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
}
/* (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 {
}
private SessionContext myContext;
MessageKey amk = null;
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException{
Object obj = null;
Message msg = null;
String file_destination_payroll_01 = "";
String file_destination_payroll_02 = "";
String file_destination_payroll_03 = "";
try
{
obj = inputModuleData.getPrincipalData();
msg = (Message) obj;
amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Start of Custom-JavaAdapterModule JNDI copyToDynamicFolder ................");
//1. Read all MessageProperty (or DynamicConfigurationVariable)
ArrayList<String> dynmPrLst = new ArrayList<String>();
for (MessagePropertyKey key : msg.getMessagePropertyKeys()) {
String propertyName = key.getPropertyName(); //"FileName"
String propertyNamespace = key.getPropertyNamespace(); //"http://sap.com/xi/XI/System/File"
String propertyValue = msg.getMessageProperty(key); //"01.zip"
String propertyVal = "";
propertyVal = propertyName + "_" + propertyNamespace + "_" + propertyValue;
dynmPrLst.add(propertyVal);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("MessageProperty " + propertyVal));
}
//2. Read "FileName" MessageProperty(or DynamicConfigurationVariable)
String sourceFileName = "";
for(int i=0;i<dynmPrLst.size();i++){
String dynmParmStr = dynmPrLst.get(i).trim();
if(dynmParmStr.indexOf("FileName_http://sap.com/xi/XI/System/File") > -1){
sourceFileName = dynmParmStr.substring(dynmParmStr.lastIndexOf("_")+1);
break;
}
}
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("Source FileName is " + sourceFileName));
/**
* 3. Read value of Module-Input-Parameters (set in channel's Module)
* For example:
* ModuleInputParameter file_destination_payroll_01
* Value <filename>, <destinationFolder>
* Value 01.zip, /Folder1/Folder2/
*/
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Reading module input parameters.....");
file_destination_payroll_01 = (String) moduleContext.getContextData("file_destination_payroll_01");
file_destination_payroll_02 = (String) moduleContext.getContextData("file_destination_payroll_02");
file_destination_payroll_03 = (String) moduleContext.getContextData("file_destination_payroll_03");
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("file_destination_payroll_01 " + file_destination_payroll_01));
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("file_destination_payroll_02 " + file_destination_payroll_02));
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("file_destination_payroll_03 " + file_destination_payroll_03));
/**
* 4. Check if "FileName" matches to any of ModuleInputParameters
* and if found then set respective destination to "Directory" MessageProperty(or DynamicConfigurationVariable)
*/
directoryPropertyValue = "";
getDirectoryVal(sourceFileName, file_destination_payroll_01);
getDirectoryVal(sourceFileName, file_destination_payroll_02);
getDirectoryVal(sourceFileName, file_destination_payroll_03);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ( "File " + sourceFileName + " to be sent to directory " + directoryPropertyValue));
/**
* 5. Set "Directory" MessageProperty(or DynamicConfigurationVariable)
*/
MessagePropertyKey sKey1 = new MessagePropertyKey("Directory","http://sap.com/xi/XI/System/File");
msg.setMessageProperty(sKey1, directoryPropertyValue);
inputModuleData.setPrincipalData(msg); //set msg property again as principal data
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("Setting new MessageProperty as: Namespace=http://sap.com/xi/XI/System/File Name=Directory Value=" + directoryPropertyValue));
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "End of Custom-JavaAdapterModule JNDI copyToDynamicFolder ..................");
return inputModuleData;
}catch (Exception e) {
ModuleException me = new ModuleException(e);
throw me;
}
}
private static String directoryPropertyValue;
private static void getDirectoryVal(String sourceFileName, String file_destination) {
//file_destination = "file1.zip, /abc/dyg/xyz/";
if(file_destination.trim().length() > 0){
String fNm = (file_destination.substring(0, file_destination.indexOf(","))).trim();
if(sourceFileName.equals(fNm)){
String destn = (file_destination.substring(file_destination.indexOf(",") + 1)).trim();
if(destn.length() > 0){
directoryPropertyValue = destn; //if destination found only then set value to global var
}
}
}
}
}