Posts

Sling Models with Sightly Part – IV (Key Annotations – II)

In this post, I will explain some important questions related to OSGi Services and Sling Models. These questions had been asked by some of my blog readers on the basis of my last blog.

So the Problem Statement is-

According to them, They have one interface with multiple service implementations and want to choose any one of these service implementations according to their need. So their questions were-

1. How to choose the desired service implementation from these service implementations in another services or servlets?
2. How to choose the desired service implementation from these service implementations in Sling Model Class?

I have created a demo implementation according to their requirement and for doing that I have created an interface named as TestService with a dummy method named as test(). Here is the code-

Now, I have created two dummy service implementations of this interface. Here is the code for the first implementation named as TestServiceFirstImpl.

Here is the code for my second implementation named as TestServiceSecondImpl.

Answer for the first question-
It is a two-step process. These steps are explained below-

Step 1
Add a new property that have a unique value for each and every service implementation. This property could be anything you want to add. For example, I am using service.label property for these services and on the basis of this property I will choose from these implementations.

Here are my new definitions for TestServiceFirstImpl class.

Code For TestServiceSecondImpl class.

Step:- 2

Use @Reference annotation in another servlet or in OSGi Service with an extra attribute named as “target”. Just redefine this line as shown below-

Try to run your code, you will get the desired output.

Q2. How to choose these services in Sling Model class?

In Sling Model class you can call an external service using two annotations. These are-
1. @OSGiService
2. @Inject with @Source annotation

If you are using @OSGiService annotation then you have an attribute “filter”, here add your condition and you will get the desired implementation as shown below-

If you are using @Inject with @Source annotation then you need to add one more annotation @Filter. Here is the new code-

Now here my complete working code that will show you the use of both of these two approaches.

Q3. How am I testing these annotations in Sling Model class?
I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-

For complete working code, I am sharing the Git repository link.
https://bitbucket.org/accunitysoft/accunity-blog-snippets

Happy Coding..!!

Ankur Chauhan
Tech Lead

Sling Models with Sightly Part – III (Key Annotations – I)

The title of this blog is “Key Annotations – I” because In this post, I will explain two important annotations related to Sling Models and continue explaining other annotations in my coming posts. So for explaining two annotations, I have selected following two questions for this post.

1). How to include OSGI Services in Sling Model?
2). What is the use of @ResoucePath annotation?

I am assuming that you are using the latest versions of Sling Model APIs that I described in my previous blog.
For Answering first question, I have created an OSGi Service that have a dummy method as follows-

Now, You can include OSGi Services in your Sling Models using two annotations, these are-

1. @OSGiService Annotations
2. @Inject Annotation

Here is the Sling Model class, that shows how to use these annotations?

When you call this Model class then you will see that both of these two annotations working in the same manner.

Q1. What is the use of @ResoucePath annotations?
@ResoucePath annotation is a very handy annotation provide by Sling and using this annotation, you can convert a path into its resource object without writing any code. Let’s suppose you have a predefined path (e.g. /content/geometrixx/en) of the resource and want to convert that path into resource object then you can use @ResourcePath annotation.

Q2. How to use @ResoucePath annotation?
Here the code snippet that shows you the use of this annotation-

Q3. How am I testing these annotations?
I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-

For complete working code, I am sharing the Git repository link.
https://bitbucket.org/accunitysoft/accunity-blog-snippets

Happy Coding..!!

Ankur Chauhan
Tech Lead

Sling Model with Sightly Part – I

In this post, I will explain, how to use sling models with Sightly in AEM6.x?

For doing this, I have created a project using maven archetype for AEM6. If you want to use any existing project, then you need to check two things-

1. Dependency for Sling models in your project’s pom.xml file. You can find this maven dependency in you AEM instance package finder tab as shown in fig-

1.1

Now search for this dependency in your Maven project parent pom.xml file. If it’s already there then it’s fine else add this dependency into your project.

2. In whatever java packages, you want to add your Sling Model classes, add these java packages information into your maven-bundle-plugin.
For example, I am using two java package for adding my Sling Model classes, these packages are-
sling.models and com.blog.sling.models, so I have to place these package information into my maven-bundle-plugin, as shown below-

Q1). What will happen, If I don’t add these entries?

If you don’t add these entries, then maven will not add
“Sling-Model-Packages: sling.models, com.blog.sling.models”
header entry in your bundle Manifest file. So that these classes will not behave as sling models and will work as simple java classes and, If you try to run your code without this entry, then you will not get desired output as well as there will be no error message in error.log file.

In this post, I will create a very basic example where I will get a name from the component dialog and print that value using Sling Model. So Let’s start-

Create a Sling model class.

Here I am using @Inject annotation on “firstName” property, it means that this property will be looked up from the resource (after first adapting it to a ValueMap) and it is injected.
@PostConstructor annotation defines that this method will run after injecting all field (having @Inject annotation) from the resource.

Create the component as shown below-

1.2
In slingModel.html file add these lines.

My dialog properties are as shown below: –

dialog Properties

Build your maven project and drop this component on your page. You will see a screen as-
1.3Open the dialog of this component and add any entry (For example “Ankur Chauhan”) you will see the desired output as-
1.5

Q2). Why are we creating Sling Model class for this example even we can directly use ${properties.firstName} in our slingModel.html file?

You are right, We can do that or you can say, we must do that. But as I already mention that, in this post I am going to show you a very basic use of Sling Model. So I think this is the simplest example for this post. You will see must better examples in my coming blogs.

I am also sharing the Git repository link.
Git repository link is –

https://bitbucket.org/accunitysoft/accunity-blog-snippets

Happy Coding..!!

Ankur Chauhan
Tech Lead