Exemple #1
0
    /**
     * Invokes an operation and puts the return value into map
     *
     * @param map
     * @param operation Protocol.OperationName[args], e.g. STABLE.foo[arg1 arg2 arg3]
     */
    protected void handleOperation(Map<String, String> map, String operation) throws Exception {
      int index = operation.indexOf(".");
      if (index == -1)
        throw new IllegalArgumentException(
            "operation " + operation + " is missing the protocol name");
      String prot_name = operation.substring(0, index);
      Protocol prot = prot_stack.findProtocol(prot_name);
      if (prot == null) return; // less drastic than throwing an exception...

      int args_index = operation.indexOf("[");
      String method_name;
      if (args_index != -1) method_name = operation.substring(index + 1, args_index).trim();
      else method_name = operation.substring(index + 1).trim();

      String[] args = null;
      if (args_index != -1) {
        int end_index = operation.indexOf("]");
        if (end_index == -1) throw new IllegalArgumentException("] not found");
        List<String> str_args =
            Util.parseCommaDelimitedStrings(operation.substring(args_index + 1, end_index));
        Object[] strings = str_args.toArray();
        args = new String[strings.length];
        for (int i = 0; i < strings.length; i++) args[i] = (String) strings[i];
      }

      Method method = MethodCall.findMethod(prot.getClass(), method_name, args);
      if (method == null) {
        log.warn(
            Util.getMessage("MethodNotFound"),
            local_addr,
            prot.getClass().getSimpleName(),
            method_name);
        return;
      }
      MethodCall call = new MethodCall(method);
      Object[] converted_args = null;
      if (args != null) {
        converted_args = new Object[args.length];
        Class<?>[] types = method.getParameterTypes();
        for (int i = 0; i < args.length; i++)
          converted_args[i] = MethodCall.convert(args[i], types[i]);
      }
      Object retval = call.invoke(prot, converted_args);
      if (retval != null) map.put(prot_name + "." + method_name, retval.toString());
    }