Author Archive: sbhaskar

Create a custom JCR event

In this session, we will see how to create a custom event and manage it. A custom event helps you to manage a specific activity happened on an AEM instance. For example, you want to send an e-mail to an editor as soon as an AEM page is created by a content creator; so that the editor is aware that the page needs to be edited at a later stage.

Let us start. We will create a new AEM component for creating a custom event.

  1. Inside com.aemcompany.myproject.impl, create a new class
    Note that the class is extended by EventListener interface that provides you the methods to manage the event.
  2. Create the following class variables:
    private SlingRepository repository;
    private ResourceResolverFactory resolverFactory;
    private Session session;
    private ObservationManager observationManager;
  3. Create an activate() method.
    This method gets called when the component get activated.
  4. Create a session object.
    session = repository.loginAdministrative(null);
  5. Create an  observationManager object.
    observationManager = session.getWorkspace().getObservationManager();
  6. Use the observationManager’s addEventListener method to create a custom event.
    observationManager.addEventListener(this, Event.NODE_ADDED, "/content", true, null, null, false);
  7. Implement an onEvent method.
    We will use this method to log an event whenever the custom event is fired.

    public void onEvent(EventIterator itr) {"A new node was added to /content ");                       
  8. Create deactivate() method basically to ensure to log the session out before the component gets deactivated. (Which is not explained, since it’s not directly related to the custom event.)
  9. Now, let us test it. Got to the Content folder and create a new test page.
  10. Open the log file and observe that the onEvent method is called.


Search in AEM repository

In the last session, we have seen how to add a property to a JCR node. We primarily created a resourceResolver object and created a resource using the path to the node where we wanted to set the property. Converted the resource into a Node using the adptTo() method. And, then used the setProperty() method to set the property. Finally, we created a session object in the similar manner, and then saved the property.

In this session, we will see how to perform a basic search option. There are many ways to accomplish the same. We will see the simplest way. The intention is to be cognizant of how a search operation is performed. In the repository, we will search for the page that contains a property author which is set to Sunil.

  1. Go to the interface and add the following method:
    public String getResult();
  2. In the SearchServiceImpl class, create a Resource Resolver. We have discussed this in length in of the previous sessions.
    Map<String, Object> param = new HashMap<String, Object>(); 
    param.put(ResourceResolverFactory.SUBSERVICE, "readService");
    ResourceResolver resourceResolver=null;
    resourceResolver = resolverFactory.getServiceResourceResolver(param);
  3. Create a session object using the adptTo method.
    Session session = resourceResolver.adaptTo(Session.class);
  4. Create a QueryManager object as follows:
    javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager();
  5. Create a query String.
    We need to find the page that contains author property as Sunil.

    String sqlStatement = "SELECT * FROM [cq:PageContent] WHERE CONTAINS(author, 'Sunil')";
  6. Create a JCR query.
    javax.jcr.query.Query query = queryManager.createQuery(sqlStatement,"JCR-SQL2");
  7. Obtain the result as follows:
    javax.jcr.query.QueryResult result = query.execute();
  8. Create a JCR node iterator and convert the result into an iterator.
    javax.jcr.NodeIterator nodeIter = result.getNodes();
  9. Use the following code to find the node and get its properties, such as author and title.
    while ( nodeIter.hasNext() ) {"From the search");
     javax.jcr.Node node = nodeIter.nextNode();
     title = node.getProperty("jcr:title").getString();
     author = node.getProperty("author").getString();
  10. Return author.
  11. Access the bundleservice component. Add the following code in the default script.
    <%= searchService.getResult()%>
  12. Refresh the page we created. It should display the value of the author property.

Update a node property using an AEM bundle

In the previous session, we have seen how to obtain a Resource. We later, translated the Resource into a Page object, and then programmatically obtained its title. We will see one more example. Here, using an AEM bundle, we will write a property to the Page we created. In this case too, we need to use a Resource object to obtain a Session object using which we can save what we write.

Let us start:

  1. Create a new interface:
    (There is a reason for using this name. In the next session, we are going to discuss about searching.)
  2. Add the following method.
    public void addProperty();.
  3. Create the implementation class:
  4. Implement the addProperty() method.
  5. Get the ResourceResolver.
    (Visit the previous post to see what the code accomplishes.)

    Map<String, Object> param = new HashMap<String, Object>();  
    param.put(ResourceResolverFactory.SUBSERVICE, "readService");
    ResourceResolver resourceResolver=null;
    resourceResolver = resolverFactory.getServiceResourceResolver(param);
  6. Obtain the resource.
    Resource pageResource = resourceResolver.getResource("/content/aem-company/jcr:content");

    Note that we need to add the property to the jcr:content node of the page we created.

  7. Translate the resource into a node using the adaptTo() class.
    Node myNode = pageResource.adaptTo(Node.class);
  8. Set a property to the node.
    myNode.setProperty("author", "sunil");
  9. Obtain the session from resourceResolver.
    Session session = resourceResolver.adaptTo(Session.class);
  10. Save the session:;
  11. Deploy the bundle.
  12. Add a method in the bundleservice component.
    com.aemcompany.myproject.SearchService searchService=sling.getService(com.aemcompany.myproject.SearchService.class);
  13. Access the page so that the method is called.
  14. Log into CRXDELite.
  15. See the page properties and ensure that the new property is added.
    That’s it. See you soon.

