Expand my Community achievements bar.

SOLVED

AdaptTo in aem

Avatar

Level 1

Hi All,

 

we can adapt resource to page and resourceresolver to pagemanager but  we can't adapt resource to pagemanger and resolver to page  how does adapter know this?

Thanks,

Keerthi K.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Kummk123,

This depends on the implementation of specific object and/or AdapterFactory correlated with it. Official documentation describes is like below:

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 method 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, this statement is often 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 is 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, it depends on the internal logic, which must be documented. Hence this reference.


In general if you will try to adapt to a class that for which adapter is not supported you will get a null as a result.

Please find below documentation that describes many details:

Additionally please also check list of available adapter, so you will know what type can be adapt to which object.

Last but not lease you can check full list of adapters on your AEM instance under: /system/console/adapters or /system/console/status-adapters

This might be useful in case to find some custom adapter provided by 3rd party.

Finally you can implement your own adapter in case OOTB are not covering your needs.

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @Kummk123,

This depends on the implementation of specific object and/or AdapterFactory correlated with it. Official documentation describes is like below:

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 method 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, this statement is often 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 is 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, it depends on the internal logic, which must be documented. Hence this reference.


In general if you will try to adapt to a class that for which adapter is not supported you will get a null as a result.

Please find below documentation that describes many details:

Additionally please also check list of available adapter, so you will know what type can be adapt to which object.

Last but not lease you can check full list of adapters on your AEM instance under: /system/console/adapters or /system/console/status-adapters

This might be useful in case to find some custom adapter provided by 3rd party.

Finally you can implement your own adapter in case OOTB are not covering your needs.