在Java代码中使用Hybris执行Groovy脚本的可能性(无HAC)

发布于 2025-01-29 14:17:32 字数 1419 浏览 2 评论 0原文

我想知道是否有一种方法可以用Hybris执行时髦的脚本。我知道如何使用Hybris Administration Console(HAC)执行Groovy脚本,但是我需要一个解决方案来从Java代码执行此类脚本。最好使用Patching Framework的@SystemSetup,但不是必需的。 ( ttps://help.sap.com/docs /SAP_Commerce/D0224ECA81E249CB821F2CDF45A82ACE/5DB22427A1D541669BC4D12793A7B672.HTML

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

TPS ://www.stackextend.com/hybris/run-native-sql-query-hybris/“ rel =” nofollow noreferrer“> https://www.stackextend.com/hybris/hhybris/run-native-sql-quer-sql-query-hybris /

因此,欢迎任何可以帮助我解决解决方案的人(或有明确的答案)。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2025-02-05 14:17:33

可以在您的代码中运行Groovy。甚至在SystemSetup中。

您可以使用Hybris服务(Spring Bean)de.hybris.platform.scripting.engine.scriptinglanguagesservice
这可以在

代码中的Processing扩展程序中可用,这可能是这样的事情

final ClassPathResource resource = new ClassPathResource("location.groovy");
final ResourceScriptContent content = new ResourceScriptContent(resource);

ScriptExecutable groovyScript = scriptingLanguagesService.getExecutableByContent(content);

ScriptExecutionResult result = groovyScript.execute();

,将在您的类路径中的给定位置上执行脚本。如果您的class路径中的文件中没有凹槽,则可能还有其他内容类型。例如:siglescriptcontent

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

灯下孤影 2025-02-05 14:17:33

有很多可以执行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 ONE,则 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);

请参阅

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文