You are on page 1of 15

1/30/2014

Search

Java Mapping in SAP PI / XI Step-by-step example Techplay

Search

Techplay
./techplay
Home
About

Lego Mindstorms: The perfect place to start with robotics


The Game
Aug 18

Java Mapping in SAP PI / XI Step-by-step example


Categories:
SAP PI / XI
by admin
I often had requests from friends on how to create Java Mappings in SAP PI (SAP XI) so I thought I would write this post to help any others out there who
may be struggling. The Java Mapping in SAP PI allows businesses to leverage Java skills that they already have for PI message mappings and can also help
them to re-use Java code which may have been written for other applications.
It also provides extended functionality in situations where the graphical mapping may be insufficient, inefficient, or possibly unmaintainable. An example of this
would be the situation where a specific string of text needs to be replaced at any occurrence in the source message. This is the example I will be
demonstrating.
Requirements
First you will need the Java SDK installed on the development machine. The version depends on the version of SAP PI you wish to deploy onto but this is not
a strict requirement, however it does eliminate possible issues. For SAP PI 7.0 I would recommend using JDK 1.4, whereas for SAP PI 7.1 it would be Java
5.0. This is simply because these are the versions of the Java runtime used by SAP PI on those respective versions.
Second, you will need to obtain the library aii_map_api.jar from the SAP PI server. I usually keep a local copy since one does not always have access to
the SAP PI operating system. The library can be obtained in the path:
<SAP_install_dir>/<system_name>/<instance_name>/j2ee/cluster/server<number>/apps/sap.com/com.sap.xi.services/
An example of the path on a Windows server could be:
D:\usr\sap\xid\DVEBMGS03\j2ee\cluster\server0\apps\sap.com\com.sap.xi.services.
Lastly, you will want a Java IDE, but this is entirely optional if you prefer to compile on the command line. I usually use Eclipse and in this manual I
demonstrate using Eclipse, but there will be similar ways to do the steps in other IDEs.
http://techplay.plozzle.com/?p=21

1/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Getting the Project Set Up


The next part is just about getting your Java project setup with a reference to the mapping library. If you are familiar with this process, you can happily skip
through to the next section on writing the code.
Open your favorite flavor of Eclipse and select File -> New -> Project. The following dialog box should appear. Select Java Project and click Next.

Give the project a name and select the correct Java runtime. In my case I selected JRE 1.4.2. Then click Finish.

http://techplay.plozzle.com/?p=21

2/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

This will then have created your project. Right-click on your project and select Properties. Select Java Build Path and select the Libraries tab. This will
give you the following screen.

http://techplay.plozzle.com/?p=21

3/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Click on Add External JARS and then find the location of the library aii_map_api.jar mentioned in the requirements above.

After adding the library, you should get the following screen.

http://techplay.plozzle.com/?p=21

4/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Click on OK and now your environment is setup up.


Writing the Code
We start by creating a Class which implements the StreamTransformation interface contained in aii_map_api.jar. The mapping I will create is very basic and
simply replaces any occurrences of the word remove with the word delete, as well as writing to the mapping trace.
Start by creating a new Class as follows. Right-click on the project name ->select New -> Class as shown below.

You should have the dialog box below. Enter a package name optionally and then enter a Class name and click add to add an interface.

http://techplay.plozzle.com/?p=21

5/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Select the StreamTransformation interface and select OK.

http://techplay.plozzle.com/?p=21

6/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

You should now have the screen below. Click Finish to create the Class.

You will now have something like the following image. The method execute is where the mapping occurs and is where you would insert your code. Input
comes in the form of the InputStream arg0, and output is passed in the OutputStream arg1. The method setParameter allows you to obtain a reference to the
mapping parameters. In my scenario I used this to access the mapping trace.

Below is an example mapping program I created.

package com.sap.pi.demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;

http://techplay.plozzle.com/?p=21

