Issues with Fetching RunMode

Avatar

Avatar

v1101

Avatar

v1101

v1101

08-12-2020

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();
}
}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

shaileshb584084

Avatar

shaileshb584084

shaileshb584084

09-12-2020

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

Answers (2)

Answers (2)

Avatar

Avatar

vanegi

Employee

Avatar

vanegi

Employee

vanegi
Employee

09-12-2020

@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

Avatar

shelly-goel

MVP

Avatar

shelly-goel

MVP

shelly-goel
MVP

08-12-2020

Hi @Viki  @Reference annotation will not work here, replace it with @OSGIService annotation

 

https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-reference-guide#OSGiService