Monday, 18 March 2024

OSGi Service with filter and target in AEM

1. What happened when service interface has multiple implementation?

  • We can understand this concept for certain use cases


  • when we dealing with multiple implementations of an interface, 
  • such as a OsgiFilterDemo. For instance, 
  • let's consider having two classes: OsgiFilterDemoAImpland OsgiFilterDemoBImpl
  • Both of these classes implement methods like (getFirstName) and (getSecondName).
  • When invoking the service without explicitly specifying which implementation to use 
  • The default behavior typically refers to the original service. 
  • In this case, if no specific implementation is mentioned, OsgiFilterDemoAImpl will be called  because it has a lower service identifier compared to OsgiFilterDemoBImpl.








  • Create Dummy Component



If you want to render data in an HTML file using Sightly,





In this case, if no specific implementation is mentioned, OsgiFilterDemoAImpl will be called
because it has a lower service identifier compared to OsgiFilterDemoBImpl.



  • Suppose I want a particular implementation class, OsgiFilterDemoBImpl, to be invoked. In that case, I can define ranking using the @ServiceRanking annotation. The service with the higher ranking will be called.
  • If you want to invoke OsgiFilterDemoAImpl instead, consider giving it a higher service ranking. 




 


  • Suppose we want a specific class to be called each time, with the higher-ranking class always being invoked. However, there are times when we want either the OsgiFilterDemoAImpl or OsgiFilterDemoBImpl implementation class to be called without explicitly specifying the service ranking. In such cases, we can define a filter when calling the service, allowing us to decide whether to call OsgiFilterDemoAImpl or OsgiFilterDemoBImpl.





In other ways to implement class via Filter.









If you want to call both services in a Sling model simultaneously, you can do so.








The way you can call it in the Sling Model, but if you want to call it in a different backend module (Servlet, Scheduler, Workflow Listener, etc.), you can add a call to the OSGi service. Except for the Sling model, we can use the @Reference annotation to make the call from any other backend module. To call the classes OsgiFilterDemoAImpl or OsgiFilterDemoBImpl, you can utilize @Reference with the target parameter. By passing the target, you can define which implementation you want to call.










Tuesday, 12 March 2024

AEM Sightly/Htl(Html Template Language)

1.Global Object in sightly

  • HTL provides access to many objects useful to the developer. 
  • These objects can be used directly without having to specify anything explicitly.

  • properties: List of properties of the current resource.
  • pageProperties: List of page properties of the current page.
  • inheritedPageProperties: List of inherited page properties of the current page.




2.div data-sly-resource
  • data-sly-resource is a Sightly directive used to include and render a resource (typically a component or a fragment of content) within another component or page. 
  • It allows you to dynamically include content based on the specified path and resource type.



  • If we want to include multiple components and have them inherit properties or configurations from another component in AEM,





3.data-sly-list


  • list just iterates the items written inside its condition
  • It repeat the internal structure.
  • It doesn't repeat the complete structure.
  • It means repeat the child elements.



4.data-sly-repeat
  • It means whole container is repeated.
  • It repeat the complete structure.
  • It means repeats the elements.





When observing the output of using data-sly-list and data-sly-repeat, they appear identical.

The difference between data-sly-repeat and data-sly-list in Sightly 

View source page of data-sly-list 

  • only inner structure iterate.


View source page of data-sly-repeat 


  • complete structure iterate.
when working with data structures such as lists or arrays

  • index: zero-based counter (0..length-1)
  • count: one-based counter (1..length).
  • first: true for the first element being iterated.
  • middle: true if element being iterated is neither the first nor the last.
  • last: true for the last element being iterated.
  • odd: true if index is odd.
  • even: true if index is even.

5.data-sly-attribute
  • provide the functionality to add dynamically generate the attribute.








6.data-sly-unwrap
  • div data-sly-unwrap will unwrap or skip current tag and print its value.



8.data-sly-template

  • use to create a template or reusable html code which can be consume on multiple places by data-sly-call.
  • Using this approach we can easily include one html into another and pass data in the form of parameter.



9.data-sly-use
  • It means we can call any method present in your Sling model.

10.Time and Date





11.Selector




12. data-sly-set
  • It allow us to assign value to a custom variable as show below. 
  • We can take it as a assignment operator.




Thanks 
Matri Sharma

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...