8

I am new to Apache Sling, CQ5, etc.

In our codebase, we have a code snippet similar to this:

void perform(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    ResourceResolver resourceResolver = request.getResourceResolver();

    Session session = resourceResolver.adaptTo(Session.class);
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
}

What's adapTo doing here?

Also is there a good documentation/user manual available I can read to get started using Sling, CQ5, etc.?

TheFooProgrammer
  • 2,439
  • 5
  • 28
  • 43

1 Answers1

7

The adaptTo() method found in many sling objects allows to "transform" objects. Sling could have decided to add a resolver.getSession() method, but that wouldn't have been very flexible. The nice thing about adaptTo is that is it dynamic. you can create adapters to transform between different types (they are OSGi services). Sling and CQ5 also include bunch of adapters by default. The sling Wiki has some docs about adapters.

About how to start with Sling and CQ5, the sling site is a good place to start

santiagozky
  • 2,519
  • 22
  • 31
  • 1
    But the `ResourceResolver` looks more like a Service Locator. I mean, is it transforming itself to different types of classes, or is it finding the appropriate class and return it? – TheFooProgrammer Feb 13 '14 at 00:37
  • 1
    It is more of a factory. Also, it depends on the implementation of the particular adaption you are invoking. Normally it creates a new object taking what is necessary from the original one. I suppose resourceResolver.adaptTo(PageManager.class) basically does pageManagerFactory.getPageManager(resourceResolver) – santiagozky Feb 13 '14 at 09:02