Search in sources :

Example 1 with AsyncChannel

use of com.disney.groovity.util.AsyncChannel in project groovity by disney.

the class Accept method tag.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object tag(Map attributes, Closure body) throws Exception {
    // channel may be null, which will create an anonymous channel that can only be offered to via reference
    Object channel = resolve(attributes, "channel");
    Policy policy = Policy.block;
    String policyStr = resolve(attributes, "policy", String.class);
    if (policyStr != null) {
        policy = Policy.valueOf(policyStr);
    }
    int qSize = -1;
    Object qDef = resolve(attributes, "q");
    if (qDef != null) {
        if (qDef instanceof Number) {
            qSize = ((Number) qDef).intValue();
        } else {
            qSize = Integer.valueOf(qDef.toString());
        }
    }
    Closure completed = resolve(attributes, "completed", Closure.class);
    final ScriptHelper scriptHelper = getScriptHelper(body);
    final Binding binding = scriptHelper.getBinding();
    final Map variables = binding.getVariables();
    final Map asyncVariables = Async.asyncCopy(variables);
    final AwaitContext asyncContext = AwaitContext.get(variables);
    final CharArrayWriter out = new CharArrayWriter();
    asyncVariables.put(OUT, out);
    if (asyncContext != null) {
        // signal a boundary for sequencing async output
        asyncContext.signalAsync(variables, out);
    }
    Binding asyncBinding = new Binding(asyncVariables);
    Object owner;
    if (body.getThisObject() instanceof Class) {
        // if the parent script is not available it is a special case (static) and we may share the context
        owner = body.getOwner();
    } else {
        // in normal contexts create a safe copy of the script
        Binding oldThreadBinding = ScriptHelper.THREAD_BINDING.get();
        ScriptHelper.THREAD_BINDING.set(asyncBinding);
        try {
            owner = scriptHelper.load(scriptHelper.getClassLoader().getScriptName());
        } finally {
            if (oldThreadBinding == null) {
                ScriptHelper.THREAD_BINDING.remove();
            } else {
                ScriptHelper.THREAD_BINDING.set(oldThreadBinding);
            }
        }
    }
    AsyncChannel asyncChan = AsyncChannel.open(sharedThreadPool, channel, qSize, policy, body, completed, owner, asyncBinding);
    String var = resolve(attributes, "var", String.class);
    if (var != null && var.length() > 0) {
        bind(body, var, asyncChan);
    }
    if (asyncContext != null) {
        asyncContext.add(asyncChan.getFuture());
    }
    return asyncChan;
}
Also used : Policy(com.disney.groovity.util.AsyncChannel.Policy) Binding(groovy.lang.Binding) Closure(groovy.lang.Closure) AsyncChannel(com.disney.groovity.util.AsyncChannel) CharArrayWriter(java.io.CharArrayWriter) ScriptHelper(com.disney.groovity.util.ScriptHelper) Map(java.util.Map) AwaitContext(com.disney.groovity.tags.Await.AwaitContext)

Aggregations

AwaitContext (com.disney.groovity.tags.Await.AwaitContext)1 AsyncChannel (com.disney.groovity.util.AsyncChannel)1 Policy (com.disney.groovity.util.AsyncChannel.Policy)1 ScriptHelper (com.disney.groovity.util.ScriptHelper)1 Binding (groovy.lang.Binding)1 Closure (groovy.lang.Closure)1 CharArrayWriter (java.io.CharArrayWriter)1 Map (java.util.Map)1