在java代码中使用Hybris执行groovy脚本的可能性(没有hAC)



我想知道是否有一种方法可以使用Hybris执行groovy脚本。我知道如何使用Hybris Administration Console (hAC)执行groovy脚本,但是我需要一个从java代码执行此类脚本的解决方案。最好使用补丁框架的@SystemSetup,但不是必需的。(https://help.sap.com/docs/SAP_COMMERCE/d0224eca81e249cb821f2cdf45a82ace/5db22427a1d541669bc4d12793a7b672.html?locale=en-US)

我正在寻找一种方法类似于Impex进口(例如。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;
}

这里与SQL相同:https://www.stackextend.com/hybris/run-native-sql-query-hybris/

所以任何可以帮助我解决问题的人(或者一个明确的答案,如果这是可能的或不可能的)都是欢迎的。

谢谢!

可以在代码中运行groovy。即使在SystemSetup.

可以使用hybris service (spring bean)de.hybris.platform.scripting.engine.ScriptingLanguagesServiceprocessing扩展

中可用在代码中,这可能是像

这样的东西
final ClassPathResource resource = new ClassPathResource("location.groovy");
final ResourceScriptContent content = new ResourceScriptContent(resource);
ScriptExecutable groovyScript = scriptingLanguagesService.getExecutableByContent(content);
ScriptExecutionResult result = groovyScript.execute();

这将在类路径中的给定位置执行脚本。如果类路径中的文件中没有groovy,那么还可以使用其他的Content类型。例如:SimpleScriptContent

有许多执行groovy的脚本。

方式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());

方式2:我们可以用如下简单的方法来实现。

下面是示例代码:
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

方式3:如果您想要OOB的,ScriptingJobPerformable是OOB类,你可以从中引用。

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

详细说明请参考脚本引擎

最新更新