Search code examples
aemaem-6

How do I get runmode in java using resourceResolver


I have an OSGi service which needs to run only in publish instance. How do I get runmode in java when I only have a resourceResolver and not request ?


Solution

  • To get a list of run modes the current AEM instance is using you can use the SlingSettingService in your service and/or servlet.

    import org.apache.felix.scr.annotations.Component;
    import org.apache.sling.settings.SlingSettingsService;
    
    @Component
    public class MyService {
    
        @Reference
        private SlingSettingsService slingSettingsService;
    
        private boolean isPublish() {
            return this.slingSettingsService.getRunModes().contains("publish");
        }
    }
    

    See:

    AEM 6.1: https://docs.adobe.com/docs/en/aem/6-1/ref/javadoc/org/apache/sling/settings/SlingSettingsService.html

    AEM 6.2: https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc/org/apache/sling/settings/SlingSettingsService.html

    AEM 6.3: https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/org/apache/sling/settings/SlingSettingsService.html

    AEM 6.4: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/org/apache/sling/settings/SlingSettingsService.html