3

I am trying to access an OSGi service from a sightly javascript use function. Through the sling SlingScriptHelper. I can call the sling.getService(Service.class) method from javascript, but I do not know how to specify the class in javascript.

It looks like it should be possible to reference java classes using the fully qualified name or using rhino's importPackage(..).

However, I get the following exception:

org.apache.sling.scripting.sightly.SightlyException:
org.mozilla.javascript.EvaluatorException: Can't find method
org.apache.sling.scripting.core.impl.InternalScriptHelper.getService(object).
diffa
  • 2,986
  • 1
  • 22
  • 35

1 Answers1

12

To do this, prepend Packages to the fully-qualified class name, e.g.

var myService = sling.getService(Packages.com.myco.MyService);

(where com.myco.MyService is the service interface)

See https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/content/src/main/content/jcr_root/apps/acs-commons/sightly/templates/dhlm.js#L23 for an example.

diffa
  • 2,986
  • 1
  • 22
  • 35
Justin Edelson
  • 461
  • 3
  • 8