public void testExecuteWorkflow() throws Exception {
    {
      final FrameworkProject frameworkProject =
          testFramework.getFrameworkProjectMgr().getFrameworkProject(TEST_PROJECT);
      final INodeSet nodes = frameworkProject.getNodeSet();
      assertNotNull(nodes);
      assertEquals(2, nodes.getNodes().size());
    }

    {
      // test empty workflow
      final NodeSet nodeset = new NodeSet();
      final WorkflowImpl workflow =
          new WorkflowImpl(new ArrayList<ExecutionItem>(), 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, interpreterMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(0, interpreterMock.executionItemList.size());
    }
    {
      // test undefined workflow item
      final NodeSet nodeset = new NodeSet();
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      commands.add(new testWorkflowCmdItem());

      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset.nodeSelectorWithDefaultAll())
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, interpreterMock);
      interpreterService.registerInstance(
          WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, interpreterMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertFalse(result.isSuccess());
      assertEquals(0, interpreterMock.executionItemList.size());
      assertNotNull("threw exception: " + result.getException(), result.getException());
      assertTrue(
          "threw exception: " + result.getException(),
          result.getException() instanceof WorkflowStepFailureException);
      assertEquals(
          "threw exception: " + result.getException(),
          "Step 1 of the workflow threw an exception: Failed dispatching to node test1: provider name was null for Service: CommandInterpreter",
          result.getException().getMessage());
    }

    {
      // test script exec item
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      final ExecutionItem testWorkflowCmdItem =
          new ScriptFileCommandBase() {
            @Override
            public String getScript() {
              return "a command";
            }
          };
      commands.add(testWorkflowCmdItem);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      interpreterService.registerInstance("exec", failMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, failMock);

      // set resturn result
      interpreterMock.resultList.add(
          new InterpreterResult() {
            public boolean isSuccess() {
              return true;
            }
          });

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, interpreterMock.executionItemList.size());
      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) executionItem1;
      assertEquals("a command", scriptItem.getScript());
      assertNull(scriptItem.getScriptAsStream());
      assertNull(scriptItem.getServerScriptFilePath());
      assertEquals(1, interpreterMock.executionContextList.size());
      final ExecutionContext executionContext = interpreterMock.executionContextList.get(0);
      assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
      assertNull(executionContext.getArgs());
      assertNull(executionContext.getDataContext());
      assertEquals(0, executionContext.getLoglevel());
      assertEquals("user1", executionContext.getUser());
      assertEquals(
          "expected " + nodeset + ", but was " + executionContext.getNodeSelector(),
          nodeset,
          executionContext.getNodeSelector());
    }
    {
      // test command exec item
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();
      final ExecutionItem testWorkflowCmdItem =
          new ExecCommandBase() {
            @Override
            public String[] getCommand() {
              return new String[] {"a", "command"};
            }
          };

      commands.add(testWorkflowCmdItem);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, failMock);

      // set resturn result
      interpreterMock.resultList.add(
          new InterpreterResult() {
            public boolean isSuccess() {
              return true;
            }
          });

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, interpreterMock.executionItemList.size());
      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ExecCommandExecutionItem);
      ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
      assertNotNull(execItem.getCommand());
      assertEquals(2, execItem.getCommand().length);
      assertEquals("a", execItem.getCommand()[0]);
      assertEquals("command", execItem.getCommand()[1]);
      assertEquals(1, interpreterMock.executionContextList.size());
      final ExecutionContext executionContext = interpreterMock.executionContextList.get(0);
      assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
      assertNull(executionContext.getArgs());
      assertNull(executionContext.getDataContext());
      assertEquals(0, executionContext.getLoglevel());
      assertEquals("user1", executionContext.getUser());
      assertEquals(nodeset, executionContext.getNodeSelector());
    }
    {
      // test workflow of three successful items
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();

      final ExecutionItem testWorkflowCmdItem =
          new ExecCommandBase() {
            @Override
            public String[] getCommand() {
              return new String[] {"a", "2", "command"};
            }
          };

      commands.add(testWorkflowCmdItem);

      final ExecutionItem testWorkflowCmdItemScript =
          new ScriptFileCommandBase() {
            @Override
            public String getScript() {
              return "a command";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "1"};
            }
          };
      commands.add(testWorkflowCmdItemScript);

      final ExecutionItem testWorkflowCmdItemScript2 =
          new ScriptFileCommandBase() {
            @Override
            public String getServerScriptFilePath() {
              return "/some/file/path";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "2"};
            }
          };
      commands.add(testWorkflowCmdItemScript2);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .args(new String[] {"test", "args"})
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);

      // set resturn results
      interpreterMock.resultList.add(new testResult(true, 0));
      interpreterMock.resultList.add(new testResult(true, 1));
      interpreterMock.resultList.add(new testResult(true, 2));

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertNull("threw exception: " + result.getException(), result.getException());
      assertTrue(result.isSuccess());
      assertEquals(1, result.getResultSet().size());
      assertNotNull(result.getResultSet());
      assertNotNull(
          "missing key " + testnode + ": " + result.getResultSet().keySet(),
          result.getResultSet().get(testnode));
      final List<StatusResult> test1 = result.getResultSet().get(testnode);
      assertEquals(3, test1.size());
      for (final int i : new int[] {0, 1, 2}) {
        final StatusResult interpreterResult = test1.get(i);
        assertTrue(interpreterResult instanceof testResult);
        testResult val = (testResult) interpreterResult;
        assertTrue(val.isSuccess());
        assertEquals(i, val.flag);
      }

      assertEquals(3, interpreterMock.executionItemList.size());

      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ExecCommandExecutionItem);
      ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
      assertNotNull(execItem.getCommand());
      assertEquals(3, execItem.getCommand().length);
      assertEquals("a", execItem.getCommand()[0]);
      assertEquals("2", execItem.getCommand()[1]);
      assertEquals("command", execItem.getCommand()[2]);

      final ExecutionItem item2 = interpreterMock.executionItemList.get(1);
      assertTrue(
          "wrong class: " + item2.getClass().getName(),
          item2 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) item2;
      assertEquals("a command", scriptItem.getScript());
      assertNull(scriptItem.getScriptAsStream());
      assertNull(scriptItem.getServerScriptFilePath());

      final ExecutionItem item3 = interpreterMock.executionItemList.get(2);
      assertTrue(
          "wrong class: " + item3.getClass().getName(),
          item2 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem2 = (ScriptFileCommandExecutionItem) item3;
      assertNull(scriptItem2.getScript());
      assertNull(scriptItem2.getScriptAsStream());
      assertEquals("/some/file/path", scriptItem2.getServerScriptFilePath());
      assertNotNull(scriptItem2.getArgs());
      assertEquals(2, scriptItem2.getArgs().length);
      assertEquals("-testargs", scriptItem2.getArgs()[0]);
      assertEquals("2", scriptItem2.getArgs()[1]);

      assertEquals(3, interpreterMock.executionContextList.size());

      for (final int i : new int[] {0, 1, 2}) {
        final ExecutionContext executionContext = interpreterMock.executionContextList.get(i);
        assertEquals("item " + i, TEST_PROJECT, executionContext.getFrameworkProject());
        assertNull("item " + i, executionContext.getDataContext());
        assertEquals("item " + i, 0, executionContext.getLoglevel());
        assertEquals("item " + i, "user1", executionContext.getUser());
        assertEquals("item " + i, nodeset, executionContext.getNodeSelector());
        assertNotNull("item " + i, executionContext.getArgs());
        assertEquals("item " + i, 2, executionContext.getArgs().length);
        assertEquals("item " + i, "test", executionContext.getArgs()[0]);
        assertEquals("item " + i, "args", executionContext.getArgs()[1]);
      }
    }
    {
      // test a workflow with a failing item (1), with keepgoing=false
      final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
      final ArrayList<ExecutionItem> commands = new ArrayList<ExecutionItem>();

      final ExecutionItem testWorkflowCmdItem =
          new ExecCommandBase() {
            @Override
            public String[] getCommand() {
              return new String[] {"a", "2", "command"};
            }
          };

      commands.add(testWorkflowCmdItem);

      final ExecutionItem testWorkflowCmdItemScript =
          new ScriptFileCommandBase() {
            @Override
            public String getScript() {
              return "a command";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "1"};
            }
          };
      commands.add(testWorkflowCmdItemScript);

      final ExecutionItem testWorkflowCmdItemScript2 =
          new ScriptFileCommandBase() {
            @Override
            public String getServerScriptFilePath() {
              return "/some/file/path";
            }

            @Override
            public String[] getArgs() {
              return new String[] {"-testargs", "2"};
            }
          };
      commands.add(testWorkflowCmdItemScript2);
      final WorkflowImpl workflow =
          new WorkflowImpl(commands, 1, false, WorkflowStrategy.STEP_FIRST);
      workflow.setKeepgoing(false);
      final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
      final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
      final com.dtolabs.rundeck.core.execution.ExecutionContext context =
          new ExecutionContextImpl.Builder()
              .frameworkProject(TEST_PROJECT)
              .user("user1")
              .nodeSelector(nodeset)
              .executionListener(new testListener())
              .framework(testFramework)
              .build();

      // setup testInterpreter for all command types
      final CommandInterpreterService interpreterService =
          CommandInterpreterService.getInstanceForFramework(testFramework);
      testInterpreter interpreterMock = new testInterpreter();
      testInterpreter failMock = new testInterpreter();
      failMock.shouldThrowException = true;
      //            interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE,
      // interpreterMock);
      interpreterService.registerInstance("exec", interpreterMock);
      interpreterService.registerInstance("script", interpreterMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
      interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);

      // set resturn results, fail on second item
      interpreterMock.resultList.add(new testResult(true, 0));
      interpreterMock.resultList.add(new testResult(false, 1));
      interpreterMock.resultList.add(new testResult(true, 2));

      final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);

      assertNotNull(result);
      if (!result.isSuccess() && null != result.getException()) {
        result.getException().printStackTrace(System.err);
      }
      assertFalse(result.isSuccess());
      assertNotNull("threw exception: " + result.getException(), result.getException());
      assertTrue(
          "threw exception: " + result.getException(),
          result.getException() instanceof WorkflowStepFailureException);
      WorkflowStepFailureException wfsfe = (WorkflowStepFailureException) result.getException();
      assertEquals(2, wfsfe.getWorkflowStep());
      assertNotNull(wfsfe.getExecutionResult());
      final ExecutionResult executionResult = wfsfe.getExecutionResult();
      assertNotNull(executionResult.getResultObject());
      assertNotNull(executionResult.getResultObject().getResults());
      assertEquals(1, executionResult.getResultObject().getResults().size());
      assertNotNull(executionResult.getResultObject().getResults().get(testnode));
      final StatusResult testnode1 = executionResult.getResultObject().getResults().get(testnode);
      assertNotNull(testnode1);
      assertTrue(testnode1 instanceof testResult);
      testResult failResult = (testResult) testnode1;
      assertEquals(1, failResult.flag);

      assertEquals(1, result.getResultSet().size());
      assertNotNull(result.getResultSet());
      assertNotNull(
          "missing key" + testnode + ": " + result.getResultSet().keySet(),
          result.getResultSet().get(testnode));
      final List<StatusResult> test1 = result.getResultSet().get(testnode);
      assertEquals(2, test1.size());
      for (final int i : new int[] {0, 1}) {
        final StatusResult interpreterResult = test1.get(i);
        assertTrue(interpreterResult instanceof testResult);
        testResult val = (testResult) interpreterResult;
        assertEquals(i, val.flag);
        if (0 == i) {
          assertTrue(val.isSuccess());
        } else {
          assertFalse(val.isSuccess());
        }
      }

      assertEquals(2, interpreterMock.executionItemList.size());

      final ExecutionItem executionItem1 = interpreterMock.executionItemList.get(0);
      assertTrue(
          "wrong class: " + executionItem1.getClass().getName(),
          executionItem1 instanceof ExecCommandExecutionItem);
      ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
      assertNotNull(execItem.getCommand());
      assertEquals(3, execItem.getCommand().length);
      assertEquals("a", execItem.getCommand()[0]);
      assertEquals("2", execItem.getCommand()[1]);
      assertEquals("command", execItem.getCommand()[2]);

      final ExecutionItem item2 = interpreterMock.executionItemList.get(1);
      assertTrue(
          "wrong class: " + item2.getClass().getName(),
          item2 instanceof ScriptFileCommandExecutionItem);
      ScriptFileCommandExecutionItem scriptItem = (ScriptFileCommandExecutionItem) item2;
      assertEquals("a command", scriptItem.getScript());
      assertNull(scriptItem.getScriptAsStream());
      assertNull(scriptItem.getServerScriptFilePath());
      assertNotNull(scriptItem.getArgs());
      assertEquals(2, scriptItem.getArgs().length);
      assertEquals("-testargs", scriptItem.getArgs()[0]);
      assertEquals("1", scriptItem.getArgs()[1]);

      assertEquals(2, interpreterMock.executionContextList.size());

      for (final int i : new int[] {0, 1}) {
        final ExecutionContext executionContext = interpreterMock.executionContextList.get(i);
        assertEquals(TEST_PROJECT, executionContext.getFrameworkProject());
        assertNull(executionContext.getDataContext());
        assertEquals(0, executionContext.getLoglevel());
        assertEquals("user1", executionContext.getUser());
        assertEquals(nodeset, executionContext.getNodeSelector());
        assertNull(executionContext.getArgs());
      }
    }
  }
  public DispatcherResult dispatch(
      final ExecutionContext context, final ExecutionItem item, final Dispatchable toDispatch)
      throws DispatcherException {
    final NodesSelector nodesSelector = context.getNodeSelector();
    INodeSet nodes = null;
    try {
      nodes =
          framework.filterAuthorizedNodes(
              context.getFrameworkProject(),
              new HashSet<String>(Arrays.asList("read", "run")),
              framework.filterNodeSet(
                  nodesSelector, context.getFrameworkProject(), context.getNodesFile()));
    } catch (NodeFileParserException e) {
      throw new DispatcherException(e);
    }
    if (nodes.getNodes().size() < 1) {
      throw new DispatcherException("No nodes matched");
    }
    boolean keepgoing = context.isKeepgoing();

    context
        .getExecutionListener()
        .log(4, "preparing for sequential execution on " + nodes.getNodes().size() + " nodes");
    final HashSet<String> nodeNames = new HashSet<String>(nodes.getNodeNames());
    final HashMap<String, Object> failures = new HashMap<String, Object>();
    FailedNodesListener failedListener = context.getExecutionListener().getFailedNodesListener();
    if (null != failedListener) {
      failedListener.matchedNodes(nodeNames);
    }
    boolean interrupted = false;
    final Thread thread = Thread.currentThread();
    boolean success = true;
    final HashMap<String, StatusResult> resultMap = new HashMap<String, StatusResult>();
    final Collection<INodeEntry> nodes1 = nodes.getNodes();
    // reorder based on configured rank property and order
    final String rankProperty =
        null != context.getNodeRankAttribute() ? context.getNodeRankAttribute() : "nodename";
    final boolean rankAscending = context.isNodeRankOrderAscending();
    final INodeEntryComparator comparator = new INodeEntryComparator(rankProperty);
    final TreeSet<INodeEntry> orderedNodes =
        new TreeSet<INodeEntry>(rankAscending ? comparator : Collections.reverseOrder(comparator));
    orderedNodes.addAll(nodes1);
    for (final Object node1 : orderedNodes) {
      if (thread.isInterrupted()
          || thread instanceof ExecutionServiceThread
              && ((ExecutionServiceThread) thread).isAborted()) {
        interrupted = true;
        break;
      }
      final INodeEntry node = (INodeEntry) node1;
      context
          .getExecutionListener()
          .log(
              Constants.DEBUG_LEVEL,
              "Executing command on node: " + node.getNodename() + ", " + node.toString());
      try {

        if (thread.isInterrupted()
            || thread instanceof ExecutionServiceThread
                && ((ExecutionServiceThread) thread).isAborted()) {
          interrupted = true;
          break;
        }
        final StatusResult result;
        final ExecutionContext interimcontext =
            new ExecutionContextImpl.Builder(context)
                .nodeSelector(SelectorUtils.singleNode(node.getNodename()))
                .build();
        if (null != item) {
          result = framework.getExecutionService().interpretCommand(interimcontext, item, node);
        } else {
          result = toDispatch.dispatch(interimcontext, node);
        }
        if (null != result) {
          resultMap.put(node.getNodename(), result);
        }
        if (null == result || !result.isSuccess()) {
          success = false;
          //                    context.getExecutionListener().log(Constants.ERR_LEVEL,
          //                        "Failed execution for node " + node.getNodename() + ": " +
          // result);
          if (null != result) {
            failures.put(node.getNodename(), result);
          } else {
            failures.put(node.getNodename(), "Failed execution, result was null");
          }
          if (!keepgoing) {
            break;
          }
        } else {
          nodeNames.remove(node.getNodename());
        }
      } catch (Throwable e) {
        success = false;
        failures.put(
            node.getNodename(), "Error dispatching command to the node: " + e.getMessage());
        context
            .getExecutionListener()
            .log(
                Constants.ERR_LEVEL,
                "Failed dispatching to node " + node.getNodename() + ": " + e.getMessage());

        final StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        context
            .getExecutionListener()
            .log(
                Constants.DEBUG_LEVEL,
                "Failed dispatching to node "
                    + node.getNodename()
                    + ": "
                    + stringWriter.toString());

        if (!keepgoing) {
          if (failures.size() > 0 && null != failedListener) {
            // tell listener of failed node list
            failedListener.nodesFailed(failures);
          }
          throw new DispatcherException(
              "Failed dispatching to node " + node.getNodename() + ": " + e.getMessage(), e, node);
        }
      }
    }
    if (keepgoing && nodeNames.size() > 0) {
      if (null != failedListener) {
        // tell listener of failed node list
        failedListener.nodesFailed(failures);
      }
      // now fail
      // XXX: needs to change from exception
      throw new NodesetFailureException(failures);
    } else if (null != failedListener && failures.isEmpty() && !interrupted) {
      failedListener.nodesSucceeded();
    }
    if (interrupted) {
      throw new DispatcherException("Node dispatch interrupted");
    }

    final boolean status = success;
    return new DispatcherResult() {
      public Map<String, ? extends StatusResult> getResults() {
        return resultMap;
      }

      public boolean isSuccess() {
        return status;
      }

      @Override
      public String toString() {
        return "DispatcherResult{"
            + "status="
            + isSuccess()
            + ", "
            + "results="
            + getResults()
            + "}";
      }
    };
  }