1/30/2014
Search
Java Mappi ng in SAP PI / XI – Step- by- step exampl e » Techpl ay
Search
Techpl Te chplay ay ./techplay Home About Ab out
« Lego Mi Min ndstorms: The perfect place to start with robotics The Game » 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 strugglin struggling. g. The Java Mapping Ma pping in in SAP PI P I allows businesse businessess to leverage Java skil sk ills ls that they already have for PI message messa ge mappings mappings and can also help them to re-use Java code which may have been written for other applications. It also provides p rovides extended extend ed functionality in situations where the graphical grap hical mapp mapping ing may be insuffi insufficient, cient, inefficient, inefficient, or o r possibly pos sibly unmai unmaintainable. ntainable. An example of this this would be the situation situation where a specifi sp ecificc string of text needs to be replaced re placed at a t any occurrence in the source message. This This is the example example I will will be demonstrating. Requirements
First you will will need the Java SDK install installed ed on o n the development machin machine. e. The version depends on the version of SAP PI P I you wish to deploy dep loy onto but this this is not a strict requirement, requirement, however it does elimin eliminate ate possible po ssible issues. issues. For SAP S AP PI 7.0 7. 0 I would recommend recommend using using JDK 1.4, 1. 4, whereas for SAP PI 7.1 7. 1 it would would be Java J ava 5.0. This This is simply simply because these are ar e the versions of the Java runtime runtime used by SAP PI on o n those respective respe ctive versions. versions. Second, Seco nd, you will will need to obtain o btain the the library library ‘aii_map_ap ‘aii_map_api.jar’ i.jar’ from the SAP PI server. s erver. I usually usually keep a local copy since one does not alway a lwayss have access acce ss to the SAP PI operating o perating system. system. The library library can be obtained in the the path: ‘
///j2ee/cluster/server/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 J ava IDE, ID E, but this is entirely optional optiona l if if you prefer to compile on the command c ommand line. line. I usually use use Ecli Ec lipse pse and in this manual manual I demonstrate using Eclipse, but there will be similar ways to do the steps in other IDEs.
http://techpl ay.pl ozzl e.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 C lass 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 C lass 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 Re pository)
Once you’ve 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 you’re done! Let me know if there are any questions. I’d 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, it’s 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 doesn’t 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 you’ve 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:
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