How to Write Java Mapping in SAP PI

This blog will help you learn how to Write Java Mapping in SAP PI PO.

Java Mapping is a Java based program that is written in Eclipse or NWDS Editor with Java Mapping Libraries and then uploaded in the ESR.

Java Mapping is alternative approach to writing Graphical Message Mapping.

Steps to Develop Java Mapping:

  1. Download NWDS.
  2. Download Java Mapping Libraries.
  3. Download & Install Java 8.
  4. Configure NWDS with Java SDK.
  5. Create a New Java Project.
  6. Add the Java Mapping Libraries.
  7. Create the Java Mapping Class
  8. Build the Java Mapping Jar / Zip File.
  9. Upload the Java Mapping in SAP PI (ESR) via Imported Archives.

Download NWDS:

SAP Netweaver Developer Studio is a Eclipse based Java IDE that is used to write the Java Mapping.

First step is to download NWDS on your local machine and open it.

There are two ways to get the NWDS Software:

  • Download the NWDS from here.
  • Download NWDS from SAP Marketplace.
    • Software Download  -> SUPPORT PACKAGES AND PATCHES -> By Alphabetical Index (A-Z) -> N -> SAP NETWEAVER -> SAP NETWEAVER 7.x -> DEVELOPER STUDIO -> NW DEVELOPER STUDIO 7.x

Download Java Mapping Libraries:

Before you start building the Java Mapping you need to have supported Java Libraries that can help you build the code.

Download the Java Mapping Libraries from here.

You need to have the following Java Mapping Libraries:

  • aii_af_mp.jar
  • aii_af_ms_api.jar
  • aii_af_trace.jar
  • com.sap.xpi.ib.mapping.lib.jar

These libraries are for SAP Process Orchestration 7.5 and Java 8.

If you need Java Mapping Libraries for earlier version then contact me.

Download & Install Java 8:

SAP Process Orchestration 7.5 runs on Java 8 so you need to download the Java SDK 8 from Oracle.

Download the Java 8 from here.

Once you download the Java 8, Install the software and it will configure Java on your Machine.

After the installation is complete, open the command prompt and type the following command to verify if the Java is properly installed or not.

java on cmd

Make sure when you type > java -version it will the Java 8 in the output.

Configure NWDS with Java SDK:

Once you have downloaded the NWDS Software and Java is installed on your local machine next step is to open NWDS.

Open the NWDS installation folder and find eclipse.ini file.

Open eclipse.ini file and configure the bin path for Java 8 that you have just installed on your local machine.

Java is typically found in the “C:\Program Files\Java”

Make sure the following lines are added to your eclipse.ini file:

openFile
-vm
C:\Program Files\Java\jdk1.8.0_60\bin\javaw.exe
-vmargs

Create a New Java Project:

Now that you have opened the NWDS, it is time to create the Java Project.

File -> New -> Java Project

create new java project

Create the Java Project with the following Settings:

Create Java Project

Add the Java Mapping Libraries:

Once you have create the Java Project, next step is to add the Java Mapping Libraries to your project.

Open the Build Path by right click on your Project:

Open Build Path

Add the Java Mapping Libraries -> Add External Jars

add external jars

Create the Java Mapping Class:

Create a New Class File and Add the following Code:

/***** Standard Java Libraries ********/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

/***** PI Specific Libraries *********/
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class JM_SampleJavaMapping_PI extends AbstractTransformation {

BufferedReader docInput;

String inputLine = “”;
String messageID = “”;

TransformationInput transInput = null;
TransformationOutput transOutput = null;

StringBuffer sourcePayload = new StringBuffer();
StringBuffer destPayload = new StringBuffer();

public static AbstractTrace trace;

public boolean traceon;

public JM_SampleJavaMapping_PI() throws Exception
{
docInput = null;
}

public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException
{

trace = (AbstractTrace) getTrace(); //Capture trace object and write trace for debugging purpose.
messageID = arg0.getInputHeader().getMessageId(); //Message ID only available if run from PI.
traceon = true; //make it false if wants to run the program locally. Make it true always before compiling the code and deployment.

if ( traceon ){ trace.addInfo(“Java Mapping Program Started!”); }else{ System.out.println(“Java Mapping Program Started!”); }

transInput = arg0;
transOutput = arg1;

this.execute((InputStream) arg0.getInputPayload().getInputStream(), (OutputStream) arg1.getOutputPayload().getOutputStream());

if ( traceon ){ trace.addInfo(“Java Mapping Program Completed!”); }else{ System.out.println(“Java Mapping Program Completed!”); }
}

public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

try {

if ( traceon ){ trace.addInfo(“Inside the Execute Method!”); }else{ System.out.println(“Inside the Execute Method!”); }

//Open the input Stream
try
{
docInput = new BufferedReader(new InputStreamReader(in));
}
catch (Exception e)
{
throw new StreamTransformationException(“err1: Unable to parse input document – “.concat(e.getMessage()));
}

//Read the data – The Data is available in inputData
try
{
while ((inputLine = docInput.readLine()) != null) {
sourcePayload.append(inputLine);
}
}catch (Exception e){
throw new StreamTransformationException(“err2: Error in reading the payload – “.concat(e.getMessage()));
}

//Write the Data
try
{
out.write(new String(sourcePayload).getBytes(“UTF-8”));
}
catch(Exception e) {
throw new StreamTransformationException(“err3: Error in Writing the payload back – “.concat(e.getMessage()));
}

out.flush(); //flush the stream
if (traceon){ trace.addInfo(“Outside the Execute Method!”); }else{ System.out.println(“Outside the Execute Method!”); }

} catch (Exception e) {

if( traceon ){ trace.addWarning(“[Execute Method]**Error. ” + e); }else{ System.out.println(“[Execute Method]**Error. ” + e); }
}
}

/**
* @param args
*/
public static void main(String[] args) {
// This method is executed only on the local running and no effect running from PI mapping program
try {
FileInputStream fin = new FileInputStream(“inputfile.txt”);
FileOutputStream fout = new FileOutputStream(“outputfile.txt”);

JM_SampleJavaMapping_PI instance = new JM_SampleJavaMapping_PI();
instance.traceon = false; //This will help to print the output instead of trace.
instance.execute(fin, fout);
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (StreamTransformationException e) {

e.printStackTrace();
} catch (Exception e) {

e.printStackTrace();
}

}
}

/************************************************************************************************************
//BONUS CODE: TO BINARY READ AND WRITE – GOOD WHEN NOTHING TO MASSAGE WITH DATA – FASTEST AS WELL
int buffer;
if ( traceon ){ trace.addInfo(“Inside the Execute Method!”); }else{ System.out.println(“Inside the Execute Method!”); }

//Read and Write back the file
while ((buffer = in.read()) != -1)
{
out.write(buffer);
}

out.flush();

if (traceon){ trace.addInfo(“Outside the Execute Method!”); }else{ System.out.println(“Outside the Execute Method!”); }
*************************************************************************************************************/

Build the Java Mapping Jar / Zip File:

Find the java class file in the bin folder of your project folder.

Right and create the .zip file.

create the zip file

Upload the Java Mapping in SAP PI (ESR) via Imported Archives:

Open the ESR -> Right Click on your Namespace:

imported archive

Import the Zip File in the Imported Archive:

imported archive java mapping

Srini
About the author

Srini - is an IT Architect who enjoys sharing knowledge on Computer Programming and Digital Marketing. Take a look at the Courses and Books.

Leave a Comment