Monday, 28 August 2023

System user or Service user & Resource Resolver

System User/Service User

  • System or service users in AEM are specific types of users designated to perform operations with minimal permissions within a particular bundle. Unlike regular users, they cannot log into AEM using typical credentials.
  • It means they have specific kinds of users with which you can't log into AEM using normal login credentials, as you would for regular users. They have minimal permissions, meaning they are granted only the necessary permissions to perform their designated operations, and they are always mapped to a particular bundle.
  • For example, if I have a resource that requires updating, specifically adding a particular property to it, I delegate this task to a service user that is associated with a specific bundle.
What is the need of System User/Service User?
  • In AEM 6.0, when you needed to update content or resources programmatically, In AEM 6.0, there were two primary methods to obtain the necessary session or resolver from the repository: 
  1. The administrative session 
  2. The administrative Resource Resolver.
  • Main Methods
    • Session session = SlingRepository.loginAdministrative();
    • ResourceResolver rr = resourceResolverFactory.getAdministrativeResourceResolver();

  • Two operations to get resource Resolver or session using repository you will get administrative session or administrative resource Resolver by using this two methods we will get administrative permissions
  • It means whole repository access that was breaking the principle of list privileges means 
  • If you have two main method session and Resource Resolver you could update whole repository.
  • because you had all permissions it means all access.

If you have two main method session and Resource Resolver you could update whole repository than How to resolve this kind of issue?

  • In order to address this issue, AEM introduced the concept of System User or Service User with version 6.1 .
How to create System user and How you can map those system user to a particular bundle?





  1. Creating a system/service user can be done through three methods:
  • Using Repository Explorer, which is also referred to as the plain vanilla method.


For Login





After checking the green checkbox, click on "Close." Now, you have successfully created the system user.



Friday, 25 August 2023

Queue(Interface)

Queue



  • 1.5(v) enhancement of Queue(I).
  • It is the child Interface of Collection.
  • If we want to represent a group of individual objects Prior to Processing than we should go for Queue.
  • for eg: Before sending sms message all mobile no. we have to store all mobile no. in some data structure. In which order we added mobile no. in same order only message should be delivered for this FIFO requirement Queue is the best choice.
  • Usually Queue follows First In First Out [FIFO] order but based on our requirement we can implement our own Priority order also [Priority Queue].
  • From (1.5v) onwards LinkedList class also implements Queue Interface.
  • LinkedList based implementation of Queue always First In First Out [FIFO] order.
