All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.remotecontrol.server.CommandChainInvoker Maven / Gradle / Ivy

package io.remotecontrol.server;

import io.remotecontrol.CommandChain;
import io.remotecontrol.groovy.ClosureCommand;
import io.remotecontrol.result.Result;
import io.remotecontrol.result.ResultFactory;
import org.codehaus.groovy.runtime.InvokerInvocationException;

public class CommandChainInvoker {

    private final ClassLoader parentLoader;
    private final CommandChain commandChain;
    private final ResultFactory resultFactory;

    public CommandChainInvoker(ClassLoader parentLoader, CommandChain commandChain, ResultFactory resultFactory) {
        this.resultFactory = resultFactory;
        this.parentLoader = parentLoader;
        this.commandChain = commandChain;
    }

    public Result invokeAgainst(Object delegate, Object firstArg) {
        Object arg = firstArg;

        for (ClosureCommand command : commandChain.getCommands()) {
            CommandInvoker invoker = createInvoker(parentLoader, command);
            try {
                arg = invoker.invokeAgainst(delegate, arg);
            } catch (InvokerInvocationException e) {
                return resultFactory.forThrown(e.getCause());
            } catch (Throwable throwable) {
                return resultFactory.forThrown(throwable);
            }
        }

        return resultFactory.forValue(arg);
    }

    protected CommandInvoker createInvoker(ClassLoader loader, ClosureCommand command) {
        return new CommandInvoker(loader, command);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy