Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how osgi bundles are used by sling

Tags:

java

osgi

aem

sling

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 ?

like image 574
Riju Mahna Avatar asked Jan 31 '13 05:01

Riju Mahna


People also ask

What is OSGi in Sling?

OSGi — The Sling application is built as a series of OSGi bundles and makes heavy use of a number of OSGi core and compendium services. Sling API — To implement content based Web applications with Sling, an API has been defined, this extends the Servlet API and provides more functionality to work on the content.

What is the use of OSGi bundle?

An OSGi bundle JAR file contains a JAR manifest file. This file contains metadata that enables the OSGi Framework to process the modular aspects of the bundle. Describes the version of the bundle and enables multiple versions of a bundle to be active concurrently in the same framework instance.

How does a Sling model work?

Sling Models are "pure" POJOs which maps Sling objects (resources, request objects etc.). Since Sling Models are annotation-driven Plain Old Java Objects (POJOs), annotations are used a lot. They allow you to map resource properties, assign default values, inject OSGi services and much more.

What is an OSGi bundle?

In OSGi, a single component is called a bundle. Logically, a bundle is a piece of functionality that has an independent lifecycle – which means it can be started, stopped and removed independently. Technically, a bundle is just a jar file with a MANIFEST. MF file containing some OSGi-specific headers.


1 Answers

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.

like image 67
diffa Avatar answered Oct 12 '22 23:10

diffa