Sling Adapter Annotations

[vc_row][vc_column width=”2/3″][vc_column_text el_class=”documentation”]Sling Adapter annotations may be added to the Java class definitions that implement the org.apache.sling.api.adapter.Adaptable interface, including classes that extend org.apache.sling.api.adapter.SlingAdaptable. The Maven Sling Plugin processes the annotations and writes the metadata to OSGi bundles. This metadata contains a list of Adaptable implementations linked with their corresponding adapters and a human-readable description of the conditions that the adapters can be created. The annotations have no functionality at runtime.

The AdapterWebConsolePlugin bundle tracks all of the metadata from the Sling Adapter annotations as well as the adaptables, adapters, and conditions found in registered org.apache.sling.api.adapter.AdapterFactory services.

Use of Sling Adapter annotations is not mandatory. It increases the usability of code by providing helpful reference information.

Use Within Maven

The Sling Adapter annotations are a library, not a bundle. To be able to use the annotations, add the following dependency to the Maven POM. Replace {VERSION} with the version of the library to use. The current version is listed on the Apache Sling Downloads -http://sling.apache.org/downloads.cgi page. The Maven Sling Plugin-http://sling.apache.org/site/sling.html must be included in the POM

<dependency>
     <artifactId>adapter-annotations</artifactId>
     <artifactId>org.apache.sling.api</artifactId>
     <version>{VERSION}</version>
</dependency>

NOTE: The annotations are only used if Maven is used to create the project with the Maven Sling Plugin.

A bundle with annotated adapters will have a file created in the bundle jar at /SLING-INF/adapters.json. An example can be see in the FsResource bundle.

{"org.apache.sling.api.resource.Resource": {
    "If the adaptable is a FsResource.": [
        "java.io.File",
        "java.net.URL"
    ],
    "If the resource is an FsResource and is a readable file.": "java.io.InputStream"
}}

Annotations


@Adaptable

The @Adaptable annotation defines metadata about an implementation of Adaptable. It only defines a single adaptable Java class or interface and can have one or more adapter properties. If multiple adaptable values are needed, use the @Adaptables annotation instead. @Adaptable is applied to the Java class definition or is part of the list of @Adaptables annotation’s value property.

Properties

adaptableClass: Class to use for the adaptable. By default, it is the class that the @Adaptable annotation is applied to

adapters: Array of Java classes or interfaces the adaptable can be adapted to

Usage
@Adaptable(adaptableClass = Resource.class, 
           adapters = @Adapter(value = { String.class },
           "Adapts Resources to String if the path is not empty."))
public class MyAdaptable extends Resource {
    @Override
    @SuppressWarnings("unchecked")
    public  Type adaptTo(Class type) {
        if (type == String.class && type.toString().length > 0) {
            return (Type) this.getPath();
        }
        return super.adaptTo(type);
    }
}

@Adaptables

The @Adaptables annotation contains a group of @Adapter annotations. The @Adaptables annotation can be applied to Java class definitions that implement the Adaptable interface. The @Adaptables annotation has a value property. The value property is an Array of two or more @Adaptable annotations. If only one @Adaptable annotation is applied to the class, the @Adaptables annotation is not needed.

Properties

value: Array of @Adaptable annotations

Usage
@Adaptables(value = {
         @Adaptable(adaptableClass = Resource.class, 
                    adapters = @Adapter(value = { String.class },
                    "Adapts Resources to String if the path 
                    is not empty.")),
         @Adaptable(adaptableClass = MyAdaptable.class, 
                    adapters = @Adapter(value = { String.class },
                    "Adapts MyAdaptable to String if 
                     the path is not empty."))                    
})
public class MyAdaptable extends Resource {
    @Override
    @SuppressWarnings("unchecked")
    public  Type adaptTo(Class type) {
        if (type == String.class && type.toString().length > 0) {
            return (Type) this.getPath();
        } 
        return super.adaptTo(type);
    }
}

@Adapter

The @Adapter annotation specifies the target Java class or interface that can be returned. The adaptables property of the @Adaptable annotation is an Array of @Adapter annotations.

Properties

value: The Class that can used as a return value

condition: Description of how what conditions must be met for the adapter value to be returned

Usage
@Adaptable(adaptableClass = Resource.class, 
           adapters = @Adapter(value = { String.class },
           "Adapts Resources to String if the path is not empty."))
public class MyAdaptable extends Resource {
    @Override
    @SuppressWarnings("unchecked")
    public  Type adaptTo(Class type) {
        if (type == String.class && type.toString().length > 0) {
            return (Type) this.getPath();
        }
        return super.adaptTo(type);
    }
}

For More Information

How to Create Customer Adapters in AEM/CQ 

Maven Sling Plugin[/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: SlingAdaptable Abstract Class[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]

Next: Adapter Web Console

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

No Comments on "Sling Adapter Annotations"

Leave a Comment

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