Determine runmode in Adobe CQ
Asked Answered
S

7

13

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:

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

But I can not get the objects SlingSettings or BundleContext. How can I get these objects or perhaps there is another way to get the current run-mode?

Silvie answered 3/9, 2012 at 10:7 Comment(0)
L
17

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.

If it's from a Sling script, you can use sling.getService(....) to get the 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.

Luiseluiza answered 3/9, 2012 at 19:52 Comment(0)
S
10

Finally I decided to use global.jsp, write run-modes in the page context and get it in my class:

<%
pageContext.setAttribute("runModes", sling.getService(SlingSettingsService.class).getRunModes().toString());
%>
Silvie answered 5/9, 2012 at 11:58 Comment(0)
U
6
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();
    }
}
Umbrella answered 12/9, 2012 at 9:40 Comment(0)
A
3
@Reference
RunMode runmode;

or

sling.getService( RunMode.class )

and call

getCurrentRunModes(); //returns String[]
Anacardiaceous answered 7/6, 2013 at 16:57 Comment(0)
C
2

If you're using Sightly and working with a class that extends WCMUsePojo

slingSettings =this.getSlingScriptHelper().getService(SlingSettingsService.class);
    Set<String> runmodes = slingSettings.getRunModes();
Conceptualism answered 14/11, 2015 at 18:6 Comment(1)
Was trying to find out how to get a SlingSettingsService. Nailed itEpicotyl
S
2

As Bertrand Delacretaz said it is the right way to check whether instance is Author or Publish. In jsp or java you could check like

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.

Santinasantini answered 19/11, 2015 at 18:0 Comment(2)
Any could help with error "SlingSettings can't be resolve error".Santinasantini
If you're using WCMMode as a check — as an alternative, consider if you want to check mode != WCMMode.DISABLED rather than mode == WCMMode.EDIT, which should help with the Preview issue that you highlgihted (Which one to use will be on a case-by-case, as it will probably be feature dependent)Nympholepsy
O
1

You can also try this:

RunModeService runModeService = getSlingScriptHelper().getService(RunModeService.class);
author = runModeService.isActive("author");
Ovaritis answered 27/5, 2018 at 14:1 Comment(2)
It would be helpful if you described why the answer is better/different than the others here.Vermeil
Please refer to the link below, I am not claiming it to be better but I have always preferred it, since I could get a lot of additional information using the same. helpx.adobe.com/experience-manager/6-3/sites/deploying/using/…Ovaritis

© 2022 - 2024 — McMap. All rights reserved.