Can we fetch configuration value of osgi service(osgi.config) directly in slingServlet? | Adobe Higher Education
Skip to main content
shridhar_sahu
Level 3
December 11, 2018

Can we fetch configuration value of osgi service(osgi.config) directly in slingServlet?

Hi,

I am currently working on aem 6.2.

I want to fetch values of service  directly in slingServlet.

For that i have created a Service

ConfigurationService.java

public abstract interface ConfigurationService {

       public abstract String getSimpleString();

}

ConfigurationServiceImpl.java

@Service({ConfigurationServiceImpl.class})

@Component(immediate=true, metatype=true, label="Configuration Service")

public class ConfigurationServiceImpl implements ConfigurationService{

private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceImpl.class);

@Property(label=" Link", description="Link")

private static final String AZURE_STRING = "azureString";

private String azureString;

@Activate

protected void activate(Map<String, Object> properties)

{

LOG.info("[*** AEM ConfigurationService]: activating configuration service");

readProperties(properties);

}

protected void readProperties(Map<String, Object> properties)

{

LOG.info(properties.toString());

this.azureString = PropertiesUtil.toString(properties.get("azureString"), "default");

LOG.info("Simple String: " + this.azureString);

}

@Override

public String getSimpleString() {

// TODO Auto-generated method stub

return this.azureString;

}

}

UrlGenerationServlet.java

@SlingServlet(paths="/bin/adobe/urlGenerationServlet", methods = "POST", metatype=true)

public class urlCreationServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet{

protected final Logger log = LoggerFactory.getLogger(this.getClass());

private static final long serialVersionUID = 1L;

@Reference

private ConfigurationService configurationService;

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {

         //here i tried using  getSlingScriptHelper().getService(ConfigurationService.class);

          //and ended up because we can't use WCMUsePojo in SlingServlet.

         

}

}

Regards,

Shridhar

Ce sujet a été fermé aux réponses.

1 commentaire

arunpatidar
Community Advisor
Community Advisor
December 11, 2018

Yes, You can do that. You can directly use service. there is no need to write below method to get service @Reference would do same getSlingScriptHelper().getService(ConfigurationService.class);

Sample Servlet Example.

@Component(service = Servlet.class, immediate = true, property = {

  Constants.SERVICE_DESCRIPTION + "=Get Features Servlet", "sling.servlet.paths=" + "/bin/page/getOsgiCong" })

public class GetOsgiConfigServlet extends SlingSafeMethodsServlet {

  @Reference

  private DeveloperInfo developerInfo;

  private static final long serialVersionUID = 1L;

  Logger logger = LoggerFactory.getLogger(this.getClass());

  @Override

  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

  throws ServletException, IOException {

  response.getWriter().print("Developer Info : "+developerInfo.getDeveloperInfo());

  response.getWriter().close();

  response.setContentType("text/html");

  }

}

Arun Patidar
shridhar_sahu
Level 3
December 12, 2018

Hi Arun,

I have created a maven archetype 10 project.

I'm facing following errors in eclipse:

Multiple markers at this line

- The attribute property is undefined for the annotation type Component

- The attribute service is undefined for the annotation type Component

- Constants cannot be resolved to a variable

Thanks,

Shridhar

shridhar_sahu
Level 3
December 17, 2018

Can you try to create project using archetype 11 or 12.

It seems dependencies are missing thats why it is throwing error.

Can you check your POM?

<!-- OSGi Dependencies -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
</dependency>

sample POM for 6.4

aem63app-repo/java/POMs at master · arunpatidar02/aem63app-repo · GitHub


Hi Arun, I need to implement it in an existing maven Archetype 10 Project(AEM 6.2)