Search in sources :

Example 1 with AsyncContainer

use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.

the class JsEnvironment method runResource.

public AsyncContainer runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments) {
    AsyncContainer asyncContainer = new AsyncContainer();
    runResource(scriptResource, globalBindings, arguments, asyncContainer.createCompletionCallback());
    return asyncContainer;
}
Also used : AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer)

Example 2 with AsyncContainer

use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.

the class JsValueAdapter method forceAsync.

private Object forceAsync(Object jsValue) {
    AsyncContainer asyncContainer = new AsyncContainer();
    asyncExtractor.extract(jsValue, asyncContainer.createCompletionCallback());
    return asyncContainer.getResult();
}
Also used : AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer)

Example 3 with AsyncContainer

use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.

the class JsUseProvider method provide.

@Override
public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings arguments) {
    Bindings globalBindings = renderContext.getBindings();
    if (!Utils.isJsScript(identifier)) {
        return ProviderOutcome.failure();
    }
    ScriptEngine jsEngine = scriptEngineManager.getEngineByName(JS_ENGINE_NAME);
    if (jsEngine == null) {
        return ProviderOutcome.failure(new SightlyException("No JavaScript engine was defined."));
    }
    SlingScriptHelper scriptHelper = Utils.getHelper(globalBindings);
    JsEnvironment environment = null;
    try {
        environment = new JsEnvironment(jsEngine);
        environment.initialize();
        ResourceResolver slingScriptingResolver = scriptingResourceResolverProvider.getRequestScopedResourceResolver();
        Resource callerScript = slingScriptingResolver.getResource(scriptHelper.getScript().getScriptResource().getPath());
        Resource scriptResource = Utils.getScriptResource(callerScript, identifier, globalBindings);
        globalBindings.put(ScriptEngine.FILENAME, scriptResource.getPath());
        proxyAsyncScriptableFactory.registerProxies(globalBindings);
        AsyncContainer asyncContainer = environment.runResource(scriptResource, globalBindings, arguments);
        return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
    } finally {
        if (environment != null) {
            environment.cleanup();
        }
    }
}
Also used : SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 4 with AsyncContainer

use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.

the class SlyBindingsValuesProvider method loadFactory.

private Function loadFactory(ResourceResolver resolver, JsEnvironment jsEnvironment, String path, Bindings bindings) {
    Resource resource = resolver.getResource(path);
    if (resource == null) {
        throw new SightlyException("Sly namespace loader could not find the following script: " + path);
    }
    AsyncContainer container = jsEnvironment.runResource(resource, createBindings(bindings), new SimpleBindings());
    Object obj = container.getResult();
    if (!(obj instanceof Function)) {
        throw new SightlyException("Script " + path + " was expected to return a function.");
    }
    return (Function) obj;
}
Also used : Function(org.mozilla.javascript.Function) TimingFunction(org.apache.sling.scripting.sightly.js.impl.async.TimingFunction) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer) Resource(org.apache.sling.api.resource.Resource) HybridObject(org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 5 with AsyncContainer

use of org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer in project sling by apache.

the class UseFunction method use.

private Object use(List<String> depNames, final Function callback, final Context cx, final Scriptable scope) {
    final AsyncContainer asyncContainer = new AsyncContainer();
    if (depNames.isEmpty()) {
        callImmediate(callback, asyncContainer, cx, scope);
    } else {
        final int[] counter = { depNames.size() };
        final Object[] dependencies = new Object[depNames.size()];
        for (int i = 0; i < depNames.size(); i++) {
            final int dependencyPos = i;
            dependencyResolver.resolve(depNames.get(i), new UnaryCallback() {

                @Override
                public void invoke(Object arg) {
                    counter[0]--;
                    dependencies[dependencyPos] = arg;
                    if (counter[0] == 0) {
                        Object result = JsUtils.callFn(callback, cx, scope, thisObj, dependencies);
                        asyncContainer.complete(result);
                    }
                }
            });
        }
    }
    return asyncContainer;
}
Also used : UnaryCallback(org.apache.sling.scripting.sightly.js.impl.async.UnaryCallback) AsyncContainer(org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

AsyncContainer (org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer)5 Resource (org.apache.sling.api.resource.Resource)2 SightlyException (org.apache.sling.scripting.sightly.SightlyException)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 Bindings (javax.script.Bindings)1 ScriptEngine (javax.script.ScriptEngine)1 SimpleBindings (javax.script.SimpleBindings)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)1 TimingFunction (org.apache.sling.scripting.sightly.js.impl.async.TimingFunction)1 UnaryCallback (org.apache.sling.scripting.sightly.js.impl.async.UnaryCallback)1 HybridObject (org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject)1 Function (org.mozilla.javascript.Function)1 NativeObject (org.mozilla.javascript.NativeObject)1