Access a resource property using an AEM bundle

In this session, we will learn about how to access resources using bundles deployed in the Felix console.  After all, for Sling, everything is a resource. We will update the bundle we developed with a method that accesses a node and prints its title. Though it sounds very simple, we will learn about a very important interface ResourceResolverFactory, that helps us to get ResourveResolver. ResourceResolver gets the Resource object that provides the functionalities to manipulate the nodes and its properties. We will also learn about the adptTo() method that helps to translate an object into a different object type.

So, let us start:

  1. Add the following method in HelloService interface:
    public String getPageTitle();
  2. Add the following ResourceResolveFactory reference:@Reference ResourceResolverFactory resolverFactory;
  3. Implement the getPageTitle() method.
  4. Create a hashmap object with a string & object combination as shown below:
    Map<String, Object> param = new HashMap<String, Object>();
  5. Put ResourceResolverFactory.SUBSERVICE and the service associated to the HashMap object.
    param.put(ResourceResolverFactory.SUBSERVICE, "readService");

    This is a new security layer added in AEM on 6.1 version. It means that the resolveResolver object we create is used by a readService process that we would be created later.

  6. Now create a reference to the ResourseResolver.
     ResourceResolver resourceResolver=null;
  7. Create resourceResolver object as shown below:
    resourceResolver = resolverFactory.getServiceResourceResolver(param);

    As I mentioned earlier, for Sling, everything is a resource. Generally, you use Sling APIs for doing any content manipulation. Getting a ResourceResolver object is imperative for accessing the Nodes in Oak, and later doing various operations, such as Create, Read, Update, and Delete (CRUD). You can compare it to creating a connection object in Java to a database.

  8. Now create a resource object using the getResource() method with a path to the company page as argument.
     Resource pageResource = resourceResolver.getResource("/content/aem-company");
  9. Translate the resource into a Page object using the adaptTo() class.
    Page myPage = pageResource.adaptTo(Page.class);

    Note that the method takes the class name to which we want to translate the resource as an argument. We do this operation to avail various APIs associated with various objects, such as Node, Page, and so on. In this case, we use the getTitle() method in the Page object. This can throw a null or Login Failed exception, and that’s the reason we added a try catch block.

  10. Use the following Page method to get the resource title.
    String title = myPage.getTitle();

Update the bundle service component
Update the bundleservice page, so that we can test if the bundle returns the value.

  1. Login to CRXDE LIte.
  2. Update the bundleservice page.
     <%= repositoryService.getPageTitle()%>

Create a service user mapper

  1. Login to System console.
  2. In OSGI Configuration,  search for Apache Sling Service User Mapper Service.
  3. Create a new service mapping as shown below:
  4. The first part is the bundle name. Second part is the service name that we used in the method. (Step: 5) Third part is the system user who can access the service.
    We are yet to create the system user. Let us do that.

Create a system user and provide access rights

  1. Login to CRX Explorer.
  2. Select User Administration > Create System User.
  3. Enter user ID.
  4. Select Yes for creation.
  5. Provide access to the user to avail the paths.
  6. Search for the user. (myUser)
  7. Provide full access to the content path.
  8. Then, Save the user.

Test the component

  1. Access the page we created.
    Note that it displays AEM Company, the title of the node.

Manage Sling requests using Java Servlets

In this session, we will see how to manage Sling requests using Java Servlets. Servlets are programs that run on a Web server and act as a middle layer between a request and a response. You can deploy Servlets in the Felix console and use them for managing the request from clients. An example: you need to print a message for the request coming from a certain path. Something like, “Thanks for the patience – Coming soon,” because the page is not yet ready.

  1. Import the following classes:
  2. Add the following Sling Servlet annotation:
    @SlingServlet(paths="/customer/casestudy", methods="GET")

    You can also use more generic scr plugin annotation as follows:

        @Property(name = "sling.servlet.resourceTypes", value = "sling/servlet/default"),
        @Property(name = "sling.servlet.methods", value = "GET")
  3. Ensure that the class extends SlingSafeMethodsServlet.This class is used to read data in Sling. Since we extended a serializable class, add the version number associated with a serializable class. It’s not Sling-specific; it is specific to serializable classes in Java.
  4. Create a doGet() method as follows:
    If you don’t have a doGet() method, the response throws an error.

    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException
      response.setHeader("Content-Type", "application/json");
      response.getWriter().print("Thanks for the patience. Coming soon!!");
  5. Build the project after deleting the existing bundle.
  6. Check if you can resolve the path to the correct servlet.
    1. Log into the  system/console.
    2. Go to Sling > Sling Servlet Resolver.
    3. Enter bin/casestudy.
  7. Now access the node http://localhost:4502/customer/casestudy.
  8. Note that it displays the response.
    That’s about the session. See you soon.

