Search in sources :

Example 1 with UseProvider

use of org.apache.sling.scripting.sightly.use.UseProvider in project sling by apache.

the class UseRuntimeExtension method call.

@Override
public Object call(final RenderContext renderContext, Object... arguments) {
    ExtensionUtils.checkArgumentCount(RuntimeFunction.USE, arguments, 2);
    RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
    String identifier = runtimeObjectModel.toString(arguments[0]);
    if (StringUtils.isEmpty(identifier)) {
        throw new SightlyException("data-sly-use needs to be passed an identifier");
    }
    Map<String, Object> useArgumentsMap = runtimeObjectModel.toMap(arguments[1]);
    Bindings useArguments = new SimpleBindings(Collections.unmodifiableMap(useArgumentsMap));
    ArrayList<UseProvider> providers = new ArrayList<>(providersMap.values());
    ListIterator<UseProvider> iterator = providers.listIterator(providers.size());
    while (iterator.hasPrevious()) {
        UseProvider provider = iterator.previous();
        ProviderOutcome outcome = provider.provide(identifier, renderContext, useArguments);
        Throwable failureCause;
        if (outcome.isSuccess()) {
            return outcome.getResult();
        } else if ((failureCause = outcome.getCause()) != null) {
            throw new SightlyException("Identifier " + identifier + " cannot be correctly instantiated by the Use API", failureCause);
        }
    }
    throw new SightlyException("No use provider could resolve identifier " + identifier);
}
Also used : RuntimeObjectModel(org.apache.sling.scripting.sightly.render.RuntimeObjectModel) ArrayList(java.util.ArrayList) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ProviderOutcome(org.apache.sling.scripting.sightly.use.ProviderOutcome) UseProvider(org.apache.sling.scripting.sightly.use.UseProvider) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings)

Aggregations

ArrayList (java.util.ArrayList)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 SightlyException (org.apache.sling.scripting.sightly.SightlyException)1 RuntimeObjectModel (org.apache.sling.scripting.sightly.render.RuntimeObjectModel)1 ProviderOutcome (org.apache.sling.scripting.sightly.use.ProviderOutcome)1 UseProvider (org.apache.sling.scripting.sightly.use.UseProvider)1