How to use External Jar Files in 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: –

  1. You need Eclipse to create your own Jar file if you don’t have one
  2. 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.

Step 1: – Open Eclipse and create a new project.

Step 2: – Select Java Project on the window and click on Next.

Step 3: – Give project name and select correct Java version.

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.

Step 4: – Click on Finish and you will be able to see your project on left hand side of Eclipse.

Step 5: – Create a new package under your Project. If you expand your project, you will be able to see something called as ‘src’. You need to right click on that folder and select New Package.

You need to give logical name to your package and then select on Finish.

Step 6: – Create a new Class under the new Package create in previous step. You need to right click on the newly created package and select new class.

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.

Step 8: – Import this Jar file in your CPI IFlow. Go to Resources -> Add -> Archive

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>

We will be using mapping step to format the xml.

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;
}

By using this Script, we get a CSV file with Headers as ‘FullName’ and ‘Salary’. We also convert the incoming XML data by using Jar File functions to populate the respective headers. Please refer to the below screenshot which shows the final CSV file data.

If you have a requirement where you need to process the data as an Array, then you can add a function in your Jar file which will accept input data as an Array. I have added a function which will accept AnnualSalary field data of all Employees as an Array and will add then all and send the total amount as an output. Please refer to the below screenshot.

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 you 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