
This blog is based mostly on shoaib-alam's blog https://community.sap.com/t5/technology-blogs-by-members/testing-groovy-script-locally-using-eclipse... , based on what I did to set up a new environment from scratch in 2024, as some of the links and blogs were now outdated, and everything was spread over several blogs.. this blog will combine all in one.
SAP Integration Suite runtime environment uses Java and Apache Camel integration framework. It’s important to ensure that Java, Apache Camel and Groovy libraries versions that are used in SAP Integration Suite is in sync with those used in your IDE. If IDE is using a newer version of these libraries, then locally developed Groovy scripts are likely to fail at the SAP Integration Suite runtime.
A version of the Groovy library that is used in runtime can be identified with the help of the standard GDK API by calling the getVersion() method of the groovy.lang.GroovySystem class.
You can also find the Java version and Apache Camel version by calling the getProperty(String) method of the java.lang.System class and getVersion() method of CamelContext respectively.
Add a new Integration Flow, add Groovy Script Step, and add the following Groovy code:
import com.sap.gateway.ip.core.customdev.util.Message;
import org.apache.camel.impl.*
def Message processData(Message message) {
def camelContext = new DefaultCamelContext()
StringBuilder versions = new StringBuilder()
versions << "Groovy: ${GroovySystem.getVersion()}\n"
versions << "Java: ${System.getProperty('java.version')}\n"
versions << "Camel: ${camelContext.getVersion()}"
message.setBody(versions.toString())
return message
}
Add a timer, deploy, and use a trace in combination with CPI helper to get the result without effort
iFlow to get versions from CI
version flow result
For this environment, I used the following versions:
Eclipse 2021-12 (4.22) For Java Developers https://www.eclipse.org/downloads/packages/release/2021-12/r/eclipse-ide-java-developers
Download & install this Eclipse version
After Eclipse is installed - start Eclipse and install Groovy Developer Tools:
Go to 'Help' > 'Install New Software..'
Click 'Add..'
Enter 'Groovy Developer Tools' as name
Location https://groovy.jfrog.io/artifactory/plugins-release/e4.22
You can also use a different version of Eclipse, as long as you set the project Java VM version to a Java 8 JDK.
If you used a different version of Eclipse, you can find a corresponding version of Groovy Developer Tools here: wiki
add new repository to Eclipse for Groovy Developer Tools
Install the Main Package
This step involves another integration flow in Cloud Integration, to explore and download the files we need from Cloud Integration itself
You can either download the flow from here and import it: CI Filesystem Explorer.zip
Or you can create it yourself with these instructions:
You need 2 integration processes, one endpoint to allow browsing, and one for viewing or downloading the files.
Create a script and paste in the following code:
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap
def Message browse(Message message) {
def mapHeaders = message.getHeaders()
String mainDir = mapHeaders.get("CamelHttpPath")
String mainUrl = mapHeaders.get("CamelHttpUrl")
String mainSrv = mapHeaders.get("CamelServletContextPath")
String srvUrl = mainUrl.minus(mainDir)
String urlDown = srvUrl.minus(mainSrv)+"/amba/cpifilesystemdownload"
File dir = new File(mainDir)
StringBuilder strBuilder = new StringBuilder()
strBuilder << '<meta charset="UTF-8"></br>'
if (dir.parent != null){strBuilder << "<a href='${srvUrl}${dir.parent}'>📁 ..</a></br>"}
dir.eachDir {strBuilder << "<a href='${mainUrl}/${it.name}'>📁 ${it.name}</a></br>"}
dir.eachFile {if (it.isFile()) {strBuilder << "<a href='${urlDown}/${mainDir}/${it.name}'>${it.name}</a></br>" }}
message.setBody(strBuilder.toString().replaceAll("//","/"))
mapHeaders = ['content-type':'text/html']
message.setHeaders(mapHeaders)
return message
}
def Message download(Message message) {
def mapHeaders = message.getHeaders()
String filePath = mapHeaders.get("CamelHttpPath")
StringBuilder strBuilder = new StringBuilder()
File file = new File(filePath);byte[] fileContent = file.bytes
strBuilder << fileContent.encodeBase64().toString()
message.setBody(strBuilder.toString());
mapHeaders = ['Content-Transfer-Encoding':'base64',
'Content-Disposition':'attachment; filename=${file.getName()}']
return message
}
For browsing use this https endpoint address: /amba/cpifilesystem*
Add a script block with the groovy script calling the browse function from the script
For downloading use this https endpoint address: /amba/cpifilesystemdownload*
Add a script block with the groovy script calling the download function from the script
Also add a Base64 Decoder to the download process
Now deploy and go to https://XXXXXX.it-cpiXX-rt.cfapps.XXXX.hana.ondemand.com/http/amba/cpifilesystem/app/webapps and download the war file stack.worker_cf.karaf-6.xx.x.war. Unzip this file in a new! directory and it will provide most of the required jars.
Now also download slf4j-api-2.0.11.jar and slf4j-simple-2.0.9.jar from the maven repository here https://repo1.maven.org/maven2/org/slf4j/
Create a new Groovy project
Make sure you add a Java 8 JDK to use as JRE, and make that the default JRE, like Zulu 8 https://www.azul.com/downloads/?version=java-8-lts&package=jdk#zulu
You can go to the properties of the JRE System Library, add the Java 8 JDK and then make it the default JRE
You will also need to add all the jars that you have downloaded earlier to the project
To do so, richt click the project folder > click on 'Build Path' > 'Configure Build Path...'
This will be easiest if you first create a new folder and move or copy all the following jars to it
Now over in Eclipse, in the build path window, go to the 'Libraries' tab, click on Add External JARs... and add all the jars I listed earlier.
Now you can do the last steps, creating the actual Groovy script files
New > create a Groovy Type file
You can call it 'GroovyCPITest', kind Script
Paste in the script file the following contents, which will allow to run the other script as a script or Groovy shell
package com.groovy.testing
import com.sap.gateway.ip.core.customdev.processor.MessageImpl
import com.sap.gateway.ip.core.customdev.util.Message
// Load Groovy Script
GroovyShell shell = new GroovyShell()
def script = shell.parse(new File("src/com/groovy/testing/script1.groovy"))
// Initialize message with body, header and property
Message msg = new MessageImpl()
msg.setBody(new String("Hello Groovy World"))
msg.setHeader("oldHeader", "MyGroovyHeader")
msg.setProperty("oldProperty", "MyGroovyProperty")
// Execute script
script.processData(msg)
// Display results of script in console
println("Body:\r\n" + msg.getBody())
def displayMaps = { String mapName, Map map ->
println mapName
map.each { key, value -> println( key + " = " + value) }
}
displayMaps("Headers:", msg.getHeaders())
displayMaps("Properties:", msg.getProperties())
And also a second time for a script called 'script1'
Here you can paste your own Groovy script that you want to test/debug. For this example, I used the default contents that SAP puts in a new Groovy script file.
That was finally all, you can now run/debug the script by right clicking the project > Run As/Debug As > Groovy Console/Groovy Shell
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
9 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 |