Search in sources :

Example 1 with CommandResult

use of org.activityinfo.shared.command.result.CommandResult in project activityinfo by bedatadriven.

the class BatchCommandHandler method execute.

@Override
public void execute(BatchCommand batch, ExecutionContext context, final AsyncCallback<BatchResult> callback) {
    if (batch.getCommands().isEmpty()) {
        LOGGER.warning("Received empty batch command");
        callback.onSuccess(new BatchResult(Lists.<CommandResult>newArrayList()));
    } else {
        final ArrayList<CommandResult> results = new ArrayList<CommandResult>();
        for (Command command : batch.getCommands()) {
            results.add(null);
        }
        final boolean[] finished = new boolean[batch.getCommands().size()];
        final List<Throwable> exceptions = Lists.newArrayList();
        for (int i = 0; i != batch.getCommands().size(); ++i) {
            final int commandIndex = i;
            context.execute(batch.getCommands().get(i), new AsyncCallback<CommandResult>() {

                @Override
                public void onFailure(Throwable caught) {
                    if (exceptions.isEmpty()) {
                        exceptions.add(caught);
                        callback.onFailure(caught);
                    }
                }

                @Override
                public void onSuccess(CommandResult result) {
                    results.set(commandIndex, result);
                    finished[commandIndex] = true;
                    if (all(finished)) {
                        callback.onSuccess(new BatchResult(results));
                    }
                }
            });
        }
    }
}
Also used : BatchCommand(org.activityinfo.shared.command.BatchCommand) Command(org.activityinfo.shared.command.Command) ArrayList(java.util.ArrayList) BatchResult(org.activityinfo.shared.command.result.BatchResult) CommandResult(org.activityinfo.shared.command.result.CommandResult)

Example 2 with CommandResult

use of org.activityinfo.shared.command.result.CommandResult in project activityinfo by bedatadriven.

the class JsonRpcServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Command command;
    try {
        String json = new String(ByteStreams.toByteArray(req.getInputStream()));
        command = objectMapper.readValue(json, Command.class);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to deserialize command", e);
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    CommandResult result = dispatcher.execute(command);
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("application/json");
    objectMapper.writeValue(resp.getOutputStream(), result);
}
Also used : Command(org.activityinfo.shared.command.Command) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) CommandResult(org.activityinfo.shared.command.result.CommandResult)

Example 3 with CommandResult

use of org.activityinfo.shared.command.result.CommandResult in project activityinfo by bedatadriven.

the class DispatcherStub method findResult.

private CommandResult findResult(Command command) {
    CommandResult result = results.get(command);
    if (result == null) {
        result = resultByClass.get(command.getClass());
        if (result == null) {
            throw new AssertionError("Unexpected command: " + command.toString());
        }
    }
    log.add(command);
    return result;
}
Also used : CommandResult(org.activityinfo.shared.command.result.CommandResult)

Example 4 with CommandResult

use of org.activityinfo.shared.command.result.CommandResult in project activityinfo by bedatadriven.

the class DispatcherStub method execute.

@Override
public <T extends CommandResult> void execute(Command<T> command, AsyncCallback<T> callback) {
    if (command instanceof BatchCommand) {
        BatchCommand batch = (BatchCommand) command;
        List<CommandResult> results = new ArrayList<CommandResult>();
        for (Command batchCmd : batch.getCommands()) {
            results.add(findResult(batchCmd));
        }
        callback.onSuccess((T) new BatchResult(results));
    } else {
        callback.onSuccess((T) findResult(command));
    }
}
Also used : BatchCommand(org.activityinfo.shared.command.BatchCommand) Command(org.activityinfo.shared.command.Command) ArrayList(java.util.ArrayList) BatchCommand(org.activityinfo.shared.command.BatchCommand) BatchResult(org.activityinfo.shared.command.result.BatchResult) CommandResult(org.activityinfo.shared.command.result.CommandResult)

Example 5 with CommandResult

use of org.activityinfo.shared.command.result.CommandResult in project activityinfo by bedatadriven.

the class MockRemoteCommandService method execute.

@Override
public void execute(String authToken, List<Command> cmds, AsyncCallback<List<CommandResult>> callback) {
    List<CommandResult> results = new ArrayList<CommandResult>();
    for (Command cmd : cmds) {
        Integer count = commandCounts.get(cmd.getClass());
        commandCounts.put(cmd.getClass(), count == null ? 1 : count + 1);
        if (schema != null && cmd instanceof GetSchema) {
            results.add(schema);
        } else {
            results.add(mockExecute(cmd));
        }
    }
    callback.onSuccess(results);
}
Also used : Command(org.activityinfo.shared.command.Command) ArrayList(java.util.ArrayList) GetSchema(org.activityinfo.shared.command.GetSchema) CommandResult(org.activityinfo.shared.command.result.CommandResult)

Aggregations

CommandResult (org.activityinfo.shared.command.result.CommandResult)8 Command (org.activityinfo.shared.command.Command)5 ArrayList (java.util.ArrayList)4 CommandException (org.activityinfo.shared.exception.CommandException)3 LogException (org.activityinfo.server.util.logging.LogException)2 BatchCommand (org.activityinfo.shared.command.BatchCommand)2 BatchResult (org.activityinfo.shared.command.result.BatchResult)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 User (org.activityinfo.server.database.hibernate.entity.User)1 GetSchema (org.activityinfo.shared.command.GetSchema)1 InvalidAuthTokenException (org.activityinfo.shared.exception.InvalidAuthTokenException)1