[ 
https://issues.apache.org/jira/browse/SLING-4926?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14659956#comment-14659956
 ] 

David Herges commented on SLING-4926:
-------------------------------------

Maybe this could be an addition to {{ChildResourceInjector}} ?

> Sling Models: inject a map of adaptable child resources
> -------------------------------------------------------
>
>                 Key: SLING-4926
>                 URL: https://issues.apache.org/jira/browse/SLING-4926
>             Project: Sling
>          Issue Type: Improvement
>    Affects Versions: Sling Models API 1.1.0
>            Reporter: David Herges
>              Labels: injection, model, models
>
> Iterates over child resources and injects a map with entries 
> <childResourceName, childResource.adaptTo(Target.class)>
> Usage:
> {code}
> @Model(adptable=Resource.class)
> public class MyModel {
>   @Inject @Source("adaptable-child-resources-map")
>   protected  Map<String, MyAdaptable> childResources;
> }
> {code}
> Quick write up:
> {code}
> /**
>  * AdaptableChildResourcesMapInjector ... injects child resources into a 
> map.put(resource.name(), resource.adaptTo(type))
>  *
>  * <h2>Usage</h2>
>  * <code>protected Map<String, MyAdaptable> injectableMemberField;</code>
>  */
> @Component
> @Service
> @Property(name = Constants.SERVICE_RANKING, intValue = 9999)
> public class AdaptableChildResourcesMapInjector implements Injector{
>     @Override
>     public String getName() {
>         return "adaptable-child-resources-map";
>     }
>     @Override
>     public Object getValue(Object adaptable, String name, Type declaredType, 
> AnnotatedElement annotatedElement,
>                         DisposalCallbackRegistry disposalCallbackRegistry) {
>         Resource res = resourceFromAdaptable(adaptable);
>         Class<?> targetType = declaredMapWithValueType(declaredType);
>         if ((res != null) && (targetType != null)) {
>             Map<String, Object> result = new HashMap<String, Object>();
>             Iterator<Resource> iter = res.listChildren();
>             while ((iter != null) && iter.hasNext()) {
>                 Resource next = iter.next();
>                 result.put(next.getName(), next.adaptTo(targetType));
>             }
>             return result;
>         }
>         return null;
>     }
>     private Resource resourceFromAdaptable(Object adaptable) {
>         if (adaptable instanceof Resource) {
>             return (Resource) adaptable;
>         } else if (adaptable instanceof Adaptable) {
>             return ((Adaptable) adaptable).adaptTo(Resource.class);
>         }
>         return null;
>     }
>     // Checks if <code>declaredType</code> is a Map<String, {declaredType}> 
> ... returns {declaredType} or 'null'
>     protected Class<?> declaredMapWithValueType(Type declaredType) {
>         if (declaredType instanceof ParameterizedType) {
>             ParameterizedType type = (ParameterizedType) declaredType;
>             Class<?> rawType = (Class<?>) type.getRawType();
>             Type[] typeArguments = type.getActualTypeArguments();
>             if (rawType.equals(Map.class) && (typeArguments.length == 2)
>                     && typeArguments[0].equals(String.class)) {
>                 // returns the V from Map<K, V>
>                 return (Class<?>) typeArguments[1];
>             }
>         }
>         return null;
>     }
> }
> {code}
> wdyt?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to