Created
October 29, 2014 21:07
-
-
Save yupadhyay/7ff310bce4e6e731d6a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jcr.Node; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Property; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.sling.api.adapter.AdapterFactory; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* | |
* @author yogeshupadhyay | |
* | |
*/ | |
@Component(metatype=false) | |
@Service (value=AdapterFactory.class) | |
public class CustomClassAdapterFactory implements AdapterFactory { | |
private static final Logger log = LoggerFactory.getLogger(CustomClassAdapterFactory.class); | |
private static final Class<CustomClass> YOUR_CUSTOM_CLASS = CustomClass.class; | |
@Reference | |
ResourceResolverFactory resourceResolverFactory; | |
@Property(name="adapters") | |
public static final String[] ADAPTER_CLASSES = { | |
YOUR_CUSTOM_CLASS.getName() | |
}; | |
//This is where you define what all OOTB object it can adapt to You can also define this in //property of this component as mentioned in document above or //here http://labs.sixdimensions.com/blog/dklco/2012-05-07/new-cq-55-sling-adapters-console | |
@Property(name="adaptables") | |
public static final String[] ADAPTABLE_CLASSES = { | |
Resource.class.getName(), Node.class.getName() | |
}; | |
public <AdapterType> AdapterType getAdapter(Object adaptable,Class<AdapterType> type) { | |
if(adaptable instanceof Resource){ | |
return getAdapter((Resource) adaptable, type); | |
} | |
if(adaptable instanceof Node){ | |
return getAdapter((Node) adaptable, type); | |
} | |
return null; | |
} | |
/** | |
* Adapter for resource | |
* @param resource | |
* @param type | |
* @return | |
*/ | |
@SuppressWarnings("unchecked") | |
private <AdapterType> AdapterType getAdapter(Resource resource,Class<AdapterType> type) { | |
if(null == resource){ | |
return null; | |
} | |
try{ | |
if(type == YOUR_CUSTOM_CLASS){ | |
//Logic to adapt your resource to resource | |
return (AdapterType) new CustomAdapter().adaptResourceToCustomClass(resource); | |
} | |
}catch(Exception e){ | |
log.error("Unable to adapt resource {}", resource.getPath()); | |
} | |
log.error("Unable to adapt resource {}", resource.getPath()); | |
return null; | |
} | |
/** | |
* adapter for node | |
* @param node | |
* @param type | |
* @return | |
*/ | |
@SuppressWarnings("unchecked") | |
private <AdapterType> AdapterType getAdapter(Node node,Class<AdapterType> type) { | |
if(null == node){ | |
return null; | |
} | |
try{ | |
if(type == YOUR_CUSTOM_CLASS){ | |
return (AdapterType) new CustomAdapter().adaptNodeToCustomClass(node, this.resourceResolverFactory); | |
} | |
}catch(Exception e){ | |
log.error("Unable to adapt node to Your custom class"); | |
} | |
log.error("Unable to adapt node"); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment