Caching

To improve performance, implementations are free to cache the object returned from a obj.adaptTo() call. If the obj is the same, the returned object is the same.

This caching is performed for all AdapterFactory based cases.

However, there is no general rule - the object could be either a new instance or an existing one. This means that you cannot rely on either behavior. Hence it is important, especially inside AdapterFactory, that objects are reusable in this scenario.

How it works

There are various ways that Adaptable.adaptTo() can be implemented:

  • By the object itself; implementing the method itself and mapping to certain objects.

  • By an AdapterFactory, which can map arbitrary objects.

    The objects must still implement the Adaptable interface and must extend SlingAdaptable (which passes the adaptTo call to a central adapter manager).

    This allows hooks into the adaptTo mechanism for existing classes, such as Resource.

  • A combination of both.

For the first case, the Java™ docs can state what adaptTo-targets are possible. However, for specific subclasses such as the JCR-based Resource, often this is not possible. In the latter case, implementations of AdapterFactory are typically part of the private classes of a bundle and thus not exposed in a client API, nor listed in Java™ docs. Theoretically, it would be possible to access all AdapterFactory implementations from the OSGi service runtime and look at their “adaptables” (sources and targets) configurations, but not to map them to each other. In the end, this depends on the internal logic, which must be documented. Hence this reference.

Reference

Sling

Resource adapts to:

NodeIf this is a JCR-node-based resource or a JCR property referencing a node.
PropertyIf this is a JCR-property-based resource
ItemIf this is a JCR-based resource (node or property)
MapReturns a map of the properties, if this is a JCR-node-based resource (or other resource supporting value maps)
ValueMapReturns a convenient-to-use map of the properties, if this is a JCR-node-based resource (or other resource supporting value maps). Can also be achieved (more simply) by using
ResourceUtil.getValueMap(Resource) (handles null case, and so on)
InheritanceValueMapExtension of ValueMap which allows the hierarchy of resources to be taken into account when looking for properties
ModifiableValueMapAn extension of the ValueMap, which lets you modify properties on that node
InputStreamReturns the binary content of a file resource (if this is a JCR-node-based resource and the node type is nt:file or nt:resource; if this is a bundle resource; file content if this is a file system resource) or the data of a binary JCR property resource
URLReturns a URL to the resource (repository URL of this node if this is a JCR-node-based resource; jar bundle URL if this is a bundle resource; file URL if this is a file system resource)
FileIf this is a file system resource
SlingScriptIf this resource is a script (for example, jsp file) for which a script engine is registered with sling
ServletIf this resource is a script (for example, jsp file) for which a script engine is registered with sling or if this is a servlet resource.
String
Boolean
Long
Double
Calendar
Value
String[]
Boolean[]
Long[]
Calendar[]
Value[]
Returns the values if this is a JCR-property-based resource (and the value fits).
LabeledResourceIf this is a JCR-node-based resource
PageIf this is a JCR-node-based resource and the node is a cq:Page (or cq:PseudoPage)
ComponentIf this is a cq:Component node resource
DesignIf this is a design node (cq:Page)
TemplateIf this is a cq:Template node resource
BlueprintIf this is a cq:Template node resource
AssetIf this is a dam:Asset node resource
RenditionIf this is a dam:Asset rendition (nt:file under the rendition folder of a dam:Assert)
TagIf this is a cq:Tag node resource
UserManagerBased on the JCR session if this is a JCR-based resource and the user has permissions to access the UserManager
AuthorizableThe Authorizable is the common base interface for User and Group
UserUser is a special Authorizable that can be authenticated and impersonated
SimpleSearchSearches below the resource (or use setSearchIn()) if this is a JCR-based resource
WorkflowStatusWorkflow status for the given page/workflow payload node
ReplicationStatusReplication status for the given resource or its jcr:content subnode (checked first)
ConnectorResourceReturns an adapted connector resource for certain types, if this is a JCR-node-based resource
ConfigIf this is a cq:ContentSyncConfig node resource
ConfigEntryIf this is below a cq:ContentSyncConfig node resource

ResourceResolver adapts to:

SessionThe request's JCR session, if this is a JCR-based resource resolver (default)
PageManager
ComponentManager
Designer
AssetManagerBased on the JCR session, if this is a JCR-based resource resolver
TagManagerBased on the JCR session, if this is a JCR-based resource resolver
UserManagerThe UserManager provides access to and means to maintain authorizable objects that is, users and groups. The UserManager is bound to a particular Session
AuthorizableThe current user
UserThe current user
QueryBuilder
ExternalizerFor externalizing absolute URLs, even without the request object

SlingHttpServletRequest adapts to:

No targets yet, but implements Adaptable and could be used as source in a custom AdapterFactory.

SlingHttpServletResponse adapts to:

ContentHandler
(XML)
If this is a sling rewriter response

WCM

Page adapts to:

ResourceResource of the page
LabeledResourceLabeled resource (== this)
NodeNode of the page
...Everything that the page's resource can be adapted to

Component adapts to:

ResourceResource of the component.
LabeledResourceLabeled resource (== this).
NodeNode of the component.
Everything that the component’s resource can be adapted to.

Template adapts to:

ResourceResource of the template
LabeledResourceLabeled resource (== this)
NodeNode of this template
...Everything that the template's resource can be adapted to.

Security

Authorizable, **User, and Group adapt to:

NodeReturns the user/group home node.
ReplicationStatusReturns the replication status for the user/group home node.

DAM

Asset adapts to:

ResourceResource of the asset.
NodeNode of the asset.
Everything that the asset’s resource can be adapted to.

Tagging

Tag adapts to:

ResourceResource of the tag.
NodeNode of the tag.
Everything that the tag’s resource can be adapted to.

Other

Furthermore Sling / JCR / OCM also provides an [AdapterFactory](https://sling.apache.org/site/adapters.html#Adapters-AdapterFactory) for custom OCM (Object Content Mapping) objects.

Experience Manager


Elevate and Empower Teams with Agentic AI for Exceptional Experiences

Online | Strategy Keynote | General Audience

Elevate and empower your CX teams with AI that transforms creativity, personalization, and productivity. Discover how Adobe is...

Tue, Mar 18, 1:00 PM PDT (8:00 PM UTC)

Register

How EY Enhanced Regulatory Knowledge Management with Adobe Solutions

Online | Session | General Audience

Delve into how EY leverages Adobe solutions to enhance regulatory knowledge management. EY leverages search analytics and personalization...

Wed, Mar 19, 4:00 PM PDT (11:00 PM UTC)

Register

Connect with Experience League at Summit!

Get front-row access to top sessions, hands-on activities, and networking—wherever you are!

Learn more