1

I have recently come across a piece of code which is something like:

for(int i=0;i<10000;i++) { 
   ....
   PageManager manager = resourceResolver.adaptTo(PageManager.class);
   ....
}

Does it mean 10000 PageManager objects will be created or do we get the same object each time?

Thanks

Ruchi
  • 35
  • 1
  • 7

2 Answers2

1

Have a look at the Adaptable documentation hope it answer your question

it is explicitly left as an implementation detail whether each call to this method with the same type yields the same object or a new object on each call.

AdaptTo

similar question purpose-of-resourceresolver-adapttosession

Just to explain in simple

  1. The ResourceResolver is the service API which we can resolve Resource(Resources are pieces of content on which Sling act) objects.
  2. The resource resolver is available to the request processing servlet through the SlingHttpServletRequest.getResourceResolver() method. A resource resolver can also be created through the ResourceResolverFactory.
  3. A ResourceResolver is generally not thread safe! an application which uses the resolver, must provide proper synchronization to ensure no more than one thread concurrently operates against a single resolver, resource or resulting objects.
  4. The ResourceResolver is also an Adaptable to get adapters to other types.

So when you get the resource by ResourceResolver and adaptTo other types or some representations of the object, the object will be same. Remember that the operations which you are performing on a resource (after adaptTO()) should be taken care as the resource is generally not thread safe. Example the resolver is updated to reflect the latest state by using refresh() method, etc.

Community
  • 1
  • 1
VAr
  • 2,551
  • 1
  • 27
  • 40
  • Thank you for pointing to that link. However, I am not able to find the exact implementation for the PageManager class. I tried looking into the PageManagerFactory implementation but couldn't find a concrete class that could confirm what will be returned. – Ruchi Feb 03 '17 at 04:28
  • @Ruchi Are you looking for adaptoTo(Pagemanager.class) example? – VAr Feb 03 '17 at 04:31
  • the concrete implementation in the API that can confirm if multiple objects will be created or same object will be returned. – Ruchi Feb 03 '17 at 04:45
  • @Ruchi you are on right track. Just updated my answer to provide some more information – VAr Feb 03 '17 at 06:28
0

Ok.

I couldn't find the concrete implementation in the API but I wrote this sample code to test:

    ResourceResolver res = request.getResourceResolver();
    PageManager temp = res.adaptTo(PageManager.class);
    for (int i=0; i < 100; i++) {
        PageManager mgr = res.adaptTo(PageManager.class);

        out.println("For iteration: " + i + " The object is: " + mgr.hashCode());
        if (temp == mgr) {
            out.println("For iteration: " + i + " the objects are equal");
        }
        temp = mgr;
    }

I got the same hashcode eachtime and also the two references returned true on comparison with ==; hence proving they were pointing to the same object.

Ruchi
  • 35
  • 1
  • 7