7/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
public class TextMapping implements StreamTransformation {
private Map map = null;
private AbstractTrace trace = null;
public void setParameter(Map arg0) {
map = arg0;

// Store reference to the mapping parameters

if (map == null) {
this.map = new HashMap();
}
}
public void execute(InputStream arg0, OutputStream arg1)
throws StreamTransformationException {
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(arg0));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(arg1));
// Get reference to mapping trace
trace = (AbstractTrace)map.get(StreamTransformationConstants.MAPPING_TRACE);
trace.addInfo("Processing message");
try {
//Read all lines of input stream arg0
while ((line=br.readLine()) != null) {
//Write output to output stream arg1
bw.write(line.replaceAll("remove", "delete"));
}
bw.flush();
br.close();
bw.close();
} catch (IOException ioe) {
trace.addWarning("Could not process source message");
throw new RuntimeException(ioe.getMessage());
}
trace.addInfo("Processing completed successfully");
}
}

Importing the Java mapping into the Integration Repository (Enterprise Services Repository)
Once youve completed your mapping code and would like to import it into SAP PI start by right-clicking on you project and then select Export

http://techplay.plozzle.com/?p=21

8/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Select JAR file as below and then click Next.

Click Browse and find a suitable location to store you exported archive and then click Finish.

http://techplay.plozzle.com/?p=21

9/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Now go to the Software Component and namespace you wish to import the mapping into. Right-click on Imported Archives and select New

Give the new archive a sensible name.

Click on the green Import Archive button.

Find the Jar file that was previously exported and select Open.

http://techplay.plozzle.com/?p=21

10/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Be sure to save and activate this. Without activating the mapping will not be available for selection.

Now create your interface mapping as follows and then give it a sensible name.

Click on Read Interfaces to pull in the interface information.

http://techplay.plozzle.com/?p=21

11/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

From the dropdown in the Mapping Program section select Java Class.

Select your Java Mapping Class from the search help under name.

Now you can test your Interface Mapping. The below shows the test of the mapping I created earlier. Note that the word remove gets replaced by
delete and we write two lines to the mapping trace.

http://techplay.plozzle.com/?p=21

12/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

And youre done!


Let me know if there are any questions. Id be more than happy to assist.
Regards,
Y.
Tags: example, Java mapping, PI, SAP, SAP PI, SAP XI, XI
5 comments
Skip to comment form

1.
hari

September 27, 2010 at 13:13 (UTC 2)


Reply
good

2.
Murugan

November 16, 2010 at 00:20 (UTC 2)


Reply
Thank you very much boss, its really good.

3.
Ali

June 14, 2011 at 18:41 (UTC 2)


http://techplay.plozzle.com/?p=21

13/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

Reply
Really great job man
Thanks
Ali

4.
kevin

September 14, 2011 at 16:43 (UTC 2)


Reply
Hi,
I followed each step, in Eclipse there is a warning at Map: is a raw type
private Map map = null;
public void setParameter(Map arg0) {
this.map = new HashMap();
and when i import .JAR file on PI,the class file doesnt open gives error Unable to display class; byte code format invalid

1.
admin

September 25, 2011 at 08:31 (UTC 2)


Reply
Hi Kevin,
First of all which version of SAP PI are you using?
Note that you must use the correct JDK version for your specific version. This will correspond to the JVM version of the Java stack.
Aside from this, try renaming your JAR file to have a .zip extension and then open this. Check if the classes youve defined are in this archive.
Hope that helps.

Leave a Reply
Your email address will not be published. Required fields are marked *
Name: *
Email: *
Website:

Message: *
http://techplay.plozzle.com/?p=21

14/15

1/30/2014

Java Mapping in SAP PI / XI Step-by-step example Techplay

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite="">
<cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Type the text


Privacy & Terms

Submit Comment

Copyright
2014 Techplay.
Return to top
Powered by WordPress and the Graphene Theme.

http://techplay.plozzle.com/?p=21

15/15

You might also like