AdapterFactory Interface

[vc_row][vc_column width=”2/3″][vc_column_text el_class=”documentation”]The org.apache.sling.api.adapter.AdapterFactory interface defines a service that provides a global extension of the Adapter API. Each AdapterFactory service defines the interfaces it will accept as adaptable objects and the interfaces it will return as an adapter. The AdapterFactory is NOT required to adapt all adaptable arguments to itsĀ getAdapter() method. Each AdapterFactory instance may evaluated the adaptable received to confirm that it can be adapted to the target interface and return null if it can’t.

An example of conditional processing of adapter classes would be the adapting of org.apache.sling.api.resource.Resource objects to instances object implementing the org.apache.jackrabbit.api.security.user.Group interface. The Resource will only be adapted to an object implementing Group if the resource is an AuthorizableResource and represents the values for a JCR Group.

OSGi Service Properties
adaptables A String Array of the class names of adaptable classes and interfaces
adapters A String Array of the class names of target, or adapter, classes and interfaces
adapter.condition Human readable description of conditions applied when evaluating an adaptable. If the conditions are not met, the AdapterFactory object’s getAdapter method returns null. NOTE: The conditions must be enforced in the implementation code. Unlike the adaptables and adapters properties, this property is not evaluated by the AdapterManager. It is for informational purposes only.

Constants


SERVICE_NAME (String)

Value: “org.apache.sling.api.adapter.AdapterFactory”


ADAPTABLE_CLASSES (String)

Value: “adaptables”


ADAPTER_CLASSES (String)

Value: “adapters”

Methods


getAdapter()

AdapterType getAdapter(Object adaptable, Class 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 class MyAdapterFactory implements AdapterFactory {

	public MyAdapterFactory(final BundleContext bundleContext) {
                Dictionary<String, Object> props = new Hashtable<String, Object>();
	    props.put(Constants.SERVICE_DESCRIPTION, "My Adapter");
	    props.put(AdapterFactory.ADAPTER_CLASSES, 
	          new String[]{RepositoryPath.getClass().getName() });
	    props.put(AdapterFactory.ADAPTABLE_CLASSES, 
	          new String[] {Resource.getClass().getName() });
	    this.service = bundleContext.registerService(
                                    AdapterFactory.SERVICE_NAME, 
	                            this, props);
	} 

	public  AdapterType getAdapter(Object adaptable, Class type) {
		if (adaptable instanceof Resource) {
			return new RepositoryPath(resource);
		}		
		return null;	
	}

	public class RepositoryPath {

		private String value;

		public RepositoryPath(Resource resource) {
			this.value = resource.getPath();
		}

		public String getValue() {
			return this.value;
		}		

	}

}

The same service can be defined using Apache Felix SCR notation:

@Component(metatype=false, immediate=true)
@Service({AdapterFactory.class})
@Properties({
    @Property(name="service.vendor", value="Terra Beata"),
    @Property(name="service.description", value="Repository path adapter"),
    @Property(name="adaptables", value="org.apache.sling.api.resource.Resource"),
    @Property(name="adapters", value={RepositoryPath.getClass()}),
    @Property(name="adapter.condition", value="If the resource's path is not null.")
})
public class MyAdapterFactory implements AdapterFactory {

	public  AdapterType getAdapter(Object adaptable,
			Class type) {
		if (adaptable instanceof Resource) {
			return new RepositoryPath(resource);
		}		
		return null;	
	}

	public class RepositoryPath {

		private String value;

		public RepositoryPath(Resource resource) {
			this.value = resource.getPath();
		}

		public String getValue() {
			return this.value;
		}		

	}

}

See Also

Adaptable

AdapterManager

AdapterFactory JavaDoc

GitHub: Package org.apache.sling.api.adapter trunk branch[/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: AdapterManager Interface[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]

Next: SlingAdaptable Abstract Class

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

No Comments on "AdapterFactory Interface"

Leave a Comment

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