Adapters Within Apache Sling

[vc_row][vc_column width=”2/3″][vc_column_text el_class=”documentation”]Apache Sling provides Sling Adapters. Sling Adapters is an API along with implementations of that API and conventions for using the API. Sling Adapters allows programmers a shortcut in the process of inferring one type of object from another.

Adapt, Adaptable, Adapter, Adapter Factory

Within Sling Adapters, when an object is inferred from another object it is adapted from the original object. The object that will be adapted is called the adaptable. The result of the adapting is the adapter. The services that can be used adapt to adaptable objects to adapter objects are adapter factories.

What Happens When an Object is Adapted

Java defines objects using classes and interfaces. Classes and interfaces define both properties and methods that those objects have. Classes can be logically related to other classes. Properties and methods can be used to directly return instances of other classes. Also, the information from a class’s property and methods can be used to infer objects defined by using other classes.

A very seemingly simple example of inferring one class from another is found in the Object class’s toString() method. All Java classes except for primitive values extend the Object class. Each class that extends Object can override the toString() method to make sure that the resulting String object represents the object it is called on.

The actual process of creating a String to represent an instance of a class has a series of steps. For example, a Long object with the value of 10 would return “10” as a String. The Long object is very different than the String object, but the String value, “10”, can be inferred from the long primitive of 10. Returning a String representation of a Long value is not done in a single step. Numbers are not inherently base 10. So the value of each position has to be calculated for a base 10 number. The character has to be chosen to represent the value of each position. Each character has to be appended to a byte array in the order that makes it human readable. The byte array itself would have to be converted to a String object.

Sling Adapters provides a way for services to be created that contain the logic to infer one object from another. The services contain this logic and provide it through OSGi. Sling Adapters also provides conventions and interfaces to be used with classes to make objects of those classes contain the logic themselves to infer related objects.

It should be noted that an adapter, despite its name, does not do the adapting. It is the product of the process.

How the Adapter Design Pattern Differs

The Sling Adapters appears to implement the Adapter design pattern. The two are very similar, especially in their terminology and goals. The one important difference is that the objects that are products of the Adapter design pattern should contain the original object. In the Adapter design pattern, objects are contained within wrapper classes to enable the original object to implement different interfaces. In contrast, when one object is adapted to another object with Sling Adapters, the result does not necessarily contain the original object. An adapter can can be a wrapper for a corresponding adaptable. It does not have to be.

Customization of Sling Adapters

The Sling Adapters is not an add-on to Apache Sling. It is one of the core APIs of this environment. The fundamental Java interfaces for the Sling Adapters are found in the Apache Sling API Bundle. The Apache Sling Adapter Manager Implementation Bundle does exactly what its name states, it implements the default version AdapterManager service. The API and the AdapterManager service should not be customized or extended. Functionality is added to Sling Adapters using services implementing the AdapterFactory interface and objects implementing the Adaptable interface.

See Also

Adapters documentation  Wikipedia: Adapter pattern[/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: How to Get Things Done

[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]

Next: Adapters: Apache Sling API Bundle

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

No Comments on "Adapters Within Apache Sling"

Leave a Comment

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