7

I am just starting on Apache sling and CQ5 development. There is this concept of using OSGI bundles in Sling.

I can't find out how the sling framework actually interacts with these bundles and where does the response from bundles go ?

Riju Mahna
  • 6,718
  • 12
  • 52
  • 91

1 Answers1

8

OSGi is module framework and service platform used by Sling and the CQ5 product. Sling itself is comprised of a series of bundles hosted within the Felix OSGi container. Bundles are a collection group of components/services and java classes managed by the container. The bundle can specify which packages will be imported, exported and also the versions of those dependencies.

There are a number of ways that you can interact with OSGi from Sling. From a JSP/JSP you can use the sling object (of type SlingScriptHelper), which is most likely available in your JSP page if you have included the following line:

<%@include file="/libs/foundation/global.jsp"%>

in your component or have the following:

<cq:defineObjects> 

You can use it like so:

QueryBuilder queryBuilder = sling.getService(QueryBuilder.class);

Additionally, if you have your own OSGi components (e.g. Servlet, Service, etc) you can inject references to other OSGI components/services using SCR annotations. Bertrand describes this in his answer to Getting OSGi services from a bundle in Sling/CQ. Effectively this means adding the @Reference annotation to your OSGI component variables in your components, like so:

 @Reference
 private SlingRepository repository;

When your component is loaded, then the reference will be injected by the OSGi container.

A bundle doesn't have a response as such. A deployed bundle should be visible in the system console:

http://localhost:4502/system/console/bundles

with its components, services & configuration declared here:

http://localhost:4502/system/console/services
http://localhost:4502/system/console/components
http://localhost:4502/system/console/configMgr

(Replace localhost:4502 with your own CQ server host:port)

Once you have obtained a reference to a component, then you can call the methods on that and use the return values from those calls.

Community
  • 1
  • 1
diffa
  • 2,986
  • 1
  • 22
  • 35
  • Thanks SO MUCH !!! I was almost losing hope on this.. The regular material on the internet does not answer this question – Riju Mahna Jan 31 '13 at 10:03
  • Documentation on the day [CQ5 developer site](http://dev.day.com/docs/en/cq/current.html#Developing%20on%20CQ) and [blogs](http://dev.day.com/content/ddc/blog/2008/05/slingosgi.html) is pretty good. Also the [Felix / SCR](http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html) is helpful. Finally, check out the unit/integration tests for the [Sling project itself](https://github.com/apache/sling) as these usually show the best way of using the various Sling APIs. Please accept the answer if it meets your needs. ;-) – diffa Jan 31 '13 at 10:27
  • Actually I have to learn CQ5 (including Sling, OSGi, Felix, bundles etc.) in 3 days !! :) So I am just rushing over webpages now. Thanks much – Riju Mahna Jan 31 '13 at 10:30