Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Issues with Fetching RunMode

Avatar

Level 5

Hello Community - I am facing some issues with obtaining the run modes in Sling Model. Can someone tell mw what is wrong in this?

 

@Model(adaptables = { SlingHttpServletRequest.class,
Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class TestData {

private String runMode="";

@reference
private SlingSettingsService settingsService;

@PostConstruct
protected void init() {
runMode = settingsService.getRunModes().toString();
}
}

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi,

 

If you are using the latest annotations then you should go ahead with the below sample code:-

 

import org.apache.sling.settings.SlingSettingsService;

import org.osgi.service.component.*;

 

@Model(adaptables = Resource.class)
public class HelloWorldModel {

private String runMode;

 

@OSGiService
private SlingSettingsService settings;

@PostConstruct
protected void init() {

     runMode = settingsService.getRunModes().toString();
}


}

 

Similarly you can use it along with the Exporter so that it can be used for the model in the form of the json as well with slightly implementation:-

 

@Model(
adaptables = { SlingHttpServletRequest.class },
adapters = { Test.class, ComponentExporter.class },
resourceType = { Constants.TestResource },
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class TestImpl implements Test {

}

 

Thanks

View solution in original post

3 Replies

Avatar

Employee

@v1101,

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:

https://sling.apache.org/documentation/bundles/sling-settings-org-apache-sling-settings.html

AEM 6.3: https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/or...

AEM 6.4: https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc/or...

 

Thanks!!

Avatar

Correct answer by
Level 3

Hi,

 

If you are using the latest annotations then you should go ahead with the below sample code:-

 

import org.apache.sling.settings.SlingSettingsService;

import org.osgi.service.component.*;

 

@Model(adaptables = Resource.class)
public class HelloWorldModel {

private String runMode;

 

@OSGiService
private SlingSettingsService settings;

@PostConstruct
protected void init() {

     runMode = settingsService.getRunModes().toString();
}


}

 

Similarly you can use it along with the Exporter so that it can be used for the model in the form of the json as well with slightly implementation:-

 

@Model(
adaptables = { SlingHttpServletRequest.class },
adapters = { Test.class, ComponentExporter.class },
resourceType = { Constants.TestResource },
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class TestImpl implements Test {

}

 

Thanks