'Possibilities to execute a groovy script with Hybris in java code (no hAC)
I'm wondering if there is a way to execute a groovy script with Hybris. I know how groovy scripts are executed with the Hybris Administration Console (hAC), but I need a solution to execute such a script from java code. Preferably with the Patching Framework's @SystemSetup but not necessary. (https://help.sap.com/docs/SAP_COMMERCE/d0224eca81e249cb821f2cdf45a82ace/5db22427a1d541669bc4d12793a7b672.html?locale=en-US)
I'm looking for a method similar to Impex import (eg. from core extension):
@SystemSetup(extension = SampleCoreConstants.EXTENSIONNAME)
public class CoreSystemSetup extends AbstractSystemSetup {
public static final String IMPORT_ACCESS_RIGHTS = "accessRights";
@SystemSetup(type = Type.ESSENTIAL, process = Process.ALL)
public void createEssentialData(final SystemSetupContext context)
{
importImpexFile(context, "/samplecore/import/common/file-name.impex");
}
@Override
@SystemSetupParameterMethod
public List<SystemSetupParameter> getInitializationOptions()
{
final List<SystemSetupParameter> params = new ArrayList<>();
params.add(createBooleanSystemSetupParameter(IMPORT_ACCESS_RIGHTS, "Import Users & Groups", true));
return params;
}
Here the same with SQL: https://www.stackextend.com/hybris/run-native-sql-query-hybris/
So anyone who can help me out with a solution (or with a clear answer if this is possible or not) are welcome.
Thanks!
Solution 1:[1]
It's possible to run groovy in your code. Even in SystemSetup.
You can use the hybris service (spring bean) de.hybris.platform.scripting.engine.ScriptingLanguagesService
that's available in the processing
extension
In the code, this could be something like
final ClassPathResource resource = new ClassPathResource("location.groovy");
final ResourceScriptContent content = new ResourceScriptContent(resource);
ScriptExecutable groovyScript = scriptingLanguagesService.getExecutableByContent(content);
ScriptExecutionResult result = groovyScript.execute();
This will execute a script on the given location in your classpath. If you don't have your groovy in a file in your classpath, there are other Content types possible. For example: SimpleScriptContent
Solution 2:[2]
There are many to execute groovy script.
WAY 1:
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;
final Resource resource = new FileSystemResource("/Users/zeus/scripts/setMimesForMedias.groovy");
// Let's assume we have scriptingLanguagesService injected by the Spring
final ScriptContent scriptContent = new ResourceScriptContent(resource);
final ScriptExecutable executable = scriptingLanguagesService.getExecutableByContent(scriptContent);
// now we can execute script
final ScriptExecutionResult result = executable.execute();
// to obtain result of execution
System.out.println(result.getScriptResult());
WAY 2 : We can use simple method as below to achieve.
Here is the sample code:
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
GroovyShell groovy = new GroovyShell(new Binding());
groovy.setVariable("text","Hello World!"); // you can variables here as many you needed
groovy.evaluate("println text"); // you can pass file as well instead of text
WAY 3 : If you want the OOB one, ScriptingJobPerformable is the OOB class which you can take reference from that.
final ScriptExecutable executable = scriptingLanguagesService.getExecutableByURI(dynamicScriptingJob.getScriptURI());
LOG.info("### Starting executing script : " + dynamicScriptingJob.getScriptURI() + " ###");
final Map<String, Object> params = ImmutableMap.<String, Object>builder()//
.put("cronjob", cronJob) //
.put("log", LOG) //
.build();
final ScriptExecutionResult result = executable.execute(params);
Please refer Scripting Engine for detailed explanation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yoni |
Solution 2 |