Issues with Fetching RunMode | Community
Skip to main content
Level 5
December 9, 2020
Solved

Issues with Fetching RunMode

  • December 9, 2020
  • 3 replies
  • 1912 views

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="";

@3214626
private SlingSettingsService settingsService;

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by shaileshb584084

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)
@3484101(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class TestImpl implements Test {

}

 

Thanks

3 replies

shelly-goel
Adobe Employee
Adobe Employee
December 9, 2020

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

 

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

vanegi
Adobe Employee
Adobe Employee
December 9, 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/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

 

Thanks!!

shaileshb584084Accepted solution
Level 2
December 9, 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)
@3484101(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class TestImpl implements Test {

}

 

Thanks