Queue Specific Method
  • boolean offer(Object o)
    • To add an object into the Queue.
    • Object peek()
      • To return head element of the queue. If Queue is empty than this method return Null.
    • Object element()
      • To return head element of the queue. If queue is empty than this method raise RE:NoSuchElementException.
    • Object poll()
      • To remove & return head element of the queue.If Queue is empty than this method return Null.
    • Object remove()
      • To remove & return head element of the queue. If queue is empty than this method raise RE:NoSuchElementException.
    For eg:

    Output :

    • q.peek() it returns head element of queue but if the queue is empty than it returns Null.
    For eg:




    output:

    • if we are calling q.element() or q.remove() of queue,if the queue is empty than it returns RE:NoSuchElementException.
    For eg:


    output:


    • poll() it means remove & returns head element of queue here 0 is the head element.
    • After remove head element 1 is the head element now output is 1,2,3...
    Priority Queue
    • If we want to represent a group of individual objects Prior to processing according to some priority than, we should go for PriorityQueue.
    • The priority can be either Default Natural Sorting or Customizer Sorting order defined by Comparator.
    • Insertion Order is not preserved & it is based on some priority.
    • Duplicate objects are not allowed.
    • If we are depending on Default Natural Sorting order compulsory the object should be Homogeneous & comparable otherwise we will get RE:ClassCastException.
    • If we are defining our own sorting by Comparator than objects need not be Homogeneous & Comparable.
    • Null is not allowed even as the first element also.
    Constructor
    • PriorityQueue q = new PriorityQueue();
      • Creates an empty priority with default intial capacity is 11. all object will be inserted according to Default Natural Sorting order.
      • PriorityQueue q = new PriorityQueue(int intialCapacity);
      • PriorityQueue q = new PriorityQueue(int intialCapacity Comparator);
      • PriorityQueue q = new PriorityQueue(SortedSet s);
      • PriorityQueue q = new PriorityQueue(Collection c);
      Note:
      • Some platform won't provide proper support for Thread priorities & priorityQueues.
      For eg of Customizer Sorting:





      output:


      Thanks
      Matri Sharma


      Tuesday, 22 August 2023

      Map Interface & Child classes of Map Interface(Part-2)

       Map Interface



      TreeMap
      • The underlying data structure is RED-BLACK tree.
      • Insertion order is not preserved & it is based on some sorting order of keys.
      • Duplicate keys are not allowed but values can be duplicated.
      • If we are depending on Default Natural Sorting Order than keys should be Homogeneous & comparable otherwise, we will get Run time exception saying class cache exception.
      • If we are defining our own sorting by Comparator than, keys need not be Homogeneous & comparable we can take Heterogeneous non comparable.
      • For Non-Empty TreeMap if we are trying to insert an entry with Nullkey than, we will get Run time Exception.
      • For Empty TreeMap as the 1st entry with Null key is allowed but, after inserting that entry if we are trying to insert any other entry than we will get Run time Exception.
      Note
      • The above Null Exception rule is applicable until(1.6v) only from (1.7v) onwards Null is not allowed for key.
      • But, for values we can use null any number of times there is no restriction whether, it is (1.6v)\(1.7v). 

      Constructor
      • TreeMap t = new TreeMap();
          For Default Natural Sorting of keys.
      • TreeMap t = new TreeMap(comparator c);
          For Customized Sorting.
      • TreeMap t = new TreeMap(m);
      • TreeMap t = new TreeMap(sortedMap m);
      For eg:

      output:



      • If we trying anything with null.
      for eg:

      output:
      • when we hit Heterogeneous key.
      • output is ClassCasteException.
      for eg:



      output:


      Hashtable
      • The underlying data structure for hashtable is Hashtable.
      • Insertion order is not preserved & it is based on HashCode keys.
      • Duplicate keys are not allowed & the values can be duplicated.
      • Heterogeneous objects are allowed for both key & value, otherwise we will get Run Time Exception saying Null pointer Exception.
      • It implements Serializable & cloneable interface but not Random Access.
      • Every method present in Hashtable is Synchronized & hence, Hashtable object is thread safe.
      • Hashtable is the best choice if our frequent operation is Search operation.
      Constructor
      • Hashtable h = new Hashtable();
           create an empty Hashtable object with default initial capacity       11 and default fill ratio is 0.75.
      • Hashtable h = new Hashtable(int intialCapacity);
      • Hashtable h = new Hashtable(int intialCapacity ,floatFillRatio);
      • Hashtable h = new Hashtable(map m);
      for eg:

      output:

      • If we trying anything with null.
      for eg:

      output :


      • If we configure initial capacity as 25. output is different.
      for eg:


      output:




      Properties
      • In our program if anything which changes frequently[for eg: User name, password, mailid, mobile no etc.] are not recommended to hard core in java program.
      • Because, if this is any change to reflect that change Recompilation, Rebuild, Redeploy application are required. Even sometime server restart also required, which creates a big business impact to the client.
      How can overcome of this problem? 
      • we can overcome this problem by using properties file. such type of variable things we have to configure in the properties file from that properties file we have to read into java program & we can use those properties.
      • The main advantage of this approach is:
      1. If there is a change in properties file to reflect that change just a redeployment is enough which won't create any business impact to the client. 
      2. We can use java properties to hold properties which are coming from properties file.
      3. In normal map [for eg: Hashtable, HashMap ,TreeMap etc.] key & value can be any type but in the case of properties key & value should be String Type.
      Constructor
      • Properties p = new Properties();
      Method of Properties
      • String setProperty(String pname, String pvalue)
                To Set a new property. If the specified property already             available than the old value will be replaced with new value         and return old value.
      • String getProperty(String pname)
             To get value associated with the specified property. If               specified properties than this method return Null.
      • Enumeration propertyNames()
      Another method
      • void load(InputStream is)
            To load properties from properties file into java properties          object.
      • void store(OutputStream os , String comment)
            To store properties from java properties object into 
            properties file.


      for eg:


      • This is properties file abc.properties.
      • without having properties file you can't expect any single java project.
      output:


      • Now let see what happened in properties file.


      •  If we change in file.
      for eg:


      • Some changes doing in password old password = Mohan new password = Mohan@123456*
      • Automatically changed in output after execution.
      output:



      • After execution password has been changed.
                        


      Thanks
      Matri Sharma

      Tuesday, 8 August 2023

      Map Interface & Child classes of Map Interface(Part-1)

       Map(Interface)


      • Map is not child Interface of Collection.
      • If we want to represent a group of object as Key-Value pairs, than we should go for Map.
      • for eg:
      • Both Keys & values are objects only.
      • Duplicates keys are not allowed, But values can be duplicated.
      • Each key-value pair is called Entry hence, Map is considered as a collection of Entry Objects.
      Map(Interface) Method

      1. Object put(Object key, Object value)
      • To add one key value pair to the map.
      • If the key already present than old value will be Replaced with New value and Return Old value.
      • for eg:
              m.put(101,"Shiva"); 
              m.put(102,"Ravi");
              m.put(101,"Raha");
         output is 101-Raha,not Shiva because Old value replaced with new     value.
        2.void putAll(map m)
       
        3.Object get(Object key)
      • Returns the value associated with specified key.
        4.Object remove(Object key)
      • Removes the entry associated with specified key.
        5.boolean containsKey(Object key)
        6.boolean containsValue(Objectvalue)
        7.boolean isEmpty()
        8.int size()
        9.void clear()

      Another Method
      1. Set keySet()
      2. Collection values()
      3. Set entrySet()
      These are Collection values of Map.

      Entry(Interface)
      • A map is a group of key-value pairs & each key-value pair is called an Entry. Hence, Map is considered as Entry Objects.
      • Without Exiting Map object there is no chance of exiting Entry Object, Hence Entry(Interface) is defined inside Map Interface.
      HashMap
      • The underlying Data Structure is Hashtable.
      • Insertion order is Not Preserved & it is based on HashCode of keys.
      • Duplicates are Not allowed, but values can be Duplicated.
      • Heterogeneous objects are allowed for both key & value.
      • Null is allowed for key.[Only Once]
      • Null is allowed for values.[Any Number of Times]
      • HashMap implements Serializable, cloneable Interface but Not Random Access Interface. 
      • HashMap is best choice if our frequent operation is Search Operation.
      Constructor

        1. HashMap m = new HashMap();
        • Creates an Empty HashMap Object with default initial capacity 16 & default fill Ratio 0.75.
                     2. HashMap m = new HashMap(int intialCapacity);

              Creates an Empty HashMap Object with specified initial                capacity & default fill ratio 0.75.

            3.HashMap m = new HashMap(int initialCapacity, floatFillRatio);
            4.HashMap m = new HashMap(map m);

      Difference B/w HashMap & Hashtable



      • Demo of HashMap


      Output is:




      How to get Synchronized version of HashMap Object?
      • By default HashMap is Non-Synchronized but we can get Synchronized version of HashMap by using Synchronized Map.
                 HashMap m = new HashMap();
                 map m1 = collections.synchronizedMap(m);


      LinkedHashMap
      • It is the child class of HashMap.
      • It is exactly same as HashMap[Including Methods & Constructors] except the following differences.
      Difference B/w HashMap & LinkedHashMap




      Note: 
      • LinkedHashSet & LinkHashMap are commonly used for developing Cache based application.
      IdentityHashMap
      • It is exactly same as HashMap[Including Methods & Constructor] except the following difference.
      • In the case of Normal HashMap [JVM will use .equals()] method to identify Duplicate keys which is meant for content comparison.
      • But, In the case of IdentityHashMap JVM will use(==)operator to identify Duplicate keys, which is meant for difference Comparison.
      Difference B/w (==)operator & .equals() methods
      • In general(==)operator meant for Reference Content Comparison bracket Address Comparison.
      •  .equals() method meant for Content Comparison.

      for eg:

            


       Output is:



      • I1 & I2 are Duplicate keys.
      • I1.equals(I2) return True.



      OutPut is:



      • If we replace HashMap with IdentityHashMap than, I1 & I2 are not Duplicate keys. Because [I1==I2] returns False.
      WeakHashMap

      • It is exactly same as HashMap except the following difference.
      Difference B/w WeakHashMap & HashMap
      • In the case of HashMap even though Object does not have any reference. It is not eligible for Garbage Collector. If it is associated with HashMap that is HashMap dominate Garbage collector. 
      • But In the case of WeakHashMap, If object does not contain any references it is eligible for Garbage Collector. Even object associated with WeakHashMap, that is Garbage Collector dominate WeakHashMap.

      SortedMap

      • It is child Interface of Map.
      • If we want to represent a group of key-value pairs according to some sorting order of keys than we should go for Sorted Map.
      • Sorting is based on key but not based on value.
      SortedMap defines the following specific methods
      • Object firstKey();
      • Object lastKey();
      • SortedMap headMap(Object Key);
      • SortedMap tailMap(Object Key);
      • SortedMap subMap(Object Key1, Object Key2);
      • Comparator comparator();
      for eg:


      • firstKey()-101
      • lastKey() -135
      • headMap(107)-{101-A,103-B,104-C}
      • tailMap(107)-{107-D,125-E,135-F}
      • subMap(103,125)-{103-B,104-C,107-D}
      • comparator()-null


      Thanks
      Matri Sharma



      OSGi Service

      What is OSGi Service?

      • OSGI services are the reusable services just like method in java.
      • Suppose, we have functionality like email triggering so, we will have requirement to reuse this email triggering functionality in whole website.
      • So, instead of writing this code again & again we will write this in one time in OSGI Service.
      • It has additional annotation which let AEM manipulate this OSGI Service as per the Bundle & Life cycle i.e:- Start, Stop etc.
      What is the mandatory component that designates a class as an OSGi service?

      • We should always have an interface when you are writing a service and implement that interface.
      • The mandatory annotation is @Component, which designates a class as a service.
      • Inside that you have [Service = whenever your interface is and this is mandatory part].
      • Some optional methods are executed when your service is activated and your bundle is deployed. As soon as your service is activated, the method that is returned in the 'activated' state is executed.
      Optional Method in OSGI Service and Life cycle of OSGi service




      @Activate Annotation

      • In any method, if you add an 'activate' annotation, it will execute at the time of service activation.
      @Deactivate annotation
      • In OSGi services, the @Deactivate annotation is used to mark a method as the deactivation method for the service. 
      • This method is called when the service is being deactivated by the OSGi container.
      @Modified annotation
      • The @Modified annotation is used in Apache Felix. 
      • other OSGi frameworks to mark a method that should be called whenever the properties of the component change.
      How to write OSGi service?

      Step1:-

      • Write an interface named 'ServiceWithModelOsgi'.


      Step2:-
      • Write a class named 'ServiceWithModelOsgiImpl'.

      Note:-
      • "In any backend module, we can choose to call an OSGi service. It's important to note that without a backend module, we cannot call an OSGi service. While we typically use the '@Reference' annotation to call an OSGi service, when using a Sling model, we use the '@OSGiService' annotation."

      Step3:-

      • Write a interface named 'ServiceWithModel'.


      Step4:-
      • Write a class named 'ServiceWithModelImpl'.



      Thanks
      Matri

      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...