Determine runmode in Adobe CQ

Gleb picture Gleb · Sep 3, 2012 · Viewed 20.9k times · Source

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?

Answer

Bertrand Delacretaz picture Bertrand Delacretaz · Sep 3, 2012

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.