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?

  • December 11, 2018
  • 1 resposta
  • 9478 Visualizações

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

Este tópico foi fechado para respostas.

1 Resposta

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

Hi,

Can you please check your imports.

I used Osgi annotations only but you are using Felix. Sometimes we mix annotations reference which cause this error.


Hi Arun,

I tried using osgi annotations and i am getting following compilation error.

I'm using Maven Archetype 10 project.

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.adobe.sample.core.aem.QuotationConfigurationService;

@Component(service = Servlet.class, immediate = true, property = {Constants.SERVICE_DESCRIPTION + "=Get Features Servlet", "sling.servlet.paths=" + "/bin/page/getOsgiCong" })  //Constants cannot be resolved to a variable

public class GetOsgiConfigServlet extends SlingSafeMethodsServlet {

@Reference //The annotation @Reference is disallowed for this location

private QuotationConfigurationService 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.getURLString());

response.getWriter().close();

response.setContentType("text/html");

}

}