Adapters has been edited by Mike Mueller (Jul 01, 2009).

(View changes)

Content:

Adapters

The Resource and ResourceResolver interfaces are defined with a method adaptTo, which adapts the object to other classes. Using this mechanism the JCR session of the resource resolver calling the adaptTo method with the javax.jcr.Session class object. Likewise the JCR node on which a resource is based can be retrieved by calling the Resource.adaptTo method with the javax.jcr.Node class object.

To use resources as scripts, the Resource.adaptTo method must support being called with the org.apache.sling.api.script.SlingScript class object. But of course, we do not want to integrate the script manager with the resource resolver. To enable adapting objects to classes which are not foreseen by the original implementation, a factory mechanism is used. This way, the script manager can provide an adapter factory to adapt Resource to SlingScript objects.

Adaptable

The Adaptable interface defines the API to be implemented by a class providing adaptability to another class. The single method defined by this interface is

/**
 * Adapts the adaptable to another type.
 *
 * @param <AdapterType> The generic type to which this resource is adapted
 *            to
 * @param type The Class object of the target type, such as
 *            <code>Node.class</code>
 * @return The adapter target or <code>null</code> if the resource cannot
 *         adapt to the requested type
 */
<AdapterType> AdapterType adaptTo(Class<AdapterType> type);

This method is called to get a view of the same object in terms of another class. Examples of implementations of this method is the Sling ResourceResolver implementation providing adapting to a JCR session and the Sling JCR based Resource implementation providing adapting to a JCR node.


But to be clear: Any object can be adapted even if it does NOT implement the Adaptable }}interface, if an {{AdapterFactory service delivers an }}{{{}getAdapter() method which adapts an object to another one. To check if there's any existing }}{{{}AdapterFactory which can adapt a given object to another one }}the {{AdapterManager service with it's getAdapter() method does the job. So the Adaptable interface merely is an indicator that the object provides built-in support for beeing adapted.{{}}

SlingAdaptable

The SlingAdaptable class is an implementation of the Adaptable interface, calls the AdapterManager (see below) to provide an adapter to the SlingAdaptable object to the requested class. This class may be extended to have extensible adapters not foreseen at the time of the class development.

An example of extending the SlingAdaptable class will be the Sling JCR based Resource implementation. This way, such a resource may be adapted to a SlingScript by means of an appropriatley programmed AdapterFactory (see below).

AdapterFactory

The AdapterFactory interface defines the service interface and API for factories supporting extensible adapters for SlingAdaptable objects. The interface has a single method:

/**
 * Adapt the given adaptble object to the adaptable type. The adaptable
 * object is guaranteed to be an instance of one of the classes listed in
 * the {...@link #ADAPTABLE_CLASSES} services registration property. The type
 * parameter is on of the classes listed in the {...@link #ADAPTER_CLASSES}
 * service registration properties.
 *
 * @param <AdapterType>
 * @param adaptable
 * @param type
 * @return
 */
<AdapterType> AdapterType getAdapter(Object adaptable,
        Class<AdapterType> type);

This method is called by the AdapterManager on behalf of the SlingAdaptable object providing the SlingAdaptable as the adaptable parameter the requested class as the type parameter. Implementations of this interface are registered as OSGi services providing two lists: The list of classes wich may be adapted (property named adaptables) and the list of classes to which the adapted class may be adapted (property named adapters). A good example of an Class implementing AdapterFactory is the SlingScriptAdapterFactory.

AdapterManager

The AdapterManager is an internal class used by the SlingAdaptable objects to find an AdapterFactory to delegate the adaptTo method call to. To make the AdapterManager available globally, it is actually defined as a service interface. Thus the adapter manager may be retrieved from the service registry to try to adapt whatever object that needs to be adapted - provided appropriate adapters exist.

The AdapterManager interface is defined as follows:

public interface AdapterManager {

    /**
     * Returns an adapter object of the requested <code>AdapterType</code> for
     * the given <code>adaptable</code> object.
     * <p>
     * The <code>adaptable</code> object may be any non-<code>null</code>
     * object and is not required to implement the <code>Adaptable</code>
     * interface.
     *
     * @param <AdapterType> The generic type of the adapter (target) type.
     * @param adaptable The object to adapt to the adapter type.
     * @param type The type to which the object is to be adapted.
     * @return The adapted object or <code>null</code> if no factory exists to
     *         adapt the <code>adaptable</code> to the
     *         <code>AdapterType</code> or if the <code>adaptable</code>
     *         cannot be adapted for any other reason.
     */
    <AdapterType> AdapterType getAdapter(Object adaptable,
            Class<AdapterType> type);

}

Reply via email to