Пример #1
0
 @SuppressWarnings("unchecked")
 public <T extends Command> T getCommand(Class<T> clazz) {
   for (Command cmd : _cmds) {
     if (cmd.getClass() == clazz) {
       return (T) cmd;
     }
   }
   return null;
 }
Пример #2
0
 protected void setInSequence(Command[] cmds) {
   if (cmds == null) {
     return;
   }
   for (Command cmd : cmds) {
     if (cmd.executeInSequence()) {
       setInSequence(true);
       break;
     }
   }
 }
Пример #3
0
  protected String log(String msg, boolean logContent, Level level) {
    StringBuilder content = new StringBuilder();
    if (logContent) {
      if (_cmds == null) {
        try {
          _cmds =
              s_gson.fromJson(
                  _content, this instanceof Response ? Answer[].class : Command[].class);
        } catch (RuntimeException e) {
          s_logger.error("Unable to convert to json: " + _content);
          throw e;
        }
      }
      try {
        s_gogger.toJson(_cmds, content);
      } catch (Throwable e) {
        StringBuilder buff = new StringBuilder();
        for (Command cmd : _cmds) {
          buff.append(cmd.getClass().getSimpleName()).append("/");
        }
        s_logger.error("Gson serialization error " + buff.toString(), e);
        assert false : "More gson errors on " + buff.toString();
        return "";
      }
      if (content.length() <= (1 + _cmds.length * 3)) {
        return null;
      }
    } else {
      if (_cmds == null) {
        _cmds =
            s_gson.fromJson(_content, this instanceof Response ? Answer[].class : Command[].class);
      }
      content.append("{ ");
      for (Command cmd : _cmds) {
        content.append(cmd.getClass().getSimpleName()).append(", ");
      }
      content.replace(content.length() - 2, content.length(), " }");
    }

    StringBuilder buf = new StringBuilder("Seq ");

    buf.append(_agentId).append("-").append(_seq).append(": ");

    buf.append(msg);
    buf.append(" { ").append(getType());
    buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via);
    buf.append(", Ver: ").append(_ver.toString());
    buf.append(", Flags: ").append(Integer.toBinaryString(getFlags())).append(", ");
    buf.append(content);
    buf.append(" }");
    return buf.toString();
  }
Пример #4
0
  public <T extends Command> Answer getAnswerFor(Class<T> clazz) {
    assert (clazz != Command.class)
        : "You passed in a generic Command.  Seriously, you think you did that?";
    int i = 0;
    for (Command cmd : _cmds) {
      if (cmd.getClass() == clazz) {
        break;
      }
      i++;
    }

    assert i < _cmds.size() : "You sure you actually sent this command " + clazz;

    return _answers[i];
  }