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();
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
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
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
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
Thanks!!
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
Views
Likes
Replies