Technical Articles
How to use External Jar Files in SAP CPI
Introduction: –
While building an Integration in CPI, we often come across a situation where we need to use same piece of code which we have used earlier or sometimes we need to use external Jar file’s class to process some data.
This Blog will explain how you can use External Jar files in CPI and make use of different functions inside Jar file to process data. This Jar file can also be used as a common code repository where you can keep all the common functions which you usually use in CPI groovy script and then make use of it in different IFlows by simply importing it.
Problem Statement: –
How to reuse External Jar file’s functions in CPI to process Data.
Prerequisites: –
- You need Eclipse to create your own Jar file if you don’t have one
- You need basic understanding of groovy scripts in CPI. To know more about groovy scripting click here
Resolution: –
I will be explaining here from start how to create your own Jar file and then use it in CPI. If you already have your Jar file, then you can start following the blog from Step 8.
So here you should give some logical name to your Project and make sure you select ‘Use an execution environment JRE’ radio button and ‘JavaSE – 1.8’ as an option. This is because CPI’s Java version is 8 and if you create Jar file on any other version it will give error while processing.
You need to give logical name to your package and then select on Finish.
Give a logical name to your class and select Finish. Please note that once you click on finish an empty class will be created and you can write your functions inside this class now.
For example, I have written two functions to concatenate Full name and to calculate Monthly salary of an employee. Please refer to the below screenshot.
Here you can write as many functions as you want and since this is a Java code you can also write complex Java functions here which can be used in CPI for processing.
Step 7: – After completing the code now you can export the project as Jar file, and it will be saved as a Jar file on your local machine.
Now suppose we have the below xml payload. We want to concatenate the FullName and calculate monthly salary by using the functions in Jar file.
Input XML: –
<root>
<row>
<FirstName>Jon</FirstName>
<MiddleName>Vander</MiddleName>
<LastName>Williamson</LastName>
<AnnualSalary>12000</AnnualSalary>
</row>
</root>
Now to use the functions from the Jar file, we need to use Groovy Script. Please refer to the below script which we have used here to access functions in Jar File.
import com.sap.it.api.mapping.*;
import test.*;
def String FullName(String FirstName, String MiddleName, String LastName){
CpiTest c1 = new CpiTest();
String fullname = c1.getFullName(FirstName,MiddleName,LastName);
return fullname;
}
def String MonthlySalary(int AnnualSalary){
CpiTest c1 = new CpiTest();
def SalaryperMonth = c1.getSalaryperMonth(AnnualSalary);
return SalaryperMonth;
}
So, as you can see I have created two functions one to concatenate Full Name and one to calculate Monthly Salary. Both the functions use the Jar File Functions which we have imported. Please note to use any of the Jar File function in Groovy script we need to Import package which we have created while creating Jar File. This can be done by adding an import statement (import test.*).
So now when we use this groovy script and stimulate the above mapping, we get the desired result.
So, by using the Jar File function we got Full Name concatenated and Monthly Salary calculated as well.
We can use this Jar file in Groovy Script component as well. Below is the example where I have used Jar File function in normal groovy script.
I have used the same input xml as the incoming body to the groovy script and I have created CSV file out of it. Please refer to the below script which will convert the incoming XML into CSV file and will also use Jar file functions to do data transformations.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import test.*;
def Message processData(Message message) {
def body = message.getBody(java.lang.String);
def xmlBody = new XmlSlurper().parseText(body);
String FinalBody = "FullName,Salary\n";
xmlBody.row.each { it ->
String FN = it.FirstName.text();
String MN = it.MiddleName.text();
String LN = it.LastName.text();
String Sal = it.AnnualSalary.text();
int Salary = Sal.toInteger();
CpiTest c1 = new CpiTest();
def fullname = c1.getFullName(FN,MN,LN);
def SalaryperMonth = c1.getSalaryperMonth(Salary);
FinalBody = FinalBody + fullname + "," +SalaryperMonth + "\n";
}
message.setBody(FinalBody);
return message;
}
New Function Added – CalTotalSalary. This Function will add all the integer data sent as an array and give total amount as an output. I used this function in CPI Mapping step by using the below mentioned script.
def void TotalSalary(int[] Salary, Output output){
CpiTest c1 = new CpiTest();
def Ans = c1.CalTotalSalary(Arrays.asList(Salary));
output.addValue(Ans);
}
This is how you can modify the Jar functions as per your requirement and achieve the desired output.
Conclusion: –
This is how you can use Jar File functions in your CPI Artifact.
You can also follow the same process to create your own Jar File repository which you can use in all your Integrations. This will save a lot of time in terms of development of Integrations.
Hope this Blog post helps 😊
Regards,
Dhairya Khimsaria
Senior Consultant, Veritas Prime Labs
Hi Dhairya,
Excellent write-up. I am waiting for your next blog already !
Shall stay tuned.
Regards
Raja Sekhar
Great Content Dhairya! Thank you for Sharing 🙂
Good One Dhairya.
Good one Dhairya !
Good one Dhairya...!
That was really helpful and wonderful blog @dhairya. Keep up the good work
Hi Dhairya Khimsaria,
always good to re-iterate topics like this. Have a look at what we did in the past blog posts. Section "Sharing scripts across iFlows" gives an additional angle to your contribution.
KR
Martin
Hello,
did you also try to use your custom external JAR within a xslt mapping?
I am able to use functions of an external jar from apache in my xslt mapping, but my own functions fails as the class can't be loaded with error:
Cannot find a 1-argument function named Q{java:test.FindEarliestDate}findEarliestDate(). Cannot load Java class java:test.FindEarliestDate. For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS
The JAR is build accordingly to your guide and the class is part of the package "test".
Snippet of the xslt mapping and class import:
Import
....
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a1="java:test.FindEarliestDate" exclude-result-prefixes="a1">
Function call
...
<xsl:variable name="scheduledPickupDate">
<xsl:value-of select="a1:findEarliestDate(//ScheduledPickupDate)"/>
</xsl:variable>
Regards,
Daniel
Update:
I used a newer version of eclipse IDE to create the jar and now the class/function also can be used within xslt mapping with the syntax:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a1="java:test.FindEarliestDate" exclude-result-prefixes="a1">
..
<xsl:variable name="scheduledPickupDate">
<xsl:value-of select="a1:findEarliestDate(//ScheduledPickupDate)"/>
</xsl:variable>
BR,
Daniel
Great content, I really appreciate that