AdapterManager Interface

[vc_row][vc_column width=”2/3″][vc_column_text el_class=”documentation”]The function of the Sling Adapters to provide a way to adapt a Java object implementing one interface to another interface that is related, but is not compatible. The org.apache.sling.api.adapter.AdapterManager service interface provides a way to simply access the available org.apache.sling.api.adapter.AdapterFactory services.

The default service that implements the AdapterManager interface coordinates requests to adapt objects with the AdapterFactory services. AdapterFactory services are dynamic and provide the actual transformations for adaptable objects. The AdapterManager binds and unbinds AdapterFactory services as they become available. There can be only one AdapterManager service, but there can be any number of services implementing the AdapterFactory interface.

Each AdapterFactory service associates itself with a set of original interfaces that can be input into the adapter and a set of target interfaces that can those original interfaces can be adapted to.

Requests to adapt an object are made to the AdapterManager’s single public method, getAdapter(). The name can be misleading. The result of this method is not an object that can perform the adapting of a Java object from one type to another, it is the resulting, target, object of the adapt process that is an alternate representation of the original object in a new interface or class.

When a request is made for an adaptation of an object of one interface to another object of a different interface, the AdapterManager reviews its internal list of AdapterFactory objects for AdapterFactory objects that have matching adaptable classes and target classes. The AdapterManager iterates through the list of matching AdapterFactory services and requests these services to process the adaptable object. If the current AdapterFactory service returns a non-null value, that value is returned and the rest of the AdapterFactory services are not used.

Customization of AdapterManager

The AdapterManager is key to core functionality of Apache Sling. Custom implementations of AdapterManager should not be used and the default AdapterManager service should never be extended.

Extensibility is provided through the OSGi services that implement AdapterFactory and, to a lesser extended, objects that implement the Adaptable interface. Adaptable/Adapter pairs can be added by the use of custom AdapterFactory services. Classes that implement the Adapter interface can change the adaptation behavior for only themselves through overriding the adaptTo() method.

Methods


getAdapter()

<AdapterType> AdapterType getAdapter(final Object adaptable, final Class<AdapterType> type)
Parameters:

adaptable: the original object to be adapted
type: the Class to provide as an adapter version

Returns:

An object that is an instance of the class specified in the type parameter. If not able to adapt to the requested type, null is returned.

Note: AdapterType is not an interface or class. It is a type parameter for the generic type, such as the conventional symbols, E, K, N, T, V.

Example

public Node getNode(Object adaptable, AdapterManager adapterManager) {
     Node result = adapterManger.getAdapter(adaptable, Node.class);
     if (result == null) {
          log.debug("Not able to adapt object to Node");
     }
     return result; // Returns null if object cannot be adapted
}

See Also

Adaptable

AdapterFactory

AdapterManager JavaDoc

Generic Types

GitHub: Package: org.apache.sling.api.adapter

GitHub: Class: org.apache.sling.adapter.internal.AdapterManagerImpl.java[/vc_column_text][/vc_column][vc_column width=”1/3″][vc_widget_sidebar sidebar_id=”woocommerce”][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]Previous: Adaptable Interface[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]

Next: AdapterFactory Interface

[/vc_column_text][/vc_column][/vc_row]

No Comments on "AdapterManager Interface"

Leave a Comment

Your email address will not be published. Required fields are marked *