Here is the complete class:

package com.aemcompany.myproject.impl;

@SlingServlet(paths="/bin/casestudy", methods="GET")
public class ServletResponse extends SlingSafeMethodsServlet{
private static final long serialVersionUID = 1L;
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException
response.setHeader("Content-Type", "application/json");
response.getWriter().print("Thanks for the patience. Coming soon!!");

Create configurable parameters in an OSGi component

In this session, you will see how to configure values for various parameters in an OSGi bundle and obtain the same for further processing.
We will also learn about ComponentContext object that helps to interact with a component.

(As usual, it’s a continuation of the previous session. Obtain the latest package and project from GitHub.)

Following are the prerequisites:

  • You should have installed the aem-company package.
  • You should have downloaded the AEM project and would had imported it into Eclipse.
    (Both these packages are available in my GitHub account.)

We will accomplish the following in this session:

  • We will modify the bundle service component that prints “Hello Apache Jackrabbit Oak.” We created the component to see how to print the value returned by a service in a bundle.
  • We will develop a component that will help us to configure the salutation. That is, it prints the salutation (now, Hello), based what we configure. We may configure it to print as “Hi Apache Jackrabbit Oak.


Update the project with the dependent jar files
We need to import class. Means, we need to add the corresponding jar to the Maven project.

  1. Go to
  2. Search for
  3. Select the first class displayed.
    Following is the entry that we need in the POM file.

    <!-- -->
  4. Open the bundle POM and add the following:
  5. Right-click Maven > Update Project.
    This will download the required jar to the project.

Add a property to the component
Add a property that can be configured:

  • Add the following to the class as shown below:
    @Component(immediate = true, metatype = true, label = "Salutation Service")
    @Property(name = "Label", description = "The Salutation", value = "Hello")

    It creates a service with configurable property, The Salutation.
    Default value of salutation is Hello.

Update the project with the logic

  1. Create a protected activate() method.
    Activate method gets executed when a component is activated. It takes a single argument, ComponentContextComponentContext object helps to interact with the execution context of a component. This needs to be called from the activate() method. We will later setting the class variable salutation based on the value entered in the component.

     protected void activate(ComponentContext componentContext)
        this.salutation= PropertiesUtil.toString(componentContext.getProperties().get("Label"),null);

    PropertiesUtil is a utility class that is used converting property types. The componentContext.getProperties().get(“Label”) method gets the value in the Label property; null is the default value. Rest is simple.

  2. Add a getSalutation method that returns salutation.
     public String getSalutation()
       return salutation;
  3. In the HelloService interface, add the method that we just implemented.
    public String getSalutation();

Remove the existing bundle
We are not doing a clean install now. We will handle that topic later.

  1. Log in CRXDE Lite.
  2. Move to aem-company/install.
  3. Delete the bundle.
  4. Now build the project.

Test the component

  1. Let us update the bundle-service component.
  2. Add the following in the default jsp script:
     <%= repositoryService.getSalutation()%>

    The code becomes:

    <%= repositoryService.getSalutation()%> <B><%= repositoryService.getRepositoryName() %> </B>

    Using the sling.getService() method, we created an object to the HelloService interface, and called the methods it exposed.

  3. Go to system console.
  4. Go to the Components tab.
  5. Locate the HellService component.
  6. Select the Configure button.
  7. Enter the label as Hi.
  8. Refresh the web page we created earlier.
    Notice that it has updated.

That’s about the session. You learned how to obtain values from a component using the ComponentContext.
See you soon.


Enable logging for a bundle

In this session, we will see how to enable logging for a bundle. We will later create a logger configuration in Felix console and will specify a file to write the logs. Enabling logging in a bundle helps you a lot while debugging. We will use Simple Logging Facade for Java (SLF4J). It’s an abstraction of various logging frameworks based on Java.

Note that it’s a continuation of my previous session. Refer to the previous videos when in doubt. You can download the sample project from GitHub. You may need and

  1. Let us open the Eclipse project that we have created.
  2. Locate the class.
  3. Import org.slf4j.Logger and org.slf4j.LoggerFactory.
  4.  Add the following statement.
    public static final Logger LOGGER = LoggerFactory.getLogger(HelloServiceImpl.class);

    Using the LoggerFactory instance, it creates a logger for the class passed.

  5. Inside the getRepositoryName() method, add the Logger.Info method."Adding the repository");
  6. Log into Felix console:
  7. Go to Sling > Log Support.
  8. Select Add New Logger.
  9. Provide the logs name. (logs\aem-company.log)
  10. Then, select the logger class name. (com.aemcompany.myproject)
  11. Now access the web page created earlier.
    It has a component that uses this class to print the repository name.
    Means, when we access the page, the method gets executed – resulting a log entry.
  12. Go to the crx-quickstart\logs folder.
    Note that the aem-company log is created.

Convert an AEM Maven project to an Eclipse-based project

In the previous session, we created an AEM project based on an archetype. We explored the project, deployed it into the Felix console, and then accessed the service it exposed. In this session, we will convert the project into an Eclipse project. So that we can import the project into Eclipse. And, later use Eclipse as an IDE for further learning.

Convert project into an Eclipse project
We can use a Maven command to do this.

  1. Navigate to the project directory.
  2. Execute the following command:
    mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true

The project is converted into a Maven project.

Import project into Eclipse and create a Run configuration

  1. In Eclipse, select Import > Existing Project Into Workspace.
  2. Navigate to the project directory and select Finish.
    Bundle and Content projects get imported into the current workspace.
  3. Update the POM files of respective projects, so the bundle and content are created in the aem-company project (that we have been working with.)
    In the bundles’s POM, update the Sling URL property as follows:

  4. In the Content POM, change the target configuration from myproject to aem-company:
  5. Create a Run profile.
  6. Select Run > Run Configuration.
  7. Under Maven Build, right-click > New.
  8. Name it as AEM.
  9. Update Base Directory to your project’s base directory.
  10. Add the following:
    Goal: Install
    Profile: AutoInstallBundle
    Save the configuration.
  11. Using CRXDE Lite, navigate to apps/aem-company.
  12. Create a folder named install.
  13. Now run the configuration.

The project is successfully compiled and deployed into Felix console. You can go to CRXDE Lite and check the Install folder that we created now to see the bundle.
(If you didn’t get it correctly, download the project from my GitHub account.)

Create a bundle for AEM using Maven

In this session, you will learn about developing and deploying bundles for AEM development.

(Following is the transcript of the video above. See the video to get the right context. For sample files, visit GitHub.)

Bundles help to modularize Java development using the Open Source Gateway Initiative (OSGi) framework. A bundle is essentially a Jar file that you deploy in the Apache Felix console. Felix console is the OSGi container.

To make it simpler, to accomplish a complex task, you can deploy a bundle in Felix console that runs with AEM, and then avail the services offered by the bundle. We are not going to explore a lot about OSGi bundles. We will see how a bundle can easily be developed, deployed, and used. You need to have installed Maven in your system. I am not going to talk about how it can be done.
There are plenty of information available in the Internet. Please install Maven.

I am on a Windows machine. After installing Maven, if I run mvn in a command prompt, this the the result that I get. You should see something similar if you had a successful installation and you added it to your class path.

I have created directory, aem-company. This is the company name we used for our sample project. Let me move to the directory.

I will run the following command to create an AEM project. (Please refer the video.) The command is long; I have added the command in my GitHub repository.

Using this command, we will create an AEM project using a Maven archetype. Archytype is a project templating toolkit. Basically, it creates a sample project from which you can start your work.
Note that Adobe provides Archetype repository URL for the developers. I don’t want to explain rest of the parameters. It would be too complex for this session. After running the command, a project structure is created.

Let us explore it further. There are two folders: bundle and content. We will see bundle first. It primarily created a Java project. By default, it added the first Java class that you can use as a start.
You can use this project structure to add more classes.

It created an Interface, HelloService. And, it implements a getRepositoryName() method that should return the underlying repository name. In this case, Apache Jackrabbit Oak is the repository we use.

Go to the Impl folder where we have the actual implementation. @service and @content are two JCR annotations we will see later.

The implementation class has a getRepositoryName() implementation that returns the repository name.
Now, let us deploy the project.

Run the following command: mvn -PautoInstallPackage install
Let us wait for it to complete.

Now log into Apache Felix:

Locate the bundle by searching for AEM-Company.
You could see the service we deployed now: com.aemcompany.myproject.HelloService

Let us quickly access the service. I have developed a component that uses the service. Obtain it from my GitHub. I used a JSP page as the component’s default script, since it’s easy to access a Java service in a JSP page.

com.aemcompany.myproject.HelloService repositoryService=sling.getService(com.aemcompany.myproject.HelloService.class);
Hello, <B><%= repositoryService.getRepositoryName() %> </B>

Let us go to the AEM page we created. Drag and drop the component to the page.
Note that it displays the repository name.

Download AEM company training complete package

Download the complete package from my GitHub account. Package name is I thought it will help you to learn quickly.
Have a great 2016!!