简体   繁体   English

确定Adobe CQ中的运行模式

[英]Determine runmode in Adobe CQ

How do I programmatically know which run-mode the instance is running? 如何以编程方式知道实例正在运行的运行模式? I created a custom tag that provides the config depending on the instance run-mode, but I can not determine the current run-mode. 我创建了一个自定义标记,根据实例运行模式提供配置,但我无法确定当前的运行模式。
I found a method that returns a list of run-mods instance: 我找到了一个返回run-mods实例列表的方法:

SlingSettings settings = ...get from BundleContext...
Set<String> currentRunModes = settings.getRunModes();

But I can not get the objects SlingSettings or BundleContext. 但我无法获取SlingSettings或BundleContext对象。 How can I get these objects or perhaps there is another way to get the current run-mode? 如何获取这些对象,或者可能有另一种方法来获取当前的运行模式?

SlingSetttings is the right way - If it's from Java the simplest way to get it is with an SCR @Reference annotation in a class that's an SCR @Component, saves you from having to go through BundleContext. SlingSetttings是正确的方法 - 如果它来自Java,最简单的方法就是在一个SCR @Component的类中使用SCR @Reference注释,使您不必通过BundleContext。

If it's from a Sling script, you can use sling.getService(....) to get the SlingSettings. 如果它来自Sling脚本,您可以使用sling.getService(....)来获取SlingSettings。

Note that the cases where you need to read the run modes are rare, usually you'd rather setup your OSGi configurations to depend on the run modes and have the OSGi components modify their behavior based on that. 请注意,您需要阅读运行模式的情况很少见,通常您宁愿设置OSGi配置以依赖于运行模式,并让OSGi组件根据它修改其行为。

Finally I decided to use global.jsp, write run-modes in the page context and get it in my class: 最后我决定使用global.jsp,在页面上下文中编写运行模式并在我的类中获取它:

<%
pageContext.setAttribute("runModes", sling.getService(SlingSettingsService.class).getRunModes().toString());
%>
import java.util.Set;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.apache.sling.settings.SlingSettingsService;

public class myClass {
    public static Set<String> getRunModes() {
        BundleContext bundleContext = FrameworkUtil.getBundle(myClass.class).getBundleContext();
        ServiceReference serviceReference = bundleContext.getServiceReference(SlingSettingsService.class.getName( ));
        SlingSettingsService slingSettingsService = (SlingSettingsService)bundleContext.getService(serviceReference);
        return slingSettingsService.getRunModes();
    }
}
@Reference
RunMode runmode;

or 要么

sling.getService( RunMode.class )

and call 并打电话

getCurrentRunModes(); //returns String[]

If you're using Sightly and working with a class that extends WCMUsePojo 如果您正在使用Sightly并使用扩展WCMUsePojo的类

slingSettings =this.getSlingScriptHelper().getService(SlingSettingsService.class);
    Set<String> runmodes = slingSettings.getRunModes();

As Bertrand Delacretaz said it is the right way to check whether instance is Author or Publish. 正如Bertrand Delacretaz所说,检查实例是作者还是发布是正确的方法。 In jsp or java you could check like 在jsp或java中你可以检查一下

import  org.apache.sling.settings.SlingSettingsService
Set<String> runModes = sling.getService(SlingSettingsService.class).getRunModes();

if (runModes.contains("author")) {
} 

Another way is using 另一种方式是使用

if (mode == WCMMode.EDIT) 
{
}

But this approach will fail in case of Preview mode and wouldn't work. 但是这种方法在预览模式下会失败并且不起作用。

You can also try this: 你也可以试试这个:

RunModeService runModeService = getSlingScriptHelper().getService(RunModeService.class);
author = runModeService.isActive("author");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM