Ad Code

Sunday, July 30, 2017

Migration of SCR annotations to OSGi R6 annotations in AEM 6.3


I have recently gone through a migration project from an older version of AEM to AEM 6.3 and the major challenge while migrating java classes is that sometimes the API become deprecated in the newer version.

From AEM 6.2, One of our favorite Apache Felix Maven SCR Plugin has been deprecated and in place of this, Maven Bundle Plugin (version 6.2 or greater) has been introduced.
It always take a small effort to adapt new things, But change is the nature’s law. So here we will discuss how OSGi plugin has replaced all SCR annotations in AEM.
Packages:
Now the package  “org.apache.felix.scr.annotations.*” will be replaced with “org.osgi.service.component.annotations.*”  and “org,osgi.service.metatype.annotations.*
While using Maven SCR plugin, we can find the Declarative Service(DS) output under /target/classes/OSGi-INF but using OSGi Bundle Plugin our DS output are found in /OSGI-INF, packaged under the compiled JAR File.

Note:The Apache Felix SCR annotations are replaced in AEM 6.3 projects with OSGi R6 annotations

How To start with OSGi Annotations?

1.Add a plugin in Parent Pom.xml
<plugin>
 <groupId>org.apache.felix</groupId>
 <artifactId>maven-bundle-plugin</artifactId>
 <version>3.2.0</version>
 <inherited>true</inherited>
</plugin>

2.Add the following maven dependencies.
<dependency>
 <groupId>org.osgi</groupId>
 <artifactId>org.osgi.service.component.annotations</artifactId>
 <version>1.3.0</version>
</dependency>

<dependency>
 <groupId>org.osgi</groupId>
 <artifactId>org.osgi.annotation</artifactId>
 <version>6.0.0</version>
</dependency>

<dependency>
 <groupId>org.osgi</groupId>
 <artifactId>org.osgi.service.metatype.annotations</artifactId>
 <version>1.3.0</version>
</dependency>

<dependency>
 <groupId>org.osgi</groupId>
 <artifactId>org.osgi.compendium</artifactId>
 <version>4.2.0</version>

<scope>provided</scope>
</dependency>

NoteMake sure that while building the code,plugin is working fine.

Migration of OSGi Components and Service

Before : In SCR Annotations:

@Component(name=”Test Service”,immediate=true,description="this is description")
@Service(TestService.class)
public class TestService {
                     public String getName()
{
return "testService";
}
}

After:In OSGi Annotations
@Component(name=”Test Service” service=TestService.class,immediate=true)
public class TestService {
                     public String getName()
{
return "testService";
}
}

Migration of Servlet

OSGi annotations has reduced our effort to remember a lot of Annotations.

In the Sling Servlets:
We used to use @Component, @Service @SlingServlet @Properties in SCR Annotations.
OSGi annotation just have @Component with the collaboration of all these annotations.

Before : In SCR Annotations:
@SlingServlet(paths="/bin/servlet",selectors = {"test","test1"},extensions = "html")
public class Test extends SlingSafeMethodsServlet{
  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)throws IOException
  {
      response.getWriter().print(" I am in doGet Method");
  }
}

After:In OSGi Annotations

@Component(service=Servlet.class,
          property={
                  Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
                  "sling.servlet.methods=" + HttpConstants.METHOD_GET,
                  "sling.servlet.resourceTypes="+ "com.poc.osgiannotation/components/structure/page",
                 "sling.servlet.paths="+ "/bin/servlet",
                  "sling.servlet.extensions=" + "txt"
          })
public class SimpleServlet extends SlingSafeMethodsServlet {
{
@Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)throws IOException
  {
      response.getWriter().print(" I am in doGet Method");
  }
}

Custom OSGi Configuration in AEM

OSGi annotations provided a flexibility to create configuration in a separate interface and we can use it in any place.
The OSGi Configurations generates metatype specification that provides a way to describe configuration of the component.Such a metatype information can be used at runtime to generate forms to edit/create the configuration of a component. For example the Apache Felix web console does exactly that.

Let’s understand how to make OSGi configurations in AEM using OSGi Bundle Plugin.


configuration.PNG
Fig- OSGi COnfiguration in felix Console

Migration of Custom Workflow Process Step

Before:In SCR Annotations
@Component(immediate = true, enabled = true, metatype = true)
@Service(value = WorkflowProcess.class)
@Property(name = "process.label", value = "Activate Page", propertyPrivate = true)
public class RejectionMailProcess implements WorkflowProcess {
public void execute(WorkItem workitem, WorkflowSession wfsession,MetaDataMap metaDataMap)
{
System,.out.println(“ I am in execute method”);
}
}

After: In OSGi Annotations
@Component(service = WorkflowProcess.class, property = {"process.label=Activate Page" })
public class TestWorkflow implements WorkflowProcess {
public void execute(WorkItem workitem, WorkflowSession wfsession,MetaDataMap metaDataMap)
{
System,.out.println(“ I am in execute method”);
}
}


How to use @Reference Annotations in OSGi Annotations?

There are multiple ways to use @Reference annotations in OSGi annotations:

1.
@Reference
TestService testService;

2.
private TestService testService;
@Reference
public void bindTestService(TestService testService) {
     this.testService = testService;
}

public void unbindTestService(TestService testService) {
     this.testService = testService;
}


If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com


Hope it will help you guys !!
Thanks and Happy Learning

Sunday, July 23, 2017

Tips and Tricks of HTL(Sightly) Development in AEM 6.3


Hello Everyone,
In the AEM world, There are a lot of changes in every moment. As the technology is growing , Adobe has done a wonderful job in the field of developer tools and maven-plugins that can make the development easier. I am not a great fan of developer tools, But what if
  • They made our manual tasks automatic.
  • They just speed up our development.
  • They saves our time and stop it at the same place where we are doing wrong.
If all the above reasons exist, so yes i would like to give some time from my hectic schedule  to install that on my machine and take a lot of long term advantage of it.

So today we will talk about some advantages that can help me in development
HTL Maven Plugin
Before this plugin, There was not any way to validate our sightly (HTL) templating language. Either it is right or wrong(syntactically) ,the code gets build and when we see the page we got to know...ah… something is wrong.This plugin came in picture as “Prevention is better than cure”.
  We just add this plugin in our content’s pom.xml and it validates HTL at the time of build. So there is no possibility of syntactical error at run time and saves a lot of effort of developer to find out that which syntax he/she has missed out.
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>htl-maven-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<sourceDirectory>${basedir}/src/main/content/jcr_root</sourceDirectory>
</configuration>
<executions>
<execution>
<id>validate-scripts</id>
<goals>
<goal>validate</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
AEM Brackets Extension
Before this tool,I was always using VLT tool but this can be a great alternative of vault in AEM.

Benefits over Vault:
  • Automatic Syncing
  • List each and every file you have changed in brackets, so that unnecessary changes won’t go.
  • Easy to use console
Steps by Steps installation of Brackets AEM Extension:
  • The AEM Brackets Extension supports Brackets version 1.0 or greater.Download the Latest Brackets from http://brackets.io/
  • Add AEM Brackets Extension:
    • Go to File-> Extension Manager
    • Search AEM Brackets Extension in the Search Bar.
    • Install the software

Important to Note:
  • Sometimes install become failed so you can use “install from URL “ and give the github path here.
installed software.PNG
Fig - AEM Brackets Extension 
  • If this also doesn’t work,you can download the zip from github and put it on “drag zip here”.Drag zip here worked for me.
  • After the installation of extension, you can open content package folder (jcr_root)of the project in brackets.

Connect Brackets with Server:

    In order to synchronize your content to and from development AEM instance , You need to define your project Settings.
      • Go to AEM->Project Settings
      • Add configurations like this:
    project_settings.PNG
    Fig - Synchronization settings
    This is automatically generated “brackets.json” file in brackets which contains all the configurations,added in the above dialog.
    brackets.PNG
    Fig - configuration file

    Advantages of AEM Brackets Extension:

    1. Synchronization Content:
    The AEM Brackets Extension provides synchronization based on the rules written in filter.xml . So filter.xml play a very important role in syncing the content.
      • Automatic Synchronization of Changed Files:
    This synchronize changes from Brackets editor to AEM but the automatic synchronization is not applicable from AEM instance to brackets editor.
      • Manual Bidirectional Synchronization:
    In the Project Explorer, You can Export to Server and Import from Server options which right clicking on a file and folder.

    1. Synchronization Status:The AEM Bracket Extension provides a notification icon on the right hand side of console, which indicates the status of the synchronization.
    Different colors represent different status of synchronization:
    Green : All data has been synchronized
    Blue: Data synchronization is in progress.
    Red: Data is unable to synchronized.
    Yellow: Some of the files are not synchronized.
    1. The full content package(whole project) can be synchronized by using the options like Export Content Package and Import Content Package in the AEM Menu.
    2. The AEM Brackets Extension provides some auto completion options that helps to write “HTL attributes” and “expressions”.
    The Demonstration video on AEM Bracktes Extension:


    AEM HTL Read–Eval–Print Loop

    This is also an interesting tool which provides a live coding environment to test HTL scripts and see outputs.For Learning perspective it is quite easy to see the output on the same page.
    To install this on your AEM instance:
    1.Download the Package
    2. Install it in AEM Package Manager.

    Now you can see the console from here.
    repl.png
    Fig - REPL tool
    New features of HTL in AEM 6.3
    HTL (HTML templating Language) is in a great demand nowadays, in terms of features it is growing after every new version of AEM .
    So here we will discuss what happiness AEM 6.3 bring for us:
    1.Fetch the properties of a resource : I think this is the best feature till date as when we need to fetch some properties of a resource, we need to go for a Java class( either Sling Model and WCMPojo) .But now what we can do:
    <sly data-sly-use.node="/etc/commerce/">${node.jcr:title}</sly>

    2. Request Attributes: In the data-sly-include and data-sly-resource you can now pass requestAttributes in  order to use them in the receiving HTL-script.
    Note: You can also Download the Package for this and do it at your place.
    3. Date Formatting: Before AEM 6.3 ,If there is a need to format any date in HTL, you need to use a java class to do that.But now this work has also become very easier to us as HTL provides a support of date-format,timezone and locale.
    Examples:
    1. <h2>${ 'dd-MM-yyyy' @ format=currentPage.jcr:created}</h2>

    1. <h2>${ 'dd-MMM-yyyy hh:mm:ss' @ format=currentPage.jcr:created, timezone='PST'}</h2>

    1. <h2>${ 'dd-MMMM-yyyy hh:mm:ss' @ format=currentPage.jcr:created, timezone='PST', locale='fr'}</h2>

    4.Number Formatting:In HTL, now we can show the number in decimal format:

    <h2>${ '#.00' @ format=300}</h2>

    The Demonstration video on new Features of HTL:


    References: http://blogs.adobe.com/experiencedelivers/experience-management/new-htl-features-aem6-3


    If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com


    Hope it will help you guys !!
    Thanks and Happy Learning .