You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "David Herges (JIRA)" <ji...@apache.org> on 2015/08/05 14:20:04 UTC

[jira] [Created] (SLING-4926) Sling Models: inject a map of adapatbale child resources

David Herges created SLING-4926:
-----------------------------------

             Summary: Sling Models: inject a map of adapatbale 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


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)