Eclipse Integration with Hybris Groovy Console
This post explains how to set up Eclipse IDE in order to remotely execute Groovy scripts on the running Hybris instance. The post is based on and develops further the idea of the article eCommerce with Hybris: Debugging Hybris with Beanshell from Eclipse IDE. Make sure you are familiar with the topics explained there before continuing reading this post.
Motivation
If you follow instructions from the previous post, you already know how to get the job done. But what are the areas we can improve there? Here is what I think can be done better:
-
BeanShell. Although it’s fairly simple language, a lot of people are already familiar with Groovy and often it’s their language of choice when it comes to the scripting on the JVM. It is also my personal preference.
-
Eclipse integration. If you draw attention to the existing approach, we actually write Java code in the source file within restricted area bounded with specially-treated comments. When you run the program your script is fetched from this area and converted to the BeanShell on the fly, then it is sent to the Hybris BeanShell endpoint. While it’s definitely valid and working approach, it seems a bit complex and fragile to me.
Let’s work towards improving the points we highlighted.
The approach discussed below, right now works only on unix systems, because of the unix-spceific bash script used to post Groovy scripts to Hybris.
Adding Groovy Support
We don’t want to pollute our project with debugging code, so we will create a separate project for our Groovy scripts. And since we are going to write our scripts in Groovy we need to add Groovy support to Eclipse. To teach Eclipse understand Groovy install this plugin. This post doesn’t cover installation instructions, the process is well explained on the plugin’s web page, you need to install it on your own.
After successfully installing Groovy-Eclipse plugin, create new Groovy project and add it to your workspace. The workspace should look similar to this:
Since your scripts will be executed within Hybris process that knows nothing about your Groovy project, all the scripts should have default package, i.e. source files should be located right in the src
directory:
Despite the fact Groovy is dynamic language you’d rather use typed variables, to leverage auto-completion support. To make Hybris platform’s classes visible in our scripts, add appropriate project to the Groovy project dependencies:
Posting Groovy Script To Hybris
Here is the interesting part. In order to post your scripts to Hybris Groovy endpoint we will use this bash script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# This script posts BeanShell/Groovy script to the
# remote Hybris endpoint and parses the response.
_usage_ ()
{
echo "Usage: ./hybris_script <path_to_script> [<hybris_url>]"
}
case $# in
0) _usage_; exit 1 ;;
1) SCRIPT=$1; HYBRIS_URL="http://localhost:9001" ;;
2) SCRIPT=$1; HYBRIS_URL=$2 ;;
*) echo "Too many arguments."; _usage_; exit 1 ;;
esac
FILE_NAME=$(basename "$SCRIPT")
SCRIPT_EXTENSION="${FILE_NAME##*.}"
# Extract extension and obtain hybris url for script processing.
echo "Script extension: $SCRIPT_EXTENSION"
if [ "$SCRIPT_EXTENSION" == "bsh" ]
then
echo "BeanShell script obtained."
HYBRIS_SCRIPT_URL="/console/beanshell/execute"
elif [ "$SCRIPT_EXTENSION" == "groovy" ]
then
echo "Groovy script obtained."
HYBRIS_SCRIPT_URL="/console/groovy/execute"
else
echo "Not supported script type passed."
echo "Only .bsh and .groovy allowed"
exit 1
fi
# Post script to the remote endpoint and parse response.
FULL_URL="$HYBRIS_URL$HYBRIS_SCRIPT_URL"
echo "Posting $SCRIPT to the $FULL_URL..."
OUTPUT_TEXT=$(curl "$FULL_URL" \
-k -iL -b cookies.txt -c cookies.txt -s -u admin:nimda \
-H "Origin: $HYBRIS_URL" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json" \
-H "Referer: $FULL_URL" \
-H "X-Requested-With: XMLHttpRequest" \
-H "Connection: keep-alive" \
--data-urlencode "script@$SCRIPT" \
--data "commit=false")
echo "Obtained response:"
echo -e $OUTPUT_TEXT
- Line 13: Update URL to your project-specific value.
-
Line 44:
-u admin:nimda
- change admin user’s credentials if your settings differ from default ones.
Because we explicitly provide admin user’s credentials along with the request, we don’t need to modify Spring Security configuration to allow access to the Hybris Groovy console endpoint.
In order to integrate this script with Eclipse IDE, we need to configure it as “External Tool”. Here is how to do this:
- Open Eclipse IDE
-
In the main menu find
Run -> External Tools -> External Tools Configuration...
command and click on it: -
External Tools Configuration
dialog window should appear. Inside the window do following steps:- Click
Program
list item on the left panel. - Click
New launch configuration
toolbar button. In the appeared panel on the right, chooseMain
tab. - In the
Name
text field type descriptive name for your custom configuration, e.g. Hybris Groovy Console. - In the
Location
field specify full path to the «bash-script,bash script» shown above. -
In the
Arguments
text area specify single argument${resource_loc}
which always refers to full path of the file currently active in the Eclipse. -
Click
Apply
button to save your settings. Here is how theExternal Tools Configuration
window should look like after completing these steps: -
Optionally you may want to disable building the entire workspace every time you launch a Groovy script. Navigate to
Build
tab and switch radio button to theThe project containing the selected resource
option:
- Click
You can create several
External Tools Configurations
for different environments, e.g. test, staging, etc. Just provide URL for a specific environment as the second argument on the step 5.
Now we are all set and ready to execute Groovy scripts on a live Hybris instance. Let’s try a simple example. Type following code in your GroovyMain.groovy
file:
import de.hybris.platform.catalog.CatalogService
import de.hybris.platform.core.Registry
CatalogService catalogService = Registry
.applicationContext
.getBean("catalogService") as CatalogService
println "\nHybris catalogs:"
catalogService.allCatalogs.each {
println "$it.id - $it.version"
}
As you can see full auto-completion support is available:
Execute Run -> External Tools -> Hybris Groovy Console
command:
After running this command you should be able to see results of the script execution in the Eclipse console: