Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Issues with Fetching RunMode

v1101
Level 5
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
shaileshb584084
Correct answer by
Level 3
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
vanegi
Employee
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!!

shaileshb584084
Correct answer by
Level